none
How to cause menu item to be selected when program starts? RRS feed

  • Question

  • The program in question has a main menu on the form and the first time the program is run the user has to select the "settings" item on the menu to invoke a second form where he specifies some items necessary for the program to run.  The user then selects the "Run" menu item to start the programs function.  On subsequent executions of the program, I'd like the "Run" to happen automatically if the "Settings" has previously been run.  I know how to remember that "Settings" has been run and detect that at startup, but how can I programatically "press" the "Run" menu item?
    Monday, September 24, 2018 9:24 PM

All replies

  • As I see it simply call the underlying method for settings the first time then as done with settings call the run operation. This means the click event for both call a method to perform said operations as that is all that should be in their click events. Don't attempt to invoke the actual menu item, it doesn't make sense.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, September 24, 2018 10:28 PM
    Moderator
  • Do you have some reason for this awkward upsidedown operation?

    The program should startup running. The user should not have to select run.

    Why not check if the setting has been filled on startup (formshown or load) and if not then show the dialog until it has been filled and saved. There can still be a separate settings menu item.

    I dont see why you would do it as you describe?
     
    :)

    Tuesday, September 25, 2018 12:09 AM
  • If it seems to you that this is an "awkward upsidedown operation", it's partly because I decided that delving into the complexities of the program would detract from the simple question that I asked.  The program accesses four serial ports and has 7 threads that manage the interchange of data among the ports.  The actual COM port numbers used for any one execution could change for a number of reasons.  One of which is the fact that Windows will sometimes renumber serial ports due to hardware changes or for other unknown reasons.  Indiscriminate execution without the ability to specify the correct COM port numbers would make no sense.  So, the user is given the option to alter the COM port numbers to be used via the Setup menu item.  95% of the time, the Setup procedure would not be necessary and the program execution could simply continue.  At present, the user selects the Run menu item in those instances.  Being able to simply programatically initiate the Run item when no Setup is necessary would seem to be a simple approach.  Of course, there are many other ways it could have been done, but my question was: can a menu item be programatically executed?
    Tuesday, September 25, 2018 12:58 PM
  • Actually, it makes perfect sense.  I simplified the explanation of operation and that adds to the confusion.  As I mentioned in reply to another response, the program uses four COM ports who's numbers might change for a variety of reasons.  Normal operation would be to specify the COM port numbers the first time around and then hit Run.  On subsequent program starts, based on user observation of form indicators, the user would just hit Run without going through Setup.  In the event of an unsuccessful startup (e.g. device on COM port was not powered on, etc.), there's another menu item, Restart, which simply restarts the application.  That is the situation where it would be nice to programmatically select the Run item when the restart occurs.  Yes, I could modify the program so that the Run code is in a separate method which is called by the Run click routine or the form load code.

    You mention "unmark" for replies: I don't see an "unmark" option, only a "Mark as answer" option.  How does one unmark a reply or answer?

    Tuesday, September 25, 2018 1:26 PM
  • As mentioned in my first reply, do this.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, September 25, 2018 1:29 PM
    Moderator
  • If it seems to you that this is an "awkward upsidedown operation", it's partly because I decided that delving into the complexities of the program would detract from the simple question that I asked.  The program accesses four serial ports and has 7 threads that manage the interchange of data among the ports.  The actual COM port numbers used for any one execution could change for a number of reasons.  One of which is the fact that Windows will sometimes renumber serial ports due to hardware changes or for other unknown reasons.  Indiscriminate execution without the ability to specify the correct COM port numbers would make no sense.  So, the user is given the option to alter the COM port numbers to be used via the Setup menu item.  95% of the time, the Setup procedure would not be necessary and the program execution could simply continue.  At present, the user selects the Run menu item in those instances.  Being able to simply programatically initiate the Run item when no Setup is necessary would seem to be a simple approach.  Of course, there are many other ways it could have been done, but my question was: can a menu item be programatically executed?

    Sounds like double talk but what do I know?

    "can a menu item be programatically executed"

    You can call a menu by calling the menu event sub or a sub that does the function can be called by the menu and the rest of your code anytime.

    ie  if you dont need the args then just:

           dgvObjectMenu_Click(0, nothing )

    is that what you mean?

    Tuesday, September 25, 2018 1:44 PM
  •  I am really not sure what you are really trying to do.  I'm not sure if you just want the menu to open,  or if you want it to open and have a specific item selected/highlighted,  or if you want to programmatically execute the code that is in the menu item's Click event.

     If you want a specific menu item to open/drop down, then you can use the menu item's ShowDropDown method.  However,  if the item is actually a dropdown that is in another dropdown,  you must drop down the parent item and all other drop down items in it's family tree.

     For example,  the code below drops down my Options menu,  then drops down my Run menu.  Then it selects/highlights the 3rd item in the Run menu dropdown items.

    Imports System.IO.Ports
    
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            For Each spName As String In SerialPort.GetPortNames
                RunToolStripMenuItem.DropDownItems.Add(spName)
            Next
        End Sub
    
        Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
            MenuStrip1.Focus() 'set focus to the menu just in case TabIndex is not 0, first in the Tab order
            OptionsToolStripMenuItem.ShowDropDown() 'drops down the (Options) dropdown
            RunToolStripMenuItem.ShowDropDown() 'drops down the (Run) dropdown
            RunToolStripMenuItem.DropDownItems(2).Select() 'selects/highlights the 3rd item in the (Run) dropdown
        End Sub
    Private Sub RunToolStripMenuItem_DropDownItemClicked(sender As Object, e As ToolStripItemClickedEventArgs) Handles RunToolStripMenuItem.DropDownItemClicked MessageBox.Show("Run " & e.ClickedItem.Text) End Sub End Class

     

     When the form is started,  this is what it does....

     

     However,  if all you need to do is execute the Click event and not actually open/show the menu,  then you can use the ToolStripMenuItem.PerformClick method to simulate the user actually clicking a menu item.  For example, if I place this in the form Load event, after loading/adding all the menu items,  it will perform a click on the "COM3" menu item without displaying the menu item.

    RunToolStripMenuItem.DropDownItems(2).PerformClick()


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Tuesday, September 25, 2018 9:15 PM
    Tuesday, September 25, 2018 8:46 PM