0

I am able to change the color of the items in DBCombobox using its Style as csOwnerDrawFixed similar to
How do I draw the selected list-box item in a different color?

procedure TForm1.DBComboBoxDrawItem(Control:TWinControl;
   Index: Integer; Rect: TRect; State: TOwnerDrawState);
with (Control as TDBComboBox).Canvas do
begin
 if odSelected in State then
  Brush.Color := $00EACAB6;
  Font.Color := clBlack;
  FillRect(Rect);
  TextOut(Rect.Left, Rect.Top, (Control as TDBComboBox).Items[Index]);
  if odFocused In State then
  begin
    Brush.Color := (Control as TDBComboBox).Color;
    DrawFocusRect(Rect);
   end; {if}
  end; {with} 
end;

Generally, By default i am able to use the DBCombobox Standard (style :csDropdown) to choose the items and also to enter the text in DB Combobox

But the problem i am facing currently is, when i change the color of DBCombobox using (Style as csOwnerDrawFixed or csOwnerDrawVariable). The color gets changed but i am not able enter the text in DBCombobox.

Could someone tell me how can i change the color of items and could enter the text in the DBCombobox at the same time. Thanks !

enter image description here

Note: The Color Shown is not the default one but the modified one

delsql
  • 601
  • 2
  • 7
  • 15
  • Can you show us what you have so we can reproduce? – Dsm Sep 28 '17 at 12:33
  • Sorry, I meant code-wise. I should have made that clear. – Dsm Sep 28 '17 at 12:53
  • For a DBComboBox the help explicitly states that this is not possible (I.e. in these modes users cannot input data). As a suggestion you might consider using a normal TCombobox and link it to the data source via live bindings, but I appreciate that this could be a major shift. – Dsm Sep 28 '17 at 13:53

1 Answers1

0

With TComboBox it is possible. Try this. You can add CloseUp and DropDown event handlers

procedure TForm1.ComboBox1CloseUp(Sender: TObject);
begin
 TComboBox(Sender).Style := csDropDown
end;

procedure TForm1.ComboBox1DropDown(Sender: TObject);
begin
 TComboBox(Sender).Style := csOwnerDrawFixed;
end;

For TDBComboBox there is no OnCloseUp event so you can handle another event for instance OnChange:

procedure TForm1.DBComboBox1Change(Sender: TObject);
begin
   TDBComboBox(Sender).Style := csDropDown
end;

procedure TForm1.DBComboBox1DropDown(Sender: TObject);
begin
  TDBComboBox(Sender).Style := csOwnerDrawFixed;
end;

or set the Style in the Application.OnIdle event handler:

procedure TForm1.ApplicationEvents1Idle(Sender: TObject; var Done: Boolean);
begin
  if (not DBComboBox1.DroppedDown) and (DBComboBox1.Focused)
  then
    DBComboBox1.Style := csDropDown;
  if (not DBComboBox1.DroppedDown) and (not DBComboBox1.Focused)
  then
    DBComboBox1.Style := csOwnerDrawFixed;
end;
asd-tm
  • 3,381
  • 2
  • 24
  • 41
  • I also thought of this, but when I tried it, it did not seem to work. The OnDrawItem event was not triggered. – Dsm Sep 28 '17 at 14:48
  • @Dsm It will be triggered when `DBComboBox`.`Style` is set to `csOwnerDraw*` – asd-tm Sep 28 '17 at 14:52
  • It wasn't for me. I put a break point in the routine and it was not triggered. Maybe I missed something. – Dsm Sep 28 '17 at 14:57
  • @Thanks for the solution. I have tried your first approach with DBCombobox using Change and Dropdown event handlers. But However it works only one instance. When i make dropdown second time it throws an error – delsql Sep 28 '17 at 14:58
  • @Dsm Your issue is discussed [here](https://stackoverflow.com/questions/8563508/how-do-i-draw-the-selected-list-box-item-in-a-different-color?noredirect=1&lq=1). Read the answers and comments in that q. My answer works fine for me. – asd-tm Sep 28 '17 at 16:43