简体   繁体   中英

VB 2010 Textboxes And Buttons Enabled/Disabled

i have one more question about a project in my visual basic 2010. i have 2 textboxes, one button and one progress bar. You have to type username (in tb1) and a random password (in tb2) so the button is enabled and the progress bar starts if i press it. I want the button to be disabled if i havent written any text in the textboxes and enabled if i have. I tried this code but it doesnt work.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 
        If TextBox1.Text <> "" And TextBox2.Text <> "" Then
            Button1.Enabled = True
        ElseIf Button1.Enabled = False Then
            MsgBox("Progress Fail.", MsgBoxStyle.Exclamation)
        End If
    End Sub

Have you any ideas on how can i solve this? Thanks for help

I added a list with radio buttons too. now i want button1 to be enabled if i choose one item from the radio buttons too. Thanks again

This needs to be done in some event of the text boxes, not the button. Try this:

Private Sub TextBox_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox2.TextChanged, TextBox1.TextChanged
    Button1.Enabled = TextBox1.Text <> "" And TextBox2.Text <> ""
End Sub

Set the Button to disabled at startup, and it should work fine. Note that the sub is used to work for both text boxes (see the Handles part).

---EDIT---

Okay, so we include the ComboBox as well in the check. I am a bit insecure... yes, you could just add another event to the Handles -part since the events have the same signature (= same parameters of same types). But this is getting a bit dirty in my opinion.

So let's do it rather like this:

Private Sub TextBox_TextChanged(sender As System.Object, e As System.EventArgs) _
     Handles TextBox2.TextChanged, TextBox1.TextChanged

    evaluateUserEntries()
End Sub

Private Sub ComboBox1_SelectedValueChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedValueChanged
    evaluateUserEntries()
End Sub

Private Sub evaluateUserEntries()
    Button1.Enabled = TextBox1.Text <> "" And TextBox2.Text <> "" And ComboBox1.SelectedIndex <> -1
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM