Hi

I am creating a button & textbox manually and have those codes in the form load event. Also I disabled the button & I have the number 1 in my text box.

May I please have the code to enable the button when any number greater than one is entered in the textbox?

Thank you in advance...

Hi

I am creating a button & textbox manually and have those codes in the form load event. Also I disabled the button & I have the number 1 in my text box.

May I please have the code to enable the button when any number greater than one is entered in the textbox?

Thank you in advance...

Give this a shot :cheesy:

Public Class Form1
 
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Button1.Enabled = False
End Sub
 
Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
If Val(TextBox1.Text) > 1 Then
Button1.Enabled = True
ElseIf Val(TextBox1.Text) <= 1 Then
Button1.Enabled = False
End If
End Sub
End Class

Give this a shot :cheesy:

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Button1.Enabled = False
End Sub

Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
If Val(TextBox1.Text) > 1 Then
Button1.Enabled = True
ElseIf Val(TextBox1.Text) <= 1 Then
Button1.Enabled = False
End If
End Sub
End Class

Hi

Thanks for your time

Yes! that is exactly my code too...

The code in the form load event was okay...since I am creating the button in there..

But for the text change event, it is giving me error at the text with RED below

Error is : Handles clause requires a Withevents variable defined in the containing type or one of its base type

I am very new to VB...and don't have any idea about the error...could anyone please help me!

Private Sub TextBox1_TextChanged(ByVal sender As Object, 
ByVal e As System.EventArgs) Handles [COLOR=Red]TextBox1[/COLOR].TextChanged

If Val(TextBox1.Text) > 1 Then
Button1.Enabled = True
ElseIf Val(TextBox1.Text) <= 1 Then
Button1.Enabled = False
End If

End Sub

Edited 3 Years Ago by pyTony: fixed formatting

Very odd, it works on my end.. You are using Visual Basic 2005 Express Edition, correct?

In your post you said:

I am creating a button & textbox manually and have those codes in the form load event. Also I disabled the button & I have the number 1 in my text box.

Do you mean you put them on the form programically or using the IDE. If you are codeing them you didnot show them in your form load evend

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Button1.Enabled = False
End Sub

If this is the case then the program does not know what TextBox1 is because it hasn't been declared yet and gets the error message.

I'm using visual studio .NET 2005

I think your design already have button1 & textbox1...

My design only has the Form1

My dilemma is that I am creating those in the Form_Load event


Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim Button1 As New Button()
Button1.Name = "Button1"
Button1.Text = "btn"
Button1.Size = New Size(75, 23)
Button1.Enabled = False

Dim Textbox1 As New TextBox
Textbox1.Name = "Textbox1"
Textbox1.Text = "1"
Textbox1.Size = New Size(30, 20)

End Sub

Private Sub Textbox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Textbox1.TextChanged

If Val(Textbox1.Text) > 1 Then
Button1.Enabled = True

ElseIf Val(TextBox1.Text) <= 1 Then
Button1.Enabled = False
End If

End Sub


End Class

any suggestions...

Move:
Dim Button1 As New Button
Dim Textbox1 As New TextBox
To outside the load form. When you dim it in a sub it is destroyed when you exit the sub or you need to add the control to the controls collection.

I'm using visual studio .NET 2005

I think your design already have button1 & textbox1...

My design only has the Form1

My dilemma is that I am creating those in the Form_Load event


Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim Button1 As New Button()
Button1.Name = "Button1"
Button1.Text = "btn"
Button1.Size = New Size(75, 23)
Button1.Enabled = False

Dim Textbox1 As New TextBox
Textbox1.Name = "Textbox1"
Textbox1.Text = "1"
Textbox1.Size = New Size(30, 20)

End Sub

Private Sub Textbox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Textbox1.TextChanged

If Val(Textbox1.Text) > 1 Then
Button1.Enabled = True

ElseIf Val(TextBox1.Text) <= 1 Then
Button1.Enabled = False
End If

End Sub


End Class

any suggestions...

Move:
Dim Button1 As New Button
Dim Textbox1 As New TextBox
To outside the load form. When you dim it in a sub it is destroyed when you exit the sub or you need to add the control to the controls collection.

No..I am still getting the error "Handles clause requires a WithEvents variable defined in the containing type or one of its base types."
@
the text with red below


Public Class Form1

Dim Button1 As New Button()
Dim Textbox1 As New TextBox()

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Button1.Name = "Button1"
Button1.Text = "btn"
Button1.Size = New Size(75, 23)
Controls.Add(Button1)
Button1.Enabled = False

Textbox1.Name = "Textbox1"
Textbox1.Text = "1"
Textbox1.Size = New Size(30, 20)
Textbox1.Location = New Point(70, 80)
Textbox1.BorderStyle = BorderStyle.Fixed3D
Controls.Add(Textbox1)

End Sub

Private Sub Textbox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Textbox1.textchanged

If Val(Textbox1.Text) > 1 Then
Button1.Enabled = True

ElseIf Val(TextBox1.Text) <= 1 Then
Button1.Enabled = False

End If

End Sub

End Class


Thanks for your time

Sorry about that. I meant to add that to my last post. You need to define your objects using WithEvents.

Dim WithEvents Button1 As New Button()
    Dim WithEvents Textbox1 As New TextBox()

YES it works...

but when you create buttons and textboxes dynamically, it's not working..

Anyways thanks a lot for the help

This article has been dead for over six months. Start a new discussion instead.