Recent

Author Topic: ComboBox and ComboBoxEx  (Read 707 times)

valter.home

  • Jr. Member
  • **
  • Posts: 75
ComboBox and ComboBoxEx
« on: May 04, 2021, 12:20:15 am »
I worked on it for a couple of hours but couldn't find a way out.
I have a TComboBox set to Style=csOwnerDrawFixed but I need that when I select an item it must not remain selected.
The Autoselect property set to false doesn't seem to do its job.
All the styles that foresee the readonly remain selected after clicking on an item.
I tried to deselect the text in the OnSelect and OnChange events but without success.
Only with TComboBoxEx do I get the desired result but I need to create several TComboBox at runtime and TComboBoxEx offers several additional properties that I don't really need.
Do you know if there is a solution to be able to use the classic TComboBox?

jamie

  • Hero Member
  • *****
  • Posts: 4742
Re: ComboBox and ComboBoxEx
« Reply #1 on: May 05, 2021, 12:42:35 am »
try this inside the OnDrawItem

Set you combobox to a OwnwerDrawFixed and implement this onDrawItem

Code: Pascal  [Select][+][-]
  1. procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer;
  2.   ARect: TRect; State: TOwnerDrawState);
  3. begin
  4.   with TCombobox(Control) do
  5.     Begin
  6.       Canvas.Brush.color := Brush.color;
  7.       Canvas.FillRect(AREct);
  8.       Canvas.Font.Color := font.color;
  9.       If Index <> -1 then
  10.       Canvas.TextOut(Arect.left, Arect.Top, Items[Index]);
  11.     end;
  12. end;                      
  13.  

I gather from your message you are trying to remove the highlighted text ?
The only true wisdom is knowing you know nothing

jamie

  • Hero Member
  • *****
  • Posts: 4742
Re: ComboBox and ComboBoxEx
« Reply #2 on: May 05, 2021, 01:00:59 am »
Also try this , this will give you a highlight but does not mark the selected one in the list..

You need to include the lclType unit for this.

Code: Pascal  [Select][+][-]
  1.  
  2. procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer;
  3.   ARect: TRect; State: TOwnerDrawState);
  4. begin
  5.   with TCombobox(Control) do
  6.     Begin
  7.       IF OdFocused in State then
  8.       Canvas.Brush.Color := clHighlight else
  9.       Canvas.Brush.color := Brush.color;
  10.       Canvas.FillRect(AREct);
  11.       If odFocused in state then
  12.       Canvas.Font.Color := Not clHighLight else
  13.       Canvas.Font.Color := font.color;
  14.       If Index <> -1 then
  15.       Canvas.TextOut(Arect.left, Arect.Top, Items[Index]);
  16.     end;
  17. end;
  18.                                            
  19.  
The only true wisdom is knowing you know nothing

valter.home

  • Jr. Member
  • **
  • Posts: 75
Re: ComboBox and ComboBoxEx
« Reply #3 on: May 05, 2021, 12:32:39 pm »
Maybe I'm cracking my head over something very simple that I can't see.
I have tried your code and it obviously does what you say.
My requirement is even simpler, only the selected element (i.e. combobox1.text) must not be highlighted.
It is fine that the elements are highlighted on mouseover but when the TComboBox is closed the selected item it should not be highlighted.
This is because in my application the user creates panels with other components inside including the TComboBox and each of these panels is equipped with a handle through which it is possible to drag the panel above or below the other panels.
To better show which panel is moving, it and all components take on another color until you release the mouse button.
But if the TComboBox item is highlighted you cannot see the color change.
Thanks for your code, I'll try to see if I can adapt it.



jamie

  • Hero Member
  • *****
  • Posts: 4742
Re: ComboBox and ComboBoxEx
« Reply #4 on: May 05, 2021, 11:18:48 pm »
You could always move the focus to another control or use transparent overlay windows.

Let me see if I can modify the code a bit..

The only true wisdom is knowing you know nothing

jamie

  • Hero Member
  • *****
  • Posts: 4742
Re: ComboBox and ComboBoxEx
« Reply #5 on: May 05, 2021, 11:59:58 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer;
  2.   ARect: TRect; State: TOwnerDrawState);
  3. begin
  4.   WIth TCOmboBox(Control) do
  5.   Begin
  6.   canvas.Brush.Color := Brush.color;
  7.   Canvas.Font.Color := Font.Color;
  8.   if (odFocused in State) Then
  9.    Begin
  10.      if Not DroppedDown Then // showing Edit field only.
  11.         Begin
  12.          canvas.brush.color := Brush.color;
  13.          canvas.Font.Color := Font.Color;
  14.         End else
  15.         Begin
  16.          Canvas.Brush.Color := clHighLight;
  17.          Canvas.Font.Color := Not clHighlight;
  18.         end;
  19.    End Else if (odSelected in state)and(DroppedDown) Then
  20.    Begin
  21.      Canvas.Brush.color := clHighLight;
  22.      Canvas.Font.Color := Not clHighLight;
  23.    end;
  24.    Canvas.FillRect(ARect);
  25.    if Index <> -1 then
  26.    Canvas.TextOut(Arect.left, ARect.Top, Items[Index]);
  27. end;
  28.  
  29. end;                                  
  30.  
  31.  
The only true wisdom is knowing you know nothing

valter.home

  • Jr. Member
  • **
  • Posts: 75
Re: ComboBox and ComboBoxEx
« Reply #6 on: May 06, 2021, 09:39:42 am »
Many thanks Jamie,
that's exactly what I needed  :)


 

TinyPortal © 2005-2018