Have I Found a Bug in VB.NET?

G

Guest

The following code example show some perculiar behavior. I have a Form with a
listbox with "ContextMenu1" assigned to it. When I click in the Listbox, the
ContextMenu is displayed. After I make a menuItem selection in the
ContextMenu the event handing routine assigns the selected MenuItems
properties to the variables "ItemIndex" and "ItemName". Back in the routine
where the ContextMenu was displayed I would expect these variables to be
updated after the "ContextMenu1.Show(ListBox1, pos)" statement, but the 1st
MsgBox statement displays the variable values that were in effect before the
assignments in the event handling routine. Now here's the rub, Immediately
after this 1st MsgBox statement I execute another similar MsgBox statement
that then displays the correct updated values! It seem that I have some sort
if asynchronous threading issue here. Anyone have any input on this?

Here's the code...

‘ “ItemIndex†and “ItemName†are used to record selected MenuItem
‘ propoerties in the Event Handling routine “MenuItem_Clickâ€
Public ItemIndex AsInteger = -1, ItemText AsString = "New"

‘ “ListBox1_SelectedIndexChanged†responds to user clicking
‘ in the ListBox1 control. It shows then “ContextMenu1†control
‘ and waits for the response. It then displays (twice) the selected
‘ values in the public “ItemIndex†and “ItemName†variables that
‘ were set by the ContextMenu event handling routine “MenuItem_Clickâ€
PrivateSub ListBox1_SelectedIndexChanged( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles ListBox1.SelectedIndexChanged

Dim i AsInteger, pos AsNew Drawing.Point(40, 40)
ItemIndex = -1
ItemText = "Null"
ContextMenu1.Show(ListBox1, pos)
MsgBox("Msg 1: From Calling Routine : ItemIndex = " _
+ Str(ItemIndex) + ", ItemText = " + ItemText)
MsgBox("Msg 2: From Calling Routine : ItemIndex = " _
+ Str(ItemIndex) + ", ItemText = " + ItemText)
EndSub

‘ “MenuItem_Click†routine responds to the ContectMenu1 click and _
‘ sets the Public “ItemIndex†and “ItemText†variables
‘ to the selected MenuItem index and text propoerties
PrivateSub MenuItem_Click( _
ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MenuItem1.Click, MenuItem1.Click

Dim m As MenuItem
m = DirectCast(sender, MenuItem)
ItemIndex = m.Index
ItemText = m.Text
MsgBox("From Event Handler Routine : ItemIndex = " _
+ Str(ItemIndex) + ", ItemText = " + ItemText)
EndSub
 
M

Martin H.

Hello PB99,

try the MouseUp event. As I recall, the event order is
* MouseDown
* Click
* MouseUp

So from MouseUp on, the ListBox should have the correct ItemIndex.

Best regards,

Martin

P.S. Just for curiosity...does PB stand for Paderborn?
 
L

Lloyd Sheen

PB99 said:
The following code example show some perculiar behavior. I have a Form
with a
listbox with "ContextMenu1" assigned to it. When I click in the Listbox,
the
ContextMenu is displayed. After I make a menuItem selection in the
ContextMenu the event handing routine assigns the selected MenuItems
properties to the variables "ItemIndex" and "ItemName". Back in the
routine
where the ContextMenu was displayed I would expect these variables to be
updated after the "ContextMenu1.Show(ListBox1, pos)" statement, but the
1st
MsgBox statement displays the variable values that were in effect before
the
assignments in the event handling routine. Now here's the rub, Immediately
after this 1st MsgBox statement I execute another similar MsgBox statement
that then displays the correct updated values! It seem that I have some
sort
if asynchronous threading issue here. Anyone have any input on this?

Here's the code...

‘ “ItemIndex†and “ItemName†are used to record selected MenuItem
‘ propoerties in the Event Handling routine “MenuItem_Clickâ€
Public ItemIndex AsInteger = -1, ItemText AsString = "New"

‘ “ListBox1_SelectedIndexChanged†responds to user clicking
‘ in the ListBox1 control. It shows then “ContextMenu1†control
‘ and waits for the response. It then displays (twice) the selected
‘ values in the public “ItemIndex†and “ItemName†variables that
‘ were set by the ContextMenu event handling routine “MenuItem_Clickâ€
PrivateSub ListBox1_SelectedIndexChanged( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles ListBox1.SelectedIndexChanged

Dim i AsInteger, pos AsNew Drawing.Point(40, 40)
ItemIndex = -1
ItemText = "Null"
ContextMenu1.Show(ListBox1, pos)
MsgBox("Msg 1: From Calling Routine : ItemIndex = " _
+ Str(ItemIndex) + ", ItemText = " + ItemText)
MsgBox("Msg 2: From Calling Routine : ItemIndex = " _
+ Str(ItemIndex) + ", ItemText = " + ItemText)
EndSub

‘ “MenuItem_Click†routine responds to the ContectMenu1 click and _
‘ sets the Public “ItemIndex†and “ItemText†variables
‘ to the selected MenuItem index and text propoerties
PrivateSub MenuItem_Click( _
ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MenuItem1.Click, MenuItem1.Click

Dim m As MenuItem
m = DirectCast(sender, MenuItem)
ItemIndex = m.Index
ItemText = m.Text
MsgBox("From Event Handler Routine : ItemIndex = " _
+ Str(ItemIndex) + ", ItemText = " + ItemText)
EndSub

Sorry I am not close to my code but I am sure that I used the
SelectedIndices values to get indexes of selected items. If it is set to be
single select then you can just use the SelectedIndices(0) as the selected
ListViewItem and go from there.

Hope this helps.

LS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top