Maven4Champ
Technical User
- Jun 16, 2004
- 154
Hello everyone,
Here is the code I have which is used to dnyamically build a contextmenu and related items. My problem is that on the first right-click, nothing happens. On the second right-click, the menu finally appears but with the dynamic items repeated twice. The next right-click refreshes the menu and triples the items. It has to be something in my While or For Each where I am not clearing out the array properly or not terminating the loop control properly. Any assistance is GREALTLY appreciated!
Here is the code I have which is used to dnyamically build a contextmenu and related items. My problem is that on the first right-click, nothing happens. On the second right-click, the menu finally appears but with the dynamic items repeated twice. The next right-click refreshes the menu and triples the items. It has to be something in my While or For Each where I am not clearing out the array properly or not terminating the loop control properly. Any assistance is GREALTLY appreciated!
Code:
Private Sub NotifyIcon1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseClick
If Not e.Button = Windows.Forms.MouseButtons.Right Then Exit Sub
With My.Computer.Screen.WorkingArea
Call NotifyIcon1_Visible()
End With
End Sub
Private Sub NotifyIcon1_Visible() '(ByVal sender As System.Object, ByVal e As System.EventArgs) ' Handles NotifyIcon1.ButtonClick
Dim cms As ContextMenuStrip = Me.DynamicMenuStrip
Dim sMenu() As String
Dim Conn As SqlConnection = New SqlConnection(AppEnvLauncher.My.MySettings.Default.AppSiteConn)
Dim sQry As String
sQry = ""
sQry = "SELECT DISTINCT Env_Name, DisplayOrder FROM dbo.Instances WHERE (Display < '1') and env_name <> 'DSCPRD' ORDER BY DisplayOrder ASC;"
Dim dbCommand As New SqlCommand(sQry, Conn)
Conn.Open()
Dim sMenuRD As SqlDataReader = dbCommand.ExecuteReader()
Dim readerData As New System.Text.StringBuilder
ReDim Preserve sMenu(0)
Dim i As Integer
If sMenuRD.HasRows Then
i = 0
While sMenuRD.Read()
ReDim Preserve sMenu(i)
sMenu(i) = sMenuRD("Env_Name")
i = i + 1
End While
End If
For Each sMn As String In sMenu
cms.Items.Add(sMn, Nothing, New System.EventHandler(AddressOf SelectedChildMenu_OnClick))
Next
sMenuRD.Close()
If (Not sMenuRD Is Nothing) Then
sMenuRD.Close()
End If
Conn.Close()
End Sub