我想调用这个代码片段,传递一个类似于参数的“control want”,然后sub与所需的控件交互。
我怎么能做到这一点?
这是代码片段:
#Region " Move a control in real-time "
' Change Textbox1 to the desired control name
Private Sub TextBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles textbox1.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
textbox1.Capture = False
Dim ControlMoveMSG As Message = Message.Create(textbox1.Handle, &HA1, New IntPtr(2), IntPtr.Zero)
Me.DefWndProc(ControlMoveMSG)
End If
End Sub
#End Region更新:解决方案:
Private Sub MoveControl(sender As Object, e As EventArgs) Handles _
TextBox1.MouseDown, _
TextBox2.MouseDown, _
PictureBox1.MouseDown
Dim control As Control = CType(sender, Control)
control.Capture = False
Dim ControlMoveMSG As Message = Message.Create(control.Handle, &HA1, New IntPtr(2), IntPtr.Zero)
Me.DefWndProc(ControlMoveMSG)
End Sub发布于 2012-12-18 19:53:44
在这种情况下,您可以只使用sender。sender参数是对引发事件的任何控件的引用。因此,如果您将此相同的方法添加为多个控件的事件处理程序,则sender将是哪个控件引发了它当前正在处理的事件,例如:
Private Sub MouseDown(sender As Object, e As EventArgs) _
Handles TextBox1.MouseDown, TextBox2.MouseDown
' Note in the line above that this method handles the event
' for TextBox1 and TextBox2
Dim textBox As TextBox = CType(sender, TextBox)
' textBox will now be either TextBox1 or TextBox2, accordingly
textBox.Capture = False
' ....
End SubCType语句将基本Object参数强制转换为特定的TextBox类。在本例中,该方法只处理TextBox对象的事件,因此这是可行的。但是,如果让它处理来自其他类型控件的事件,则需要强制转换为更通用的Control类型(即CType(sender, Control))。
https://stackoverflow.com/questions/13931302
复制相似问题