PB99 0 Newbie Poster

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 As Integer = -1, ItemText As String = "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”
Private Sub ListBox1_SelectedIndexChanged( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles ListBox1.SelectedIndexChanged

Dim i As Integer, pos As New 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)
End Sub

‘ “MenuItem_Click” routine responds to the ContectMenu1 click and _
‘ sets the Public “ItemIndex” and “ItemText” variables
‘ to the selected MenuItem index and text propoerties
Private Sub 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)
End Sub