Hey PRA,
Please refer below smaple.
Use dropdownItemClicked Event.
Namespaces
C#
using System.Data.SqlClient;
VB.Net
Imports System.Data.SqlClient
Code
C#
private void mainMenuToolStripMenuItem_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
string submenu = e.ClickedItem.Text;
string constr = @"Server=.\SQL2005;DataBase=Test;UID=sa;PWD=pass@123";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT Title FROM Menus WHERE Title=@Title", con))
{
cmd.Parameters.AddWithValue("@Title", submenu);
con.Open();
string menuItemName = Convert.ToString(cmd.ExecuteScalar());
if (menuItemName == submenu)
{
Form2 f2 = new Form2();
f2.ShowDialog();
}
else
{
MessageBox.Show("You haven't access to this form!");
}
con.Close();
}
}
}
VB.Net
Private Sub MainMenuToolStripMenuItem_DropDownItemClicked_1(sender As System.Object, e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles MainMenuToolStripMenuItem.DropDownItemClicked
Dim submenu As String = e.ClickedItem.Text
Dim constr As String = "Server=.\SQL2005;DataBase=Test;UID=sa;PWD=pass@123"
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("SELECT Title FROM Menus WHERE Title=@Title", con)
cmd.Parameters.AddWithValue("@Title", submenu)
con.Open()
Dim menuItemName As String = Convert.ToString(cmd.ExecuteScalar())
If menuItemName = submenu Then
Dim f2 As Form2 = New Form2()
f2.ShowDialog()
Else
MessageBox.Show("You haven't access to this form!")
End If
con.Close()
End Using
End Using
End Sub
Screenshot