How To Make A Menu Using MenuStrip And MySQL Database In VbNet
In This VB.Net Tutorial We Will See How To Create A Menu Using MenuStrip And Add To This Menu Categories Using ToolStripMenuItem With Image And Text From MySQL Database Table And Add Also To This ToolStripMenuItem SubCategorys Elements From MySQL Database Using MySqlDataAdapter + DataTable + MemoryStream In Visual Basic.Net Programming Language And Visual Studio Editor.
Project Source Code:
Imports MySql.Data.MySqlClient
Imports System.IO
Public Class Create_Menu8from_MySQL
Dim connection As New MySqlConnection("datasource=localhost;port=3306;username=root;password=;database=s_t_d")
' add ToolStripMenuItem elements to menustrip using for loop
Private Sub Create_Menu8from_MySQL_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim adapter As New MySqlDataAdapter("SELECT * FROM `mypics`", connection)
Dim table As New DataTable()
adapter.Fill(table)
For i As Integer = 0 To table.Rows.Count - 1 Step +1
Dim img() As Byte
img = table(i)(3)
Dim ms As New MemoryStream(img)
Dim pic As Image = Image.FromStream(ms)
Dim tsm As New ToolStripMenuItem(table(i)(1), pic)
addItems(tsm, table(i)(0))
MenuStrip1.Items.Add(tsm)
Next
End Sub
' create a function to add subElements to ToolStripMenuItem
Sub addItems(ByVal tmenu As ToolStripMenuItem, ByVal id As String)
Dim adapter As New MySqlDataAdapter("SELECT `idMyPics`, `name`, `pic` FROM `pics2` WHERE `idMyPics` = " + id, connection)
Dim table As New DataTable()
adapter.Fill(table)
For i As Integer = 0 To table.Rows.Count - 1 Step +1
Dim img() As Byte
img = table(i)(2)
Dim ms As New MemoryStream(img)
Dim pic As Image = Image.FromStream(ms)
tmenu.DropDown.Items.Add(table(i)(1), pic)
Next
End Sub
End Class
///////////////OUTPUT: