Search code examples
winformsuser-interfacesubmenumenustrip

Delaying opening of sub-menus in a menustrip


Our environment: Visual Studio 2010, c#, .net 4 client profile.

We have a Winforms application that contains a menustrip in its main form. The items of the menustrip contain both an image (64x64) and text. The main form also has a TabControl which contains 5 tabs. In the OnLoad() method of the main form, we hide the TabControl headers so that they are not visible and therefore not clickable. Instead, when the user clicks on an item in the menustrip, we switch the active tab.

However, our menus have many sub-menu items, and since we use the main menustrip to select the active tab, we would like the sub-menu items to appear only after the user clicks the menu item for a period of time, not instantaneously. Otherwise, whenever the user changes his/her active view (by selecting a tabPage), the sub-menus appear on the screen since he/she clicked a menustrip item that contains sub menus.

Is this possible?


Solution

  • I don't completely understand the rationale, but you can delay the display of a submenu using the MouseDown handler and sleep function, like this:

    Private Sub FileToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FileToolStripMenuItem.MouseDown
    System.Threading.Thread.Sleep(2000) ' wait two seconds
    End Sub
    

    ======================

    (Edit: Added second solution)

    You can do this with a timer control and ShowDropDown/HideDropDown:

    Private Sub FileToolStripMenuItem_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles FileToolStripMenuItem.MouseDown
    ' show tab here'
    FileToolStripMenuItem.HideDropDown()
    Timer1.Interval = 500
    Timer1.Start()
    End Sub
    
    Private Sub FileToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FileToolStripMenuItem.Click
    FileToolStripMenuItem.HideDropDown()
    End Sub
    
    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    Timer1.Stop()
    FileToolStripMenuItem.ShowDropDown()
    End Sub