?
Solved

Refererencing the toolstripmenuitem in VB.NET

Posted on 2013-01-15
6
Medium Priority
?
1,202 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Stack Overflow Podcast - Developer Story

Welcome to the Stack Overflow podcast recorded Thursday July 20 at Stack Overflow Headquearters in NYC. Your hosts today are podcast regulars Jay Hanlon, David Fullerton, and Ilana Yitzhaki, plus the quite irregular Matt Sherman (Stack Overflow Engineering Manager extraordinaire)

Question has a verified solution.

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

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

719 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