Double Click event on Combo Box

jelo

Member
Joined
Jan 23, 2006
Messages
23
Programming Experience
Beginner
Hello

I am using .net framework 1.07 and I am trying to raise a double click event on the Combo Box on a Windows Form and it is not working.

Please advice me on this

Thanks
 
Double click a combobox, why?

With a combobox you have effectively made your selection when you pull down the list and select an item, why would you want the user to have to double click on the item he/she just selected?

Will the selected index change method work?

VB.NET:
    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
      [COLOR=Green] ' Do whatever you would do on a doubleclick here.[/COLOR]
    End Sub
 
Thanks for ur reply.

I need double click event because on double click of a combo box I want to change its datasource and fill it with the appropriate records.

can u help me put with this
 
How advanced are you? With a combobox, in effect, you have three components, the drop down menu, the button to drop it down, and the text area. Double click is probably not handled because of the three components, (which one fires the double click). It will probably require writing your own component class or sub class this component to handle a double click.

You may want to research overriding the WndProc method.

Easy solution, place a button right beside the combobox and use your doubleclick code on the buttons click event.

Sorry I can't be more help.
 
or better yet, add another combobox to the form, this one will hold the "datasources" in the selectedindexchanged event, you can change the datasource of the combobox you want to double click
 
To make a double click event do this.....

First build a property called CboxClick....
In this property when assigned start a new thread if it equals one. This thread should simply sleep a split second then reset the property to 0.

Then open the mousedown event sub

Sub CombBox_OnMouseDown .....

Determine which mouse button was pressed.

If e.buttons = Mouse.buttons.left then....

Count the click....

cBoxClick += 1

Check to see if it's the second time we clicked it...

If cBoxClick is = 2 then .....run the doubeclick routine you want....


I have used this to make my own triple click events for my forms and it works flawlessly. If you don't understand what I am saying then e-mail me and I will happily walk you through it. If you just want the code I can send you a working snippet to have but I would rather teach you. You need to know how to write and use Property's and how to multi thread. The reason is we want an indepent thread to be sleeping getting ready to reset the property and at the same time allowing the user to click again before it awakes.
 
Last edited:
I like the solution,
Where are you setting up the sleep so the counter resets?
How are you using Sleep? Is outside the MouseDown event?
Little Help? Little code?

Thanks for the post...
 
Here is the code to make catch a double click. Remember, the combo automatically selects whichever one you pick on click but you can double click after on the combobox and use what was selected in a different way now. Basically in this code you select an item then double click the box to use that item in the double click event. You know it works b/c the msgbox shows the selected item. If you want to override the single click event all together and simply replace it with a double click then that will be a lil different but you will still need to make the double click yourself. Try this code and see what you think. Hope this helps... please let me know.

NOTE: Imports System.Threading


Private m_cBoxClick AsInteger 'this is declared private and is used only to hold info for the property. It is not recommended_
to use this variable anywhere else. In practive I use m_ followed by the same name_
as the property it relates to, to keep track of them and where they go.
Property cBoxClick() AsInteger
Get
Return m_cBoxClick
EndGet
Set(ByVal Value AsInteger)
m_cBoxClick = Value
If m_cBoxClick = 1 Then 'only start the reset thread on the first click or it will lag over for every click
Dim t AsNew Thread(AddressOf DBL_CLICK) 'DBL_CLICK is the private routine at the bottom that sleeps then resets
t.Start()
EndIf
EndSet
EndProperty
PrivateSub ComboBox1_MouseDown(ByVal sender AsObject, ByVal e As System.Windows.Forms.MouseEventArgs) Handles_ ComboBox1.MouseDown
cBoxClick += 1 'this counts the clicks
If e.Button = MouseButtons.Left AndAlso cBoxClick = 2 Then
MsgBox(Me.ComboBox1.SelectedItem) 'this is where we put the code to run when double clicked i used msgbox to prove
EndIf
EndSub
PrivateSub DBL_CLICK() 'this is the sleeper thread that starts when our property is = to 1 remember this will sleep_
even while the property is changing then reset it to 0.
Thread.Sleep(250)
cBoxClick = 0
EndSub



You may also considering sync locking the property since theoretically the user could be clicking at the exact moment the DBL_CLICK is resetting it. I never had any problems with it before but it's a thought. I may start doing that myself from now on too.
 
Last edited:
Well, my last post was a big waste. The combo box already has it's own double click event that does exactly that. It's still helpful to you if you want to learn how to make a doubleclick event. I guess I'm not sure what you really want the combo box to do. Therefore am researching a way right now to doubleclick it with it down just for giggles. To me this is pointless b/c what's the difference in doubleclicking or just clicking once the combobox is down? Anyways, i'll see what I can do.
 
Back
Top