Hello people, please have a look at following IMAGE.
What I Wish to achieve is extract the select item and display it into a messagebox or say do other actions.
I want to extract that particular selected ITEM ..say E101.
Thanks.
3
Contributors
16
Replies
3 Days
Discussion Span
4 Months Ago
Last Updated
18
Views
Related Article:Viewing Selected Department from Menu Strip
is a VB.NET discussion thread by akasekaihime that has 8 replies, was last updated 7 months ago and has been tagged with the keywords: view, menu, strip.
@G_Waddell:
Sorry guys I forgot to mention that the ITEMS E101,E100 are added dynamically.
So there will be no predefined Private Sub....End Sub Procedure for these Items.
You can still code the subroutine that you want to handle the click event, just without the Handles clause(use the subroutine from an established menuitem as a template). In the code that adds the menuitem, use the AddHandler statement to enable your subroutine to handle the click event of the new item.
As tinstaafl says as you dynamically build the list use the AddHandler to point the click event to a common subroutine based on the normal click event.
You can then use the sender parameter to identify which item fired the event.
E101,E100 are actually titles for exams that are created. So when an Exam is created its title E101,E100 etc are stored in database.
When I click Open Exam Menu in Menustrip it is supposed to show the Exams Created by a particular user. For that I am applying fllowing code:
Public Sub Open_Exam()
Dim con As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=C:\VB Applications\PROJECT_1\PROJECT_1\examdb.mdf;Integrated Security=True;User Instance=True")
Dim cmd As New SqlCommand("Select Exam_No,Exam_Title from Exam_Master Where Author='" + frmLogin_Register.txtlogin_username.Text + "'", con)
con.Open()
Dim dr As SqlDataReader = cmd.ExecuteReader()
While dr.Read()
If dr.HasRows() Then
Welcome.tsmOpen_Exam.DropDown.Items.Add(dr.GetString(0) + " (" + dr.GetString(1) + ") ")
End If
End While
dr.Close()
con.Close()
End Sub
Now what I expect is When I click on a particular item say E101, that examination should be opened in a form for editing purpose.
for demo purpose of selecting E101,E100 Iused following code.
Private Sub tsmOpen_Exam_DropDownItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles tsmOpen_Exam.DropDownItemClicked
examno1 = tsmOpen_Exam.Selected
MessageBox.Show(examno1)
End Sub
But it just shows the Messagebox as follows. I guess am missing out using some property of tsmOpen_Exam.......such as selectedItem or any other.
ok try this, there are probably other ways but I know for sure this works:
Public Sub Open_Exam()
Dim con As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=C:\VB Applications\PROJECT_1\PROJECT_1\examdb.mdf;Integrated Security=True;User Instance=True")
Dim cmd As New SqlCommand("Select Exam_No,Exam_Title from Exam_Master Where Author='" + frmLogin_Register.txtlogin_username.Text + "'", con)
con.Open()
Dim dr As SqlDataReader = cmd.ExecuteReader()
While dr.Read()
If dr.HasRows() Then
Dim TempItem As New ToolStripMenuItem
TempItem.Name = dr.GetString(0) + " (" + dr.GetString(1) + ") "
TempItem.Text = dr.GetString(0) + " (" + dr.GetString(1) + ") "
Welcome.tsmOpen_Exam.DropDown.Items.Add(TempItem)
AddHandler TempItem.Click, AddressOf ExamToolStripMenuItem_Click
End If
End While
dr.Close()
con.Close()
End Sub
Private Sub ExamToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
examno1=DirectCast(sender, ToolStripMenuItem).Text
MessageBox.Show(examno1)
End Sub
@tinstaafl: Please have a look at the following code. Dynamic Sub Menus have been successfully created but am not able to extract the name of Sub-Menu Item.
Any modification is welcomed. I Appreciate your help. It would not have been possible so far without you. Just one more step and this will work Out
Thanks
Public Sub Open_Exam()
Dim con As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=C:\VB Applications\PROJECT_1\PROJECT_1\examdb.mdf;Integrated Security=True;User Instance=True")
Dim cmd As New SqlCommand("Select Exam_No,Exam_Title from Exam_Master Where Author='" + frmLogin_Register.txtlogin_username.Text + "'", con)
con.Open()
Dim dr As SqlDataReader = cmd.ExecuteReader()
While dr.Read()
If dr.HasRows() Then
Dim TempItem As New ToolStripMenuItem
TempItem.Name = dr.GetString(0)
TempItem.Text = dr.GetString(0) + " (" + dr.GetString(1) + ") "
AddHandler TempItem.Click, AddressOf Message1
Welcome.tsmOpen_Exam.DropDown.Items.Add(TempItem)
End If
End While
dr.Close()
con.Close()
End Sub
Public Sub Message1(ByVal sender As System.Object, ByVal e As System.EventArgs)
GlobalVariables.examno1 = "Hello World!!"
MessageBox.Show(GlobalVariables.examno1)
End Sub
Private Sub ExamToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
GlobalVariables.examno1=DirectCast(sender, ToolStripMenuItem).Name
MessageBox.Show(GlobalVariables.examno1)
End Sub
Hi
Do you still get "&Open Exam" as the result? This looks to me like it is coming from the parent "Open Menu Item" rather than the item you are clicking. You may have to add the handler after you add the Item to the menu.