Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do I add a button control in the combobox item?
Posted
Comments
Sergey Alexandrovich Kryukov 24-Nov-14 2:29am    
I'm not sure that this is what OP wants (unless the question is incorrectly formulated). "Add a button" suggests that a button should become one of the list elements (otherwise it wouldn't be "adding"). If this is so, please see my question.
—SA
Javafree 24-Nov-14 2:39am    
thanks!
Sergey Alexandrovich Kryukov 24-Nov-14 3:17am    
You are very welcome.
Good luck, call again.
—SA
Member 11364646 24-Feb-15 7:41am    
yes this the question please provide soln.

As you are asking about v.2.0. "ComboBox" probably means System.Windows.Forms.ComboBox. No, this control is not designed to hold other controls. The items of its list box part can be of any type, but presented in UI is just the string obtained as ToString. Therefore, even if you add button instance to the combo box, it won't be shown as a button, so you cannot do what you want.

I don't think what you want to achieve is a good design idea. However, you can achieve such look and behavior if you create a custom control from scratch. One of the possibility is this: your "list box-like" part of your control could be derived from ScrollableControl; this way, you could easier implement the scrolling of the elements of the "list box".

—SA
 
Share this answer
 
Now that I know that what you want is a graphic element inside a ComboBox Item: you can sub-class the ComboBox and use custom painting (GDI+) to draw an image as illustrated here: [^]

Hit detection on the graphic element alone would require some work.

Unless you are quite advanced with WinForms and creating sub-classed Controls from standard MS provided Controls, I wouldn't recommend this.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 24-Nov-14 3:16am    
There are some big problems with this approach. In fact, if you look at the many customized ComboBox-line controls in different libraries, you will see the same approach: people give up sticking features in existing combo box and instead compose a new control out of a text box and a scrolling listbox-like area. That's why I suggested what I suggested in Solution 1.

What are the problems? First of all, it's not easy to create all Button behavior in a custom draw. Remember that you need to provide visual feedback on mouse hover, on depressing and releasing the button (which also happens with the mouse capture). How? You would need to invalidate the whole combo box on all these events. There are many other problems. That's why people usually don't sub-class this particular control (based on Windows API). At the same time, composing of a similar control out of other controls is quite feasible and is often used.

—SA
BillWoodruff 24-Nov-14 7:52am    
If you really read my response it would clear to you that I do not recommend sub-classing the ComboBox.

But, if you wish to exercise your fingers sermonizing to yourself, be my guest :)
Sergey Alexandrovich Kryukov 24-Nov-14 10:39am    
How should I understand this: "you can sub-class the ComboBox"? As this is the only approach you describe, your "wouldn't recommend this" (only based on insufficient experience, not anything objective) doesn't change anything. It made a legitimate answer, but still leaving a room for my comment. After all, I added my comment to explain what is the right choice. Can you see my point?
—SA
Javafree 25-Nov-14 1:29am    
ok,thanks.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900