locked
How do I give a drop-down menu an event listener? RRS feed

  • Question

  • I am using Visual Studio 2010 Express, and I created a drop-down menu. How do I give it functionality, aka an event for each choice selected? My goal is to run a vb script depending on the choice.
    Thursday, January 9, 2014 7:50 PM

Answers

  • Hi tyler,

     I think i might understand what you mean now. You want to be able to  tell if an option has been enabled or disabled on the actual menu item. You could set the CheckOnClick property of the menu item to True. That will show a check mark on the menu item if they have Disabled or Enabled that option. Here is an example

     I added an item to the a menustrip and named it Option1_ToolStripMenuItem

    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Option1_ToolStripMenuItem.CheckOnClick = True
        End Sub
    
        Private Sub Option1_ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Option1_ToolStripMenuItem.Click
            If Option1_ToolStripMenuItem.Checked Then
                MessageBox.Show("Run script to enable option 1")
            Else
                MessageBox.Show("Run script to disable option 1")
            End If
        End Sub
    
    End Class
    

     I set the CheckOnClick = True in the Form Load event but, you can set it in the MenuItem`s properties on the Design window if you want.   :)

    • Marked as answer by Carl Cai Monday, January 20, 2014 5:38 AM
    Saturday, January 11, 2014 12:54 PM

All replies

  • Hi,

    I have moved this thread to Visual Basic forum for better response.

    Best regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, January 10, 2014 5:28 AM
  • Assuming you are working in the designer, you can select the menu item, go to the properties window, and in the events list for the menu item, set the Click event.

    As an alternative, you can just double-click on the menu item in the designer and it will generate the event handler and take you directly to the code view.


    - Brady My posts are kept as simple as possible for easier understanding. In many cases you can probably optimize or spruce up what I present. Have fun coding!

    Friday, January 10, 2014 6:18 AM
  • I am using Visual Studio 2010 Express, and I created a drop-down menu. How do I give it functionality, aka an event for each choice selected? My goal is to run a vb script depending on the choice.
    Can you elaborate this a little bit more in particular. What do you mean with VB script?

    Success
    Cor

    Friday, January 10, 2014 9:43 AM
  • I am using Visual Studio 2010 Express, and I created a drop-down menu. How do I give it functionality, aka an event for each choice selected? My goal is to run a vb script depending on the choice.

    Can you elaborate this a little bit more in particular. What do you mean with VB script?

    Success
    Cor

    I had already written VB Scripts before I started using Express. When you choose a drop-down menu choice, it'll run the respective VB script.

    Hi,

    I have moved this thread to Visual Basic forum for better response.

    Best regards,


    Thanks.

    Friday, January 10, 2014 5:12 PM
  • Hi,

     If you added a menu to your Form from the ToolBox and added some items in it like i added (Click Me) and (Something Else) as items just for this example. You can double click the item and it will automatically make the code to handle the item being clicked. It will bring you rite to the code where you put whatever you want to happen when the item is clicked by the user.

    If i double click here on (Click Me)

    It will create the code like below and bring you rite to it.

        Private Sub ClickMeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClickMeToolStripMenuItem.Click
    
        End Sub

    Now type the code in for whatever you want to happen if the item is clicked by the user.

    If you created the menu and its items in code instead then you can use the AddHandler Statement to add a handler to the menu items.

    EDIT : I should also add that if your adding the items in the code then depending on how your creating the Items you may be able to use the WithEvents when you create them. They would have to be Class scoped to do it though. You can check out the link below to see examples for this.

    Dim WithEvents Item1 As New MenuItem

    WithEvents and the Handles Clause

    • Edited by IronRazerz Friday, January 10, 2014 6:56 PM
    Friday, January 10, 2014 6:14 PM
  • Well, I'm wondering how I control the events for each selection I make from within Design view. For instance, I have a drop down menu that has Enabled and Disabled. How would I set an action for each when clicked?
    Saturday, January 11, 2014 4:29 AM
  • Well, I'm wondering how I control the events for each selection I make from within Design view. For instance, I have a drop down menu that has Enabled and Disabled. How would I set an action for each when clicked?

    Hi,

     A disabled item will not raise an event. You would have to try doing some sort of long work around to try doing that and i am not sure rite off hand without a bit of playing around how you would even do that. Of coarse, maybe i am just not understanding what your trying to do. Perhaps someone else will understand and be able to answer this.   :)

    Saturday, January 11, 2014 4:48 AM
  • Edit: I'll start a  new question.

    I didn't mean like literally disabled. Disabled has nothing to do with disabling the drop down menu or controls. Here's a better view of what I mean...

    I have two options: Enabled and Disabled. Each option runs a VB Script. One VB Script enables something, the other disables what the Enable option enabled. Again, I simply want to know how to create an event for each selection (Similar to and If Else statement). So far I can only see how to give an event when the index changes (or a change occurs).

    I guess I'm confused on how it "knows" or distinguishes the difference between the first selection and the second. You know with If Else statements, you give it a condition such as...

    If x=1 Then
    Wscript.Echo "Run the enable VB Script file."
    
    Else
    Wscript.Echo "Run the disable VB Script file."
    End If

    The same would go for this drop down menu. How does it function according to this logic? But of course in terms of Designer not so much direct coding.





    Saturday, January 11, 2014 5:18 AM
  • Each MenuItem that you double click on in the control will create it's own event handler. So say you double click the "Enabled" item, you will be shown something similar to the following.

        Private Sub EnabledToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles EnabledToolStripMenuItem.Click
    
        End Sub

    Then for the "Disabled" item,

        Private Sub DisabledToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles DisabledToolStripMenuItem.Click
    
        End Sub

    Then, after the Private Sub .... but, before the End Sub add your code to execute the script files. Something similar to

    System.Diagnostics.Process.Start("C:\vbscriptfile.vbs")

    Saturday, January 11, 2014 9:47 AM
  • Hi tyler,

     I think i might understand what you mean now. You want to be able to  tell if an option has been enabled or disabled on the actual menu item. You could set the CheckOnClick property of the menu item to True. That will show a check mark on the menu item if they have Disabled or Enabled that option. Here is an example

     I added an item to the a menustrip and named it Option1_ToolStripMenuItem

    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Option1_ToolStripMenuItem.CheckOnClick = True
        End Sub
    
        Private Sub Option1_ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Option1_ToolStripMenuItem.Click
            If Option1_ToolStripMenuItem.Checked Then
                MessageBox.Show("Run script to enable option 1")
            Else
                MessageBox.Show("Run script to disable option 1")
            End If
        End Sub
    
    End Class
    

     I set the CheckOnClick = True in the Form Load event but, you can set it in the MenuItem`s properties on the Design window if you want.   :)

    • Marked as answer by Carl Cai Monday, January 20, 2014 5:38 AM
    Saturday, January 11, 2014 12:54 PM