Click here to Skip to main content
12,762,829 members (36,589 online)
Click here to Skip to main content
Add your own
alternative version

Stats

6.8K views
3 bookmarked
Posted 2 Sep 2014

Custom combobox dropdown Close Problem

, 2 Sep 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
A simple solution to rolling up a Custom ComboBox list on mouse leave.

I've written a couple of custom WinForms controls with drop down lists before and ran across a problem of how to close the list when the mouse leaves the control area. I Googled a lot and read about global hooks and all kinds of kinky solutions but I think I've found a simple, maybe not elegant solution but it certainly works.

The custom ComboBox I've created here is comprised of a Button and a ListBox and the problem I was having was I could use the ListBox.OnMouseLeave event and rollup the ListBox but when I leave the button using the Button.OnMouseLeave, the ListBox would rolldown when I clicked on the button but rollup when I left the button even when in the ListBox area. The solution I've found tracks the mouse entering and leaving the Button and ListBox and when leaving the ListBox I rollup the control as I was doing but when I leave the button I set a timer and at the end of the tick if I'm not in the Button area or the ListBox area, I rollup the ListBox and stop the timer. If I am still in one of those areas, I reset the timer and do it again.

//==============================================================================================
// NOTE The listbox closes when the mouse leaves the listbox area but wasn't closing when
// the mouse left the button area and I couldn't figure how to do it until now.
// I solved the problem of the control not collapsing the listbox when leaving the button by
// setting a timer when the mouse leaves the button and at end of timer tick if the mouse is
// not in the button area and not in the listbox are then closed.
//==============================================================================================
private void btnDropDown_MouseEnter(object sender, EventArgs e)
{
     _inButton = true;
}        

private void btnDropDown_MouseLeave(object sender, EventArgs e)
{
     _inButton = false;
     if (_isExtended)
       timer1.Start();
}        

private void dataGridView1_MouseEnter(object sender, EventArgs e)
{
      _inList = true;
}        

private void dataGridView1_MouseLeave(object sender, EventArgs e)
{
       _inList = false;
       _inButton = false;
       HandleExtendState(CustomComboBoxExtendState.collapse);
       timer1.Stop();
}       

private void timer1_Tick(object sender, EventArgs e)
{
       if (!_inList)
       {
           if (!_inButton)
           {
               timer1.Stop();
               HandleExtendState(CustomComboBoxExtendState.collapse);
            }
            else
               timer1.Start();
        }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Mike Hankey
Retired
United States United States
I'm old and I have nothing more to say!

You may also be interested in...

Pro
Pro

Comments and Discussions

 
Questionthanks Pin
Ufuk Gündoğdu3-Sep-14 0:37
memberUfuk Gündoğdu3-Sep-14 0:37 
AnswerRe: thanks Pin
Mike Hankey3-Sep-14 0:47
professionalMike Hankey3-Sep-14 0:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170217.1 | Last Updated 2 Sep 2014
Article Copyright 2014 by Mike Hankey
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid