英文:
VB.Net Make Event through loop was duplicated
问题
我有一个代码片段,展示了如何创建一个事件列表。
在上面的代码中,一切都正常,但有一个问题是`MessageBox.Show`总是在while循环的最后显示一个固定的值。
我找不出它是如何工作的。你能帮助我吗!
英文:
I have a snip code to show how a list of events was created.
While tb.MoveNext()
Try
idMenu = tb.Current.Field(Of Integer)("idMenu")
menuName = tb.Current.Field(Of String)("HeaderName")
ClassType = tb.Current?.Field(Of String)("ClassType")
newTreeItem = New TreeViewItem()
Dim clickHandler As New MouseButtonEventHandler(Sub(sender, e)
MessageBox.Show(menuName)
End Sub)
AddHandler TryCast(newTreeItem, TreeViewItem).PreviewMouseDoubleClick, clickHandler
Catch ex As Exception
End Try
End While
In the above code, anything working ok but have a problem is MessageBox.Show
always shows a fixed value at the last of the while loop.
I can't find out how it works. Can you help me with that!
答案1
得分: 2
我认为你遇到了一个问题,这是因为你的 Message
在以下代码行中的设置方式:
> Sub(sender, e)
> MessageBox.Show(menuName)
> End Sub
变量 menuName
不是事件处理程序代码的一部分。
你可以尝试这样做:
Sub(sender, e)
Dim localMenuName as String = tb.Current.Field(Of String)("HeaderName")
MessageBox.Show(localMenuName)
End Sub
英文:
I think you are encountering a problem because of how your Message
is setup in the line:
> Sub(sender, e)
> MessageBox.Show(menuName)
> End Sub
The variable menuName
is not part of the EventHandler code.
You could try this:
Sub(sender, e)
Dim localMenuName as String = tb.Current.Field(Of String)("HeaderName")
MessageBox.Show(localMenuName)
End Sub
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论