We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,661 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Selecting Drop Down Item from Menu Strip ITEM

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 new.jpg 18.9KB
3
Contributors
16
Replies
3 Days
Discussion Span
4 Months Ago
Last Updated
18
Views
Rahul47
Junior Poster in Training
86 posts since Jan 2013
Reputation Points: 0
Solved Threads: 1
Skill Endorsements: 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
G_Waddell
Practically a Master Poster
623 posts since Nov 2009
Reputation Points: 107
Solved Threads: 95
Skill Endorsements: 6

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

Rahul47
Junior Poster in Training
86 posts since Jan 2013
Reputation Points: 0
Solved Threads: 1
Skill Endorsements: 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.

tinstaafl
Nearly a Posting Virtuoso
1,478 posts since Jun 2010
Reputation Points: 429
Solved Threads: 261
Skill Endorsements: 14

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.

G_Waddell
Practically a Master Poster
623 posts since Nov 2009
Reputation Points: 107
Solved Threads: 95
Skill Endorsements: 6

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

Rahul47
Junior Poster in Training
86 posts since Jan 2013
Reputation Points: 0
Solved Threads: 1
Skill Endorsements: 0

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

tinstaafl
Nearly a Posting Virtuoso
1,478 posts since Jun 2010
Reputation Points: 429
Solved Threads: 261
Skill Endorsements: 14

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

Rahul47
Junior Poster in Training
86 posts since Jan 2013
Reputation Points: 0
Solved Threads: 1
Skill Endorsements: 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 new.jpg 72.4KB
Rahul47
Junior Poster in Training
86 posts since Jan 2013
Reputation Points: 0
Solved Threads: 1
Skill Endorsements: 0

Try this:

examno1 = DirectCast(sender, ToolStripMenuItem).Text
tinstaafl
Nearly a Posting Virtuoso
1,478 posts since Jun 2010
Reputation Points: 429
Solved Threads: 261
Skill Endorsements: 14

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

Rahul47
Junior Poster in Training
86 posts since Jan 2013
Reputation Points: 0
Solved Threads: 1
Skill Endorsements: 0

new12

Attachments new1.jpg 30.05KB
Rahul47
Junior Poster in Training
86 posts since Jan 2013
Reputation Points: 0
Solved Threads: 1
Skill Endorsements: 0

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
Nearly a Posting Virtuoso
1,478 posts since Jun 2010
Reputation Points: 429
Solved Threads: 261
Skill Endorsements: 14

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

new2

Attachments new2.jpg 83KB
Rahul47
Junior Poster in Training
86 posts since Jan 2013
Reputation Points: 0
Solved Threads: 1
Skill Endorsements: 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
Rahul47
Junior Poster in Training
86 posts since Jan 2013
Reputation Points: 0
Solved Threads: 1
Skill Endorsements: 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
tinstaafl
Nearly a Posting Virtuoso
1,478 posts since Jun 2010
Reputation Points: 429
Solved Threads: 261
Skill Endorsements: 14

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.

G_Waddell
Practically a Master Poster
623 posts since Nov 2009
Reputation Points: 107
Solved Threads: 95
Skill Endorsements: 6

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.1104 seconds using 2.73MB