vb.net - 根据用户输入启用/禁用按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25068848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
vb.net - enable/disable a button depending on user input
提问by Enigmativity
As I'm putting the finishing touches on my program I'm having some troubles. Theres several user inputs and a submit button, once the inputs has been filled I wish to enable the submit button, else the button should be disabled. This is what I have:
当我对我的程序进行最后润色时,我遇到了一些麻烦。有几个用户输入和一个提交按钮,一旦输入被填充,我希望启用提交按钮,否则按钮应该被禁用。这就是我所拥有的:
Private Sub ButtonControl(sender As System.Object, e As System.EventArgs) Handles Input1.Validated
If Input1.Text = "" Then
ButtonSubmit.Enabled = False
ElseIf Input1.Text <> "" Then
ButtonSubmit.Enabled = True
End If
End Sub
The thing is it disables nomatter what and then it doesnt enable when my input is filed
问题是它无论如何都会禁用,然后在提交我的输入时它不会启用
回答by Mark Hall
Your code will work if you have another control that can receive the focus. Control Validation occurs on the loss of focus. If you need to have just one focusable item active you will need to use either KeyPress, KeyDownor Textchangedevents to enable your button, also make sure that the CausesValidationproperty of your TextBox is true.
如果您有另一个可以接收焦点的控件,您的代码将起作用。控制验证在失去焦点时发生。如果您只需要激活一个可聚焦的项目,您将需要使用KeyPress、KeyDown或Textchanged事件来启用您的按钮,还要确保CausesValidation您的 TextBox的属性为真。
I would also make the method more generic so you could call it from multiple textbox's by using the sender object to access the textbox that raised the event. Also if you have a True/False condition you only need to do the comparison in the first if statement and then you just use an else not an elseif.
我还将使该方法更通用,以便您可以通过使用发送方对象访问引发事件的文本框从多个文本框调用它。此外,如果您有一个 True/False 条件,您只需要在第一个 if 语句中进行比较,然后您只需使用 else 而不是 elseif。
for example:
例如:
Private Sub ButtonControl(sender As System.Object, e As System.EventArgs) Handles Input1.Validated
If DirectCast(sender, TextBox).Text = "" Then
ButtonSubmit.Enabled = False
Else
ButtonSubmit.Enabled = True
End If
End Sub
You can also use the String.IsNullOrWhiteSpaceMethod to check if just spaces have been entered if you are using the 4.0 framework or above. Like this TextChangedEventHandler.
String.IsNullOrWhiteSpace如果您使用的是 4.0 或更高版本的框架,您还可以使用Method 检查是否只输入了空格。像这个TextChangedEventHandler。
Private Sub ButtonControl(sender As Object, e As EventArgs) Handles Input1.TextChanged
If String.IsNullOrWhiteSpace(DirectCast(sender, TextBox).Text) Then
ButtonSubmit.Enabled = False
Else
ButtonSubmit.Enabled = True
End If
End Sub
回答by Enigmativity
This is the kind of thing I would do:
这是我会做的事情:
Private Sub TextBoxes_TextChanged( _
ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles _
TextBox1.TextChanged, _
TextBox2.TextChanged, _
TextBox3.TextChanged
Dim textBoxes = { TextBox1, TextBox2, TextBox3 }
Button1.Enabled = textBoxes.All(Function (tb) tb.Text <> "")
End Sub
You can then add as many text boxes in to the textBoxesarray as you need to check. Just make sure that the text boxes to the Handleslist.
然后,您可以textBoxes根据需要在数组中添加任意数量的文本框。只需确保将文本框添加到Handles列表中即可。
回答by Nick
You are going to need to use the TextBox "TextChanged" event, and be sure to set each Textbox AutoPostback="True". You can use an UpdatePanel to make the postbacks that occur on each Textbox you wish to validate less obnoxious to your end-user.
您将需要使用TextBox" TextChanged" 事件,并确保设置每个 Textbox AutoPostback="True"。您可以使用 UpdatePanel 使您希望验证的每个文本框上发生的回发对最终用户不那么讨厌。
So, your textbox (if you have many, make sure they all have the OnTextChanged="ValidateForm":
所以,你的文本框(如果你有很多,确保它们都有 OnTextChanged="ValidateForm":
<asp:TextBox ID="Input1" runat="server" OnTextChanged="Validate_TextChanged" />
Inside your textchanged ("ValidateForm") event (which each Textbox is attached to), one quick to implement route to do would just be
在您的 textchanged ("ValidateForm") 事件(每个文本框都附加到该事件)中,一个快速实现的路由就是
' Validation inside this event
Protected Sub Validate_TextChanged(sender As Object, e As EventArgs)
if Input1.text <> "" AndAlso Input2.text <> "" AndAlso ' ...etc.
End Sub
If you go the route of the UpdatePanel, you may find thisuseful.
如果您采用 UpdatePanel 的路线,您可能会发现这很有用。
回答by wanamal
Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
If TextBox1.Text.Length > 0 Then
Me.Button1.Enabled = True
Else
Me.Button1.Enabled = False
End If
End Sub
回答by FutoRicky
Do that code in the Input1_TextChanged event
在 Input1_TextChanged 事件中执行该代码
This is what I did and worked:
这就是我所做的和工作的:
Dim CheckInput1 As Boolean
Private Sub Input1_TextChanged(sender As Object, e As EventArgs) Handles Input1.TextChanged, Input1.Enter
CheckInput1 = True
If Input1.Text = "" Then
CheckInput1 = False
End If
If CheckInput1 = False Then
ButtonSubmit.Enabled = False
ElseIf CheckInput1 = True Then
ButtonSubmit.Enabled = True
End If
End Sub
There must be a more efficient code that this but I think this solves your problem.
必须有一个更有效的代码,但我认为这可以解决您的问题。

