1,105,409 Community Members

Selecting Drop Down Item from Menu Strip ITEM

Member Avatar
Rahul47
Posting Whiz
340 posts since Jan 2013
Reputation Points: 13 [?]
Q&As Helped to Solve: 20 [?]
Skill Endorsements: 2 [?]
 
-1
 

Hello people, please have a look at following IMAGE.
new

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.

Attachments
Member Avatar
G_Waddell
Practically a Posting Shark
821 posts since Nov 2009
Reputation Points: 131 [?]
Q&As Helped to Solve: 137 [?]
Skill Endorsements: 13 [?]
 
0
 

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
Member Avatar
Rahul47
Posting Whiz
340 posts since Jan 2013
Reputation Points: 13 [?]
Q&As Helped to Solve: 20 [?]
Skill Endorsements: 2 [?]
 
0
 

@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.

Member Avatar
tinstaafl
Postaholic
2,012 posts since Jun 2010
Reputation Points: 559 [?]
Q&As Helped to Solve: 402 [?]
Skill Endorsements: 35 [?]
 
0
 

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.

Member Avatar
G_Waddell
Practically a Posting Shark
821 posts since Nov 2009
Reputation Points: 131 [?]
Q&As Helped to Solve: 137 [?]
Skill Endorsements: 13 [?]
 
0
 

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.

Member Avatar
Rahul47
Posting Whiz
340 posts since Jan 2013
Reputation Points: 13 [?]
Q&As Helped to Solve: 20 [?]
Skill Endorsements: 2 [?]
 
0
 

Fellas am new in this field so please someone tell it briefly what exactly i have to do !

Member Avatar
tinstaafl
Postaholic
2,012 posts since Jun 2010
Reputation Points: 559 [?]
Q&As Helped to Solve: 402 [?]
Skill Endorsements: 35 [?]
 
0
 

What code are you using to add the new items to the menu?

Member Avatar
Rahul47
Posting Whiz
340 posts since Jan 2013
Reputation Points: 13 [?]
Q&As Helped to Solve: 20 [?]
Skill Endorsements: 2 [?]
 
0
 

@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.

Member Avatar
Rahul47
Posting Whiz
340 posts since Jan 2013
Reputation Points: 13 [?]
Q&As Helped to Solve: 20 [?]
Skill Endorsements: 2 [?]
 
0
 

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.
new1

Attachments
Member Avatar
tinstaafl
Postaholic
2,012 posts since Jun 2010
Reputation Points: 559 [?]
Q&As Helped to Solve: 402 [?]
Skill Endorsements: 35 [?]
 
0
 

Try this:

examno1 = DirectCast(sender, ToolStripMenuItem).Text
Member Avatar
Rahul47
Posting Whiz
340 posts since Jan 2013
Reputation Points: 13 [?]
Q&As Helped to Solve: 20 [?]
Skill Endorsements: 2 [?]
 
0
 

@tinstaafl: Upon using examno1 = DirectCast(sender, ToolStripMenuItem).Text following is result.

Member Avatar
Rahul47
Posting Whiz
340 posts since Jan 2013
Reputation Points: 13 [?]
Q&As Helped to Solve: 20 [?]
Skill Endorsements: 2 [?]
 
0
 

new12

Attachments
Member Avatar
tinstaafl
Postaholic
2,012 posts since Jun 2010
Reputation Points: 559 [?]
Q&As Helped to Solve: 402 [?]
Skill Endorsements: 35 [?]
 
1
 

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
Member Avatar
Rahul47
Posting Whiz
340 posts since Jan 2013
Reputation Points: 13 [?]
Q&As Helped to Solve: 20 [?]
Skill Endorsements: 2 [?]
 
0
 

@tinstaafl: Please check function. Guess there is sometng missing.

new2

Attachments
Member Avatar
Rahul47
Posting Whiz
340 posts since Jan 2013
Reputation Points: 13 [?]
Q&As Helped to Solve: 20 [?]
Skill Endorsements: 2 [?]
 
0
 

@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
Member Avatar
tinstaafl
Postaholic
2,012 posts since Jun 2010
Reputation Points: 559 [?]
Q&As Helped to Solve: 402 [?]
Skill Endorsements: 35 [?]
 
0
 

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
Member Avatar
G_Waddell
Practically a Posting Shark
821 posts since Nov 2009
Reputation Points: 131 [?]
Q&As Helped to Solve: 137 [?]
Skill Endorsements: 13 [?]
 
0
 

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.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article