Solved

Refererencing the toolstripmenuitem in VB.NET

Posted on 2013-01-15
6
1,168 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
asp.net figure out error 3 47
Learning About The VB.NET TableLayoutPanel Control 5 42
Help with Report Viewer in VS2013 6 33
What is needed to become a DBA? 7 56
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
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 a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

751 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