none
Close toolbar dropdownlist when mouse leaves RRS feed

  • Question

  • Hi

    I have a toolbar with a drop downlist.

     

    When the mouse hovers over the menu the list drops down.

     

    I would like to get it so when the mouse is no longer over the menu or items in the drop down list the list closes.

     

    Using mouse leave on the menu item works only for the menu item not the items in the list, in other words as soon as I leave the menu item it closes.

     

    It would be nice if it would only close after it has left both the menu item and the drop down list.

     

    Mike

    Tuesday, September 2, 2008 8:52 PM

Answers

  • Mike,

     

    You have really thrown me for a loop, because you say that you are using a toolstrip, and that you have a dropdownlist on the toolstrip.  I know of no dropdownlist control for the toolstrip.  The only controls that could provide a "dropdownlist" type functionality would be either a DropDownButton control or a SplitButton control.  Neither of which has a Close() method.  SplitButton has a HideDropDown() and ShowDropDown() method, so I would assume that you are referring to that, correct?

     

    If so, this can be done, however it would be EXTREMELY cumbersome to do.  The reason being, is that the items on the toolstrip are in no way connected to the toolstrip.  In a ContextMenuStrip, if you call Close(), it closes everything.  That's not the case with a ToolStrip.  I will give you a start, however you will have to do some of the grunt work.  First off, handle the toolStrips MouseLeave event:

     

    private void toolStrip1_MouseLeave(object sender, EventArgs e)

    {

    foreach(ToolStripItem tsi in toolStrip1.Items)

    {

    if (tsi.Name.Contains("Split")) // cycle through the items on the ToolStrip, and look for the                                             //ToolStripSplitButtons

    {

    if (tsi.Pressed) // check for if the "DropDown" is open

    {

    keepOpen(tsi);

    ToolStripSplitButton aButton = (ToolStripSplitButton)tsi;

    aButton.HideDropDown();

    break;

    }

    }

    }

    }

     

    Then you must create a procedure called keepOpen:

     

    private void keepOpen(ToolStripItem tsi)

    {

    bool condition1; //set the condition equal to this condition => MousePosition.X >= x coordinate of the SplitBtn

    bool condition2; //set the condition equal to this condition => MousePosition.X <= x coordinate of the SplitBtn + the SplitBtns width

    bool condition3; //set the condition equal to this condition => MousePosition.Y >= y coordinate of the SplitBtn

    bool condition4; //set the condition equal to this condition => MousePosition.Y <= y coordinate of the SplitBtn + the SplitBtns height

     

    while((condition1 && condition2) && (condition3 && condition4))

    {

    condition1; // set the condition equal to this condition => MousePosition.X >= x coordinate of the SplitBtn

    condition2; //set the condition equal to this condition => MousePosition.X <= x coordinate of the SplitBtn + the SplitBtns width

    condition3; //set the condition equal to this condition => MousePosition.Y >= y coordinate of the SplitBtn

    condition4; //set the condition equal to this condition => MousePosition.Y <= y coordinate of the SplitBtn + the SplitBtns height

    }

    }

     

    The purpose of this procedure, is to check to make sure that the mouse is still within the bounds of the ToolStripSplitButton's window.  While it is, it just keeps running.  When the mouse moves outside of these bounds, the procedure ends, and the HideDropDown() method fires.  I'll leave it up to you to figure out the code for what the bounds of the ToolStripSplitButton are, and for marking this as an answer .

     

    Bob

    Friday, September 12, 2008 12:26 AM

All replies

  • Hi,

     

    I've had a similar  problem with a combobox. The dropdowm part is actually a different window control. That means it has its own mouseEnter en mouseLeave events. To accomplish the effect you describe you must program a 2-way communication between the box part and the dropdown part. To do so, I had to inherit from Combobox and show a custom dropdown window (which is actually nothing more than a listbox). You could do something similar.

     

    regards,

    Mark

    Wednesday, September 3, 2008 10:38 AM
  • I'm relatively new to the world of programming.

    I beleive I understand what your saying however some sample could would definitely help.

     

    Thanx

     

    Mike44P

     

    Wednesday, September 3, 2008 3:41 PM
  • I would assume that you are using a MenuStrip.  That said, you need to handle the MenuStrip's MouseLeave event, and not the Menu item's mouse leave event.  You handle this by clicking on the menuStrip1 icon in designer for the form.  You would then add code similiar to the following:

     

    private void menuStrip1_MouseLeave(object sender, EventArgs e)

    {

    menuStrip1.Close();

    }

     

    Bob
    Thursday, September 11, 2008 4:13 AM
  • Hi Bob

    Unfortunately I'm using a toolstrip, prefer the look and functionality.

    I do thank you and will keep it in mind for future forms.

    I did try mouse leave on the individual items and the dropdown icon they work independantly.

    I think I'll need to wrtie something that will consider the icon and all of the items before closing the drop down.

     

    Mike44P

     

    Thursday, September 11, 2008 6:14 PM
  • Mike,

     

    You have really thrown me for a loop, because you say that you are using a toolstrip, and that you have a dropdownlist on the toolstrip.  I know of no dropdownlist control for the toolstrip.  The only controls that could provide a "dropdownlist" type functionality would be either a DropDownButton control or a SplitButton control.  Neither of which has a Close() method.  SplitButton has a HideDropDown() and ShowDropDown() method, so I would assume that you are referring to that, correct?

     

    If so, this can be done, however it would be EXTREMELY cumbersome to do.  The reason being, is that the items on the toolstrip are in no way connected to the toolstrip.  In a ContextMenuStrip, if you call Close(), it closes everything.  That's not the case with a ToolStrip.  I will give you a start, however you will have to do some of the grunt work.  First off, handle the toolStrips MouseLeave event:

     

    private void toolStrip1_MouseLeave(object sender, EventArgs e)

    {

    foreach(ToolStripItem tsi in toolStrip1.Items)

    {

    if (tsi.Name.Contains("Split")) // cycle through the items on the ToolStrip, and look for the                                             //ToolStripSplitButtons

    {

    if (tsi.Pressed) // check for if the "DropDown" is open

    {

    keepOpen(tsi);

    ToolStripSplitButton aButton = (ToolStripSplitButton)tsi;

    aButton.HideDropDown();

    break;

    }

    }

    }

    }

     

    Then you must create a procedure called keepOpen:

     

    private void keepOpen(ToolStripItem tsi)

    {

    bool condition1; //set the condition equal to this condition => MousePosition.X >= x coordinate of the SplitBtn

    bool condition2; //set the condition equal to this condition => MousePosition.X <= x coordinate of the SplitBtn + the SplitBtns width

    bool condition3; //set the condition equal to this condition => MousePosition.Y >= y coordinate of the SplitBtn

    bool condition4; //set the condition equal to this condition => MousePosition.Y <= y coordinate of the SplitBtn + the SplitBtns height

     

    while((condition1 && condition2) && (condition3 && condition4))

    {

    condition1; // set the condition equal to this condition => MousePosition.X >= x coordinate of the SplitBtn

    condition2; //set the condition equal to this condition => MousePosition.X <= x coordinate of the SplitBtn + the SplitBtns width

    condition3; //set the condition equal to this condition => MousePosition.Y >= y coordinate of the SplitBtn

    condition4; //set the condition equal to this condition => MousePosition.Y <= y coordinate of the SplitBtn + the SplitBtns height

    }

    }

     

    The purpose of this procedure, is to check to make sure that the mouse is still within the bounds of the ToolStripSplitButton's window.  While it is, it just keeps running.  When the mouse moves outside of these bounds, the procedure ends, and the HideDropDown() method fires.  I'll leave it up to you to figure out the code for what the bounds of the ToolStripSplitButton are, and for marking this as an answer .

     

    Bob

    Friday, September 12, 2008 12:26 AM