本文介绍了事件处理VB.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! Dim textControl()作为标签' 标签对象数组 Dim txtcnt 作为 整数 公共 Sub addTextControl() txtcnt = txtcnt + 1 ' 整数跟踪创建的实例数量 ReDim textControl(txtcnt) 我 .Controls.Add(textControl (txtcnt)) AddHandler textControl(txtcnt).MouseDown, AddressOf 我 .textMouseDown 结束 Sub 私有 Sub textMouseDown( ByVal 标记作为 字符串) ' 返回引发事件的对象实例 ' 此时我需要帮助 结束 Sub 解决方案 从技术上讲,你应该向我们揭示一个问题,并要求一个正式的问题。 无论如何,如果你想将它用作MouseDown事件的事件处理程序,你的textMouseDown方法的签名无效: 私有 Sub TextMouseDown(发件人作为 对象,e As MouseEventArgs) Dim myLabel 作为标签= CType (发件人,标签) ' ... 结束 Sub 备注:在addTextControl方法中,您永远不会创建新的Label。我敢打赌这个方法会在运行时抛出NullReferenceException。 备注2:重新编译数组是一项代价高昂的操作。为什么不使用List(Of Label)? 您需要更改 textMouseDown 方法的签名以匹配你正在接线的事件。 这样的事可能适合你; Dim textControl()作为标签' 标签对象数组 Dim txtcnt As 整数 公共 Sub addTextControl() txtcnt = txtcnt + 1 ' 整数跟踪创建的实例数量 ReDim textControl(txtcnt) 我 .C ontrols.Add(textControl(txtcnt)) AddHandler textControl(txtcnt).MouseDown, AddressOf 我 .textMouseDown 结束 Sub ' 此方法需要此签名,不允许使用此签名任何签名 私有 Sub textMouseDown(sender 作为 对象,args 作为 MouseEventArgs) ' 实际标签 Dim currentLabel As Label = CType (发件人,标签) ' textControl数组中标签的索引 Dim currentIndex As Integer = textControl.ToList()。IndexOf(currentLabel) ' 其标记 Dim tag As String = currentLabel.Tag 结束 Sub 希望这有帮助, Fredrik 查看事件处理VB.net的这个小例子: 私有 _objLabel 作为标签 私有 _ControlId 作为 整数 = 0 公共 Sub AddLabelControl() _objLabel = 新标签' 创建标签控件的新对象 _ControlId = _ControlId + 1 ' 将控制ID计数增加1 _objLabel.Name = 标签& _ControlId ' 控制名称 _objLabel.Text = 标签& _ControlId ' 要显示的控件文本 _objLabel.Location = 新点( 10 , 20 * _ControlId)' 控制位置 AddHandler _objLabel.MouseDown, AddressOf _objLabelMouseDown ' 添加事件处理程序 Me .Controls.Add(_objLabel)' 在窗体上添加控件 结束 Sub 私有 Sub _objLabelMouseDown( ByVal sender 作为 对象, ByVal e As MouseEventArgs) MessageBox.Show( CType (sender,Label).Name)' 鼠标按下时显示控件名称 结束 Sub 测试此代码在 Form_Load _ControlId< 5 AddLabelControl()' 添加五个标签在表单上 结束 而 我希望这会对你有所帮助。 :) Dim textControl() As Label 'Array of label objectsDim txtcnt As IntegerPublic Sub addTextControl() txtcnt = txtcnt + 1 'Integer keeps track of number of instances created ReDim textControl(txtcnt) Me.Controls.Add(textControl(txtcnt)) AddHandler textControl(txtcnt).MouseDown, AddressOf Me.textMouseDown End Sub Private Sub textMouseDown(ByVal tag As String) 'Return which instance of the object raised the event 'I need help at this point End Sub 解决方案 Technically, you are supposed to expose us a problem and ask for a formal question.Anyway, your textMouseDown method has an invalid signature if you want to use it as an eventhandler for MouseDown event:Private Sub TextMouseDown(sender As Object, e As MouseEventArgs) Dim myLabel As Label = CType(sender, Label) ' ...End SubRemark: in your addTextControl method, you never create a new Label. I would bet this method throws a NullReferenceException at runtime.Remark 2: redim an array is a costly operation. Why not using a List(Of Label) instead?You need to change the signature of your textMouseDown method to match that of the event you're wiring up.Something like this might work for you;Dim textControl() As Label 'Array of label objectsDim txtcnt As IntegerPublic Sub addTextControl() txtcnt = txtcnt + 1 'Integer keeps track of number of instances created ReDim textControl(txtcnt) Me.Controls.Add(textControl(txtcnt)) AddHandler textControl(txtcnt).MouseDown, AddressOf Me.textMouseDownEnd Sub' This method needs this signature, it's not allowed to use just any signaturePrivate Sub textMouseDown(sender As Object, args As MouseEventArgs) ' The actual Label Dim currentLabel As Label = CType(sender, Label) ' The index of the label in your textControl array Dim currentIndex As Integer = textControl.ToList().IndexOf(currentLabel) ' Tag of it Dim tag As String = currentLabel.TagEnd SubHope this helps,FredrikSee this small example for Event Handling VB.net :Private _objLabel As LabelPrivate _ControlId As Integer = 0Public Sub AddLabelControl() _objLabel = New Label 'Create New Object Of Label Control _ControlId = _ControlId + 1 'Increase Control Id Count By 1 _objLabel.Name = "Label" & _ControlId 'Control Name _objLabel.Text = "Label" & _ControlId 'Control Text To Display _objLabel.Location = New Point(10, 20 * _ControlId) ' Control Location AddHandler _objLabel.MouseDown, AddressOf _objLabelMouseDown 'Adding Event Handler Me.Controls.Add(_objLabel) 'Adding Control On FormEnd SubPrivate Sub _objLabelMouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) MessageBox.Show(CType(sender, Label).Name) 'Display Control Name On Mouse DownEnd SubTo Test this Code Write following Code on Form_LoadWhile _ControlId < 5 AddLabelControl() 'Add Five Label's On FormEnd WhileI hope this will help you. :) 这篇关于事件处理VB.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-03 13:07