Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Refererencing the toolstripmenuitem in VB.NET

Posted on 2013-01-15
6
Medium Priority
?
1,218 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 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
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
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 2000 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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
Screencast - Getting to Know the Pipeline

971 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