Solved

Refererencing the toolstripmenuitem in VB.NET

Posted on 2013-01-15
6
1,184 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 75

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 75

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 86

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

626 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