选择不同颜色的DBListbox项目和插入文本

时间:2017-09-28 12:28:43

标签: delphi delphi-xe8

我可以使用类似于csOwnerDrawFixed的样式更改DBCombobox中项目的颜色 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;

通常,默认情况下,我可以使用DBCombobox标准(样式:csDropdown)来选择项目,也可以在DB Combobox中输入文本

但我目前面临的问题是,当我使用(样式为csOwnerDrawFixed或csOwnerDrawVariable)更改DBCombobox的颜色时。颜色变了,但我无法在DBCombobox中输入文本。

有人可以告诉我如何更改项目的颜色,并可以同时输入DBCombobox中的文本。谢谢 !

enter image description here

注意:显示的颜色不是默认颜色,而是修改后的颜色

1 个答案:

答案 0 :(得分:0)

使用TComboBox是可能的。试试这个。您可以添加CloseUpDropDown事件处理程序

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

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

对于TDBComboBox,没有OnCloseUp事件,因此您可以处理另一个事件,例如OnChange

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

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

或在Application.OnIdle事件处理程序中设置样式:

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;