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.
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.
Hi
I take it this is the standard Menu strip class? In which case, you are not actually selecting an item, you are clicking on it.
So what you would do is use the on click event of each item to populate the messagebox or carry out the other actions.
sub item1_Click (byval sender as object, byval e as system.eventargs) Handles item1.click
msgbox(item1.text)
end sub
@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.
Ahhh I did wonder...
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.
Fellas am new in this field so please someone tell it briefly what exactly i have to do !
What code are you using to add the new items to the menu?
@tinstaafl:
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.
Try this:
examno1 = DirectCast(sender, ToolStripMenuItem).Text
@tinstaafl: Upon using examno1 = DirectCast(sender, ToolStripMenuItem).Text following is result.
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 check function. Guess there is sometng missing.
@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
Just change the .Text to .Name
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.
Here is an easy way to do it :
Add the items you want normally:
Menu1.DropDownItems.Add(Some_Item, imageList1.Images[1], ToolStripMenuItemClicked);
And cast the 'sender' of your event procedure like this:
private void ToolStripMenuItemClicked(object sender, EventArgs e)
{
ToolStripDropDownItem Item = (ToolStripDropDownItem) sender;
MessageBox.Show(Item.Text);
}
`
2 things, this thread is in the Vb.net forum not the C# forum, and this thread is very old, try to keep your answers on more current threads and match your code to the forum. Thanks
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.