Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help constructing a custom combobox

Status
Not open for further replies.

Griffyn

Programmer
Jul 11, 2002
1,077
AU
Hi all,

I want to design a control that is an image drop-down combo. ie, exactly like a standard combobox, except showing images instead of text. I'm unsure how to do this.

I've been able to write controls that contain sub-controls (eg like the TLabelEdit control) and I started with this design with a TImage as the base control, and adding a TSpeedButton as the drop-down button, and a TDrawGrid as the list of images. But I've quickly gotten out of my depth.

Can anyone give me some guidance?

Thanks.
 
Hi.
I Think Griffyn want to do a Combobox, not a "Dropdown list-component".

The main difference between the Combo and the drop down, is the editable field...

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-"There is always another way to solve it, but I prefer my way.
 
Thanks for your thoughts guys.

I looked at the combobox option, or rather the TComboBoxEx component, because it has support already there for images, but neither allow the Edit box portion to be resized vertically. My images will be 32x32 pixels, and so won't fit in the Edit box. This is why I started with the TImage control and tried to add the dropdown functionality to it. Remember, I don't have any text to show here - just bitmap images.
 
So I went ahead and set it up as I described in my first post - I have a descendant of TImage, that in it's constructor sets up a TSpeedButton, and a TDrawGrid. All's working fine except:

[ul][li]The TDrawGrid is visible after my TImageSelector appears despite me setting the TDrawGrid.Visible property to False immediately after it was created in the TImageSelector.Create constructor.[/li]
[li]When clicking the TSpeedButton (which modifies the TDrawGrid.Visible property), the TDrawGrid appears underneath other components, instead of always on top.[/li][/ul]

I check for csDesigning in ComponentState when about to create the TDrawGrid, and avoid it so that it doesn't appear when designing a form.

I handle the CM_VISIBLECHANGED and CM_ENABLEDCHANGED messages in TImageSelector so that changes to those properties get replicated to the TSpeedButton and TDrawGrid. I handle the CM_LOSTFOCUS message so that the TDrawGrid is hidden when any other control is clicked.

But none of these three messages appear to get called - and I'm concerned this is because TImage descends via TGraphicControl, TControl rather than TWinControl. Not sure.

Any more thoughts anyone?
 
Ok, solved a few more problems by trial and error.

[ul][li]I made a descendant of TCustomDrawGrid, so that I could override messages and the like[/li]
[li]The TDrawGrid being visible when the form it's on is shown was solved by capturing the CM_VISIBLECHANGED message in it's descendant, and setting Visible to False if the TSpeedButton in it's owning TImage didn't have it's button down.[/li]
[li]The TDrawGrid appearing beneath other components on the form was easily solved in the same CM_VISIBLECHANGED message method and calling BringToFront.[/li][/ul]

I still need to find out why CM_LOSTFOCUS is not being called. WM_KILLFOCUS gets called when I set the TDrawGrid.Visible property to False, but not by me clicking another component on the form, which is very weird. When I show the TDrawGrid, I'm also calling it's SetFocus method, so by my thinking, it should definitely react to CM_LOSTFOCUS. More testing ahead, unless someone can point it out to me? :)
 
So close! My only problem left is that when I have clicked the dropdown, it won't close up when I click outside of the control. Clicking a TListBox, or a TStringGrid works fine, but not when I click a TTabControl. I presume this is because the TTabControl never gains the focus.

But a TComboBox does hide it's dropdown when you click a tab in a TTabControl, and I'm delving into it's source to try and find it. Something I found was a property called MouseCapture, but I don't know how to use it, or even if it will help. Anyone have any ideas on this final hurdle?
 
Done! Worked out how to use MouseCapture. When it's set to True, all mouse clicks regardless of where they occur, trigger the mouse events on the control. So I set TCustomDrawGrid.MouseCapture := True and then capturing the left mouse click and determined whether the mouse click was over the control or not. If not, then I hide the grid (ie cancel the dropdown).

Works great!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top