禁用和启用按钮vb.net

时间:2016-05-16 19:13:12

标签: vb.net

我想禁用"确定"加载validateAdd表单时按钮,然后启用"确定"用户在文本框中输入一些文本时按钮。

这是我的禁用和启用按钮的代码。

Private Sub validateAdd_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    If txtaddlname.Text = String.Empty AndAlso txtaddfname.Text = String.Empty AndAlso txtaddmid.Text = String.Empty Then
        btnaddOk.Enabled = False
    Else
        btnaddOk.Enabled = True
    End If
End Sub

这是我的带有空文本框的validateAdd表单: enter image description here

该按钮已被禁用,但当我尝试在其中放入一些文本时,该按钮仍处于禁用状态。 enter image description here

1 个答案:

答案 0 :(得分:1)

您应该将所有代码放在三个文本框的TextChanged事件中:

Private Sub fields_TextChanged(sender As Object, e As EventArgs) Handles txtaddlname.TextChanged, txtaddfname.TextChanged, txtaddmid.TextChanged
    If txtaddlname.Text = String.Empty AndAlso txtaddfname.Text = String.Empty AndAlso txtaddmid.Text = String.Empty Then
        btnaddOk.Enabled = False
    Else
        btnaddOk.Enabled = True
    End If
End Sub

由于Loaded的{​​{1}}事件只发生过一次。希望它有所帮助:)