vb.net 在一个处理程序中处理所有文本框事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15358338/
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
Handling all textbox event in one Handler
提问by conquistador
I do know how to handle event of textboxes in my form. But want to make this code shorter because I will 30 textboxes. It's inefficient to use this:
我知道如何处理表单中的文本框事件。但想让这段代码更短,因为我将有 30 个文本框。使用它是低效的:
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged, TextBox3.TextChanged, TextBox4.TextChanged, TextBox5.TextChanged, TextBox6.TextChanged, TextBox7.TextChanged, TextBox8.TextChanged, TextBox9.TextChanged, TextBox10.TextChanged
Dim tb As TextBox = CType(sender, TextBox)
Select Case tb.Name
Case "TextBox1"
MsgBox(tb.Text)
Case "TextBox2"
MsgBox(tb.Text)
End Select
End Sub
Is there a way to shorten the handler?
有没有办法缩短处理程序?
回答by Tim Schmelter
You can use Controls.OfType+ AddHandlerprogrammatically. For example:
您可以以编程方式使用Controls.OfType+ AddHandler。例如:
Dim textBoxes = Me.Controls.OfType(Of TextBox)()
For Each txt In textBoxes
AddHandler txt.TextChanged, AddressOf txtTextChanged
Next
one handler for all:
一个处理程序:
Private Sub txtTextChanged(sender As Object, e As EventArgs)
Dim txt = DirectCast(sender, TextBox)
Select Case txt.Name
Case "TextBox1"
MsgBox(txt.Text)
Case "TextBox2"
MsgBox(txt.Text)
End Select
End Sub
回答by Rajaprabhu Aravindasamy
Say If you are having that 30 textboxesinside a panel(PnlTextBoxes), Now you can create handlerfor your textboxesdynamically like this below
说如果你textboxes在面板(PnlTextBoxes)中有那个 30 ,现在你可以像下面这样handler为你的textboxes动态创建
For each ctrl in PnlTextBoxes.controls
If TypeOf ctrl is TextBox then
AddHandler ctrl.TextChanged, AddressOf CommonClickHandler
end if
Next
Private Sub CommonHandler(ByVal sender As System.Object, _
ByVal e As System.EventArgs)
MsgBox(ctype(sender,TextBox).Text)
End Sub
回答by Jamby
If you have created very Textbox with the Designer, I don't think there is a better method.
如果您使用 Designer 创建了非常文本框,我认为没有更好的方法。
But, if you have created the Textboxes dynamically, you should AddHandler in this way:
但是,如果您动态创建了文本框,则应该以这种方式添加处理程序:
For i = 0 to 30
Dim TB as New Texbox
AddHandler TB.TextChanged, TextBox1_TextChanged
'Set every Property that you need
Me.Controls.Add(TB)
Next
回答by Cody Gray
The bestway would be to inherit from TextBox, override its OnTextChangedmethodto add your custom handling code, and then use that on your form(s) instead of the built-in TextBoxcontrol.
在最好的办法是继承TextBox,重写它的OnTextChanged方法来添加自定义的处理代码,然后使用您的形式(S),而不是内置的TextBox控制。
That way, allof the event handling code is in one single place and you increase abstraction. The behavior follows and is defined within the control class itself, not the form that contains the control. And of course, it frees you from having a bunch of ugly, hard-to-maintain Handlesstatements, or worse, slow and even uglier Forloops.
这样,所有的事件处理代码都在一个地方,你增加了抽象。行为遵循并在控件类本身中定义,而不是包含控件的表单。当然,它使您摆脱了一堆丑陋、难以维护的Handles语句,或者更糟糕的是,缓慢甚至更丑陋的For循环。
For example, add this code defining a new custom text box control to a new file in your project:
例如,将定义新的自定义文本框控件的此代码添加到项目中的新文件中:
Public Class CustomTextBox : Inherits TextBox
Protected Overridable Sub OnTextChanged(e As EventArgs)
' Do whatever you want to do here...
MsgBox(Me.Text)
' Call the base class implementation for default behavior.
' (If you don't call this, the TextChanged event will never be raised!)
MyBase.OnTextChanged(e)
End Sub
End Class
Then, after you recompile, you should be able to replace your existing TextBoxcontrols with the newly-defined CustomTextBoxcontrol that has all of your behavior built in.
然后,在重新编译后,您应该能够TextBox用新定义的CustomTextBox控件替换现有控件,该控件内置了您的所有行为。

