• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 662
  • Last Modified:

ComboBox csOwnerDrawVariable BUG?

I have a combobox with csOwnerDrawVariable style. I need that when click a button and say ComboBox1.Text:='OK' to assign this string to combo text.
But this don't work. Look that don't work for an Owner style.

What to do to still have an owner drawing but to can assign a text to my combo ( Not just if I select one from drop list).
0
ginsonic
Asked:
ginsonic
  • 2
1 Solution
 
esoftbgCommented:
All speed Buttons have Captions identical with Items of a ComboBox1 and call the method SpeedButton1Click:

procedure TForm1.SpeedButton1Click(Sender: TObject);
var
  I:      Integer;
  S:      string;
  T:      string;
begin
//  ComboBox1.Text := (Sender as TSpeedButton).Caption;  // doesn't work
  T := (Sender as TSpeedButton).Caption;
  with ComboBox1 do
  for I := 0 to Items.Count-1 do
  begin
    S := Items[I];
    if (S=T) then
    begin
      ItemIndex := I;
      Break;
    end;
  end;
end;

emil
0
 
esoftbgCommented:
unit Unit_Q_20944122;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, ToolWin, ComCtrls;

type
  TForm1 = class(TForm)
    ComboBox1: TComboBox;
    ToolBar1: TToolBar;
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    SpeedButton3: TSpeedButton;
    SpeedButton4: TSpeedButton;
    SpeedButton5: TSpeedButton;
    SpeedButton6: TSpeedButton;
    procedure SpeedButton1Click(Sender: TObject);
  private  { Private declarations }
  public   { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.SpeedButton1Click(Sender: TObject);
var
  I:      Integer;
begin
//  ComboBox1.Text := (Sender as TSpeedButton).Caption;  // doesn't work
  with ComboBox1 do
  for I := 0 to Items.Count-1 do
  begin
    if (Items[I]=(Sender as TSpeedButton).Caption) then
    begin
      ItemIndex := I;
      Break;
    end;
  end;
  (Sender as TSpeedButton).Down := True;
end;

end.

//.............................................................

object Form1: TForm1
  Left = 199
  Top = 114
  Width = 696
  Height = 480
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object ComboBox1: TComboBox
    Left = 124
    Top = 50
    Width = 128
    Height = 22
    Style = csOwnerDrawVariable
    DropDownCount = 16
    ItemHeight = 16
    TabOrder = 0
    Items.Strings = (
      'Ok'
      'Cancel'
      'Yes'
      'No'
      'Don''t care'
      'Stop asking'
      'Start'
      'Stop')
  end
  object ToolBar1: TToolBar
    Left = 0
    Top = 0
    Width = 688
    Height = 29
    Caption = 'ToolBar1'
    TabOrder = 1
    object SpeedButton1: TSpeedButton
      Left = 0
      Top = 2
      Width = 64
      Height = 22
      AllowAllUp = True
      GroupIndex = 1
      Caption = 'Ok'
      OnClick = SpeedButton1Click
    end
    object SpeedButton2: TSpeedButton
      Left = 64
      Top = 2
      Width = 64
      Height = 22
      AllowAllUp = True
      GroupIndex = 1
      Caption = 'Cancel'
      OnClick = SpeedButton1Click
    end
    object SpeedButton3: TSpeedButton
      Left = 128
      Top = 2
      Width = 64
      Height = 22
      AllowAllUp = True
      GroupIndex = 1
      Caption = 'Yes'
      OnClick = SpeedButton1Click
    end
    object SpeedButton4: TSpeedButton
      Left = 192
      Top = 2
      Width = 64
      Height = 22
      AllowAllUp = True
      GroupIndex = 1
      Caption = 'No'
      OnClick = SpeedButton1Click
    end
    object SpeedButton5: TSpeedButton
      Left = 256
      Top = 2
      Width = 64
      Height = 22
      AllowAllUp = True
      GroupIndex = 1
      Caption = 'Start'
      OnClick = SpeedButton1Click
    end
    object SpeedButton6: TSpeedButton
      Left = 320
      Top = 2
      Width = 64
      Height = 22
      AllowAllUp = True
      GroupIndex = 1
      Caption = 'Stop'
      OnClick = SpeedButton1Click
    end
  end
end
0
 
kretzschmarCommented:
thats no bug,

with such a style the text-area is not editable.
you must add your text to the items and select the itemindex after this

like

procedure TForm1.Button1Click(Sender: TObject);
var idx : Integer;
begin
  idx := ComboBox1.Items.IndexOf('OK');
  if idx = -1 then
    idx := ComboBox1.Items.add('OK');
  ComboBox1.ItemIndex := idx;
end;

meikl ;-)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now