I want it so that when i click a button(button1) and there is no text in textbox1, the button does not work and a msgbox comes out saying "You need to enter a text". What will the code be? Thanks!
Printable View
I want it so that when i click a button(button1) and there is no text in textbox1, the button does not work and a msgbox comes out saying "You need to enter a text". What will the code be? Thanks!
vb.net Code:
Private Sub btnButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnButton1.Click If TextBox1.Text = "" Then MessageBox.Show("You need to enter a text", "Missing Information", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1) End If End Sub
Ok what my button does is generate a random code, i implemented this code and it does show the error message box but, it still generates a code in "TEXTBOX2" How can i not make it generate a random code in textbox2 if there is no text in textbox1?
Also, note that you did say "the button does not work". You can disable the button so that it will not respond if a user tries to click it:
vb.net Code:
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged If TextBox1.Text = "" Then Button1.Enabled = False Else If TextBox1.Text <> "" Then Button1.Enabled = True End If End Sub
THX SO MUCH and i found the right code to make this work
Thx for the helpCode:If TextBox1.Text = "" Then
MessageBox.Show("You need to enter a text", "Missing Information", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
TextBox2.Text = "NO TEXT ENTERED!"
Else
TextBox2.Text = generateRandomCombination()
End If
So you've already got code in Button1 that generates something for TextBox2.
Change the code to this:
vb.net Code:
Private Sub btnButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnButton1.Click If TextBox1.Text = "" Then MessageBox.Show("You need to enter a text", "Missing Information", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1) Else If TextBox1.Text <> "" Then ' in this section, put the code that generates the code for TextBox2. End If End Sub
If TextBox1.Text = "" Then
MessageBox.Show("You need to enter a text", "Missing Information", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
Else If TextBox1.Text <> "" Then
' in this section, put the code that generates the code for TextBox2.
End If
The red code will check to see if TextBox1 is blank. If it is blank, then the error Message Box is shown.
The blue code will check to see if the TextBox1 is not blank. If it is not blank, then the code that fills TextBox2 is executed.