PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2017 ToolStrip Menu Item From SQL-VBForums
Results 1 to 3 of 3

Thread: ToolStrip Menu Item From SQL

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2019

    ToolStrip Menu Item From SQL


    I am wanting to populate a menu tool strip/item from a SQL table.

    Basically the table has 2 columns Link_Name & Link_Address

    The menu has already been created in designmode and i would like a Tools section and the tool strip menu item to be my Link_Name and the click event to be
    launching a browser with website link from Link_Address.

    Currently i have:

    Dim Sqlcon As SqlConnection = New SqlConnection("Data Source=JAMESPC\SUPPORTDB;Initial Catalog=Support_DB;Persist Security Info=True;User ID=user;Password=password")
            Dim SQLcmd As SqlCommand
            Dim MenuPopulate As String = "Select * from Links "
            Dim DataAdapt As New SqlDataAdapter
            SQLcmd = New SqlCommand(MenuPopulate, Sqlcon)
            Dim ds As New DataSet
            Dim Menu As New ToolStripMenuItem
            If ds.Tables(0).Rows.Count = 0 Then
                Dim i As Integer
                For i As
            End If
    I am struggling on how best to write this i am aware i can fire a event handler for the ToolStripMenuItem_Click but not sure how it would talk to the SQL DB as its a seperate Private Sub.

    Thanks James

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Sydney, Australia

    Re: ToolStrip Menu Item From SQL

    You wouldn't use a data adapter and a DataTable for a start. This is a job for a data reader. Code:
    1. Private Sub ToolsToolStripMenuItem_DropDownOpening(sender As Object, e As EventArgs) Handles ToolsToolStripMenuItem.DropDownOpening
    2.     Dim menuItems As New List(Of ToolStripMenuItem)
    3.     Dim onClick = New EventHandler(Sub(clickSender, clickE)
    4.                                        Dim menuItem = DirectCast(clickSender, ToolStripMenuItem)
    6.                                        Process.Start(CStr(menuItem.Tag))
    7.                                    End Sub)
    9.     Using connection As New SqlConnection("connection string here"),
    10.         command As New SqlCommand("SELECT Link_Name, Link_Address FROM Links", connection)
    11.         connection.Open()
    13.         Using reader = command.ExecuteReader()
    14.             While reader.Read()
    15.                 menuItems.Add(New ToolStripMenuItem(reader.GetString(reader.GetOrdinal("Link_Name")),
    16.                                                     Nothing,
    17.                                                     onClick) With {.Tag = reader.GetString(reader.GetOrdinal("Link_Address"))})
    18.             End While
    19.         End Using
    20.     End Using
    22.     ToolsToolStripMenuItem.DropDownItems.Clear()
    23.     ToolsToolStripMenuItem.DropDownItems.AddRange(menuItems.ToArray())
    24. End Sub
    I've done that on the DropDownOpening event of the parent menu item so that the list is current every time. If that's not necessary then you can just do it once, on another event.

    The URL that corresponds to the name is stored in the Tag of the menu item. When the menu item is clicked, the event handler gets the address from the Tag and passes it to Process.Start, which will open that URL in the default browser.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    May 2019

    Re: ToolStrip Menu Item From SQL

    Thanks jmcilhinney, that was what i was looking for.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts


Click Here to Expand Forum to Full Width