Solved

Refererencing the toolstripmenuitem in VB.NET

Posted on 2013-01-15
6
1,123 Views
Last Modified: 2013-01-15
I have a VB form with a menu bar containing a drop down list of three states: Virginia, Florida, and California. Each state has a context menu of three cities. Below the menu bar are two text boxes, one to display the city (TextBox1) and one to display the state (TextBox2) that the user chooses. I created code for each menu item to populate the correct text box. For example, if you click Virginia, here is the code that will put Virginia in the state text box:
TextBox2.Text = VirginiaToolStripMenuItem.Text

My program works, but there are two problems.
1) If I add all 50 states and more cities, it would be impractical to write code for each city and state. How do I iterate through all menu items to populate TextBox1 and TextBox2 with the user's choices?

2) It's possible for a city in TextBox1 to be paired with an incorrect state in TextBox2. The reason is that you can click one of the cities  without clicking its state. For example, if you hover your mouse over Virginia, the Virginia cities show up automatically. You don't have to click Virginia to see Richmond, Norfolk, and Roanoke. So you can click Richmond and then click on a different state like Florida. How can I prevent the context menu of cities from automatically appearing when you hover your cursor over the state? I need to force the user to click a state in order to see its cities.
0
Comment
Question by:MBarongan
  • 3
  • 2
6 Comments
 

Author Comment

by:MBarongan
ID: 38778057
I uploaded the program to the EE Stuff File Upload page at http://www.ee-stuff.com/Expert/Upload/upload.php. The name of the zip file is MenuStrip.zip.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 38778083
You can have one event handler which is attached to all of your menu items. The sender parameter of the event handler is the control which generated the event. You can cast that parameter to a ToolStripMenuItem to get at its Text property.

e.g.

Public Sub TooStripMenuItem_OnClick(ByVal sender As Object, ByVal e As EventArgs) Handles VirginiaToolStripMenuItem.Click, FloridaToolStripMenuItem.Click, CaliforniaToolStripMenuItem.Click
    Dim ts As TooStripMenuItem = DirectCast(sender, TooStripMenuItem)

    TextBox2.Text = ts.Text
End Sub

Open in new window

0
 

Author Comment

by:MBarongan
ID: 38778324
That worked perfectly. So if I add more states, I have to add each state's click event to the Handles list ("name of state"ToolStripMenuItem.Click)? Also, how can I use this technique to add the city to TextBox1?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 38778362
So if I add more states, I have to add each state's click event to the Handles list
Yes.

how can I use this technique to add the city to TextBox1?
I'll have to take a look at your project to see how you have it set up.
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 38778718
I would wire them up dynamically at run-time in the Load() event of the form.  Then you can add/remove them without needing to change the code:
Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        For Each state As ToolStripMenuItem In ClickHereToChooseYourCityAndStateToolStripMenuItem.DropDownItems
            For Each city As ToolStripMenuItem In state.DropDownItems
                city.Tag = state.Text
                AddHandler city.Click, AddressOf city_Click
            Next
        Next
    End Sub

    Private Sub city_Click(sender As Object, e As System.EventArgs)
        Dim city As ToolStripMenuItem = CType(sender, ToolStripMenuItem)
        TextBox1.Text = city.Text
        TextBox2.Text = city.Tag
    End Sub

End Class

Open in new window

0
 

Author Closing Comment

by:MBarongan
ID: 38781614
Thank you. That's exactly what I needed. I added both a state_Click and a city_Click in case the user chooses only a state and not a city. This is the final code I ended up with (I changed the name ClickHereToChooseYourCityAndStateToolStripMenuItem to menubar).

 Public Class Form1

    Dim state As ToolStripMenuItem
    Dim city As ToolStripMenuItem

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles _ MyBase.Load
        For Each state In menubar.DropDownItems
            AddHandler  state.Click,  AddressOf  state_Click
            For Each  city  In  state.DropDownItems
                city.Tag = state.Text
                AddHandler city.Click,  AddressOf  city_Click
            Next
        Next
    End Sub

    Private Sub state_Click(sender As Object, e As System.EventArgs)
        state = CType(sender, ToolStripMenuItem)
        TextBox2.Text = state.Text
    End Sub

    Private Sub city_Click(sender As Object, e As System.EventArgs)
        city = CType(sender, ToolStripMenuItem)
        TextBox1.Text = city.Text
        TextBox2.Text = city.Tag
    End Sub

End Class
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now