procedure TForm1.FormCreate(Sender: TObject); begin ComboBox1.Style := csDropDownList; ComboBox1.Items.Add('aaa'); ComboBox1.Items.Add('bbb'); end;procedure TForm1.ComboBox1Click(Sender: TObject); begin ShowMessage(ComboBox1.Text + ':' + IntToStr(ComboBox1.ItemIndex)); end;
在OnDrawItem事件里面好像可以...
给个思路参考一下:组合框是由一个编辑框和列表框组成,只要捕获列表框的WM_MOUSEMOVE消息时返回ComboBox的ItemIndex就可以了。 用ApplicationEvents控件可以在OnMessage里写: if Msg.message = WM_MOUSEMOVE then Caption:=IntToStr(ComboBox.ItemIndex);至于怎么添加事件写成控件,自己想想。
添加一个TActionList,假设命名为:actList,在actList的OnUpdate事件中写下:if SameText(ComboBox1.Items[ComboBxo1.ItemIndex], 'Test') then ShowMessage('Test') else if SameText(ComboBox1.Items[ComboBxo1.ItemIndex], 'Ok') then ShowMessage('Ok'); 就可以了.
if SameText(ComboBox1.Items[ComboBxo1.ItemIndex], 'Test') then Edit1.Text := 'Test' else if SameText(ComboBox1.Items[ComboBxo1.ItemIndex], 'Ok') then Edit1.Text := 'Ok';此时你再下拉看看,效果是不是就是你讲的那样?
老之的思路实现不了.即使成功了,还不如自己写个TComboxBox.
unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TMyComboBox = class(TComboBox) private public procedure WndProc(var Message: TMessage); override; end; TForm1 = class(TForm) Button1: TButton; ComboBox1: TComboBox; Memo1: TMemo; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}procedure TMyComboBox.WndProc(var Message: TMessage); var buf: PChar; index: Integer; begin with Message do begin case Msg of WM_CTLCOLORLISTBOX: begin GetMem(Buf, 20); index := Integer(SendMessage( LParam, LB_GETCURSEL, 0, 0 )); SendMessage( LParam, LB_GETTEXT, index, Integer(buf) ); Form1.Memo1.Lines.Add( buf ); FreeMem( Buf ); end; end; end; inherited WndProc( Message );end;procedure TForm1.Button1Click(Sender: TObject); var MyComboBox: TMyComboBox;begin MyComboBox := TMyComboBox.Create(self); MyComboBox.Top := 10; MyComboBox.Parent := self; MyComboBox.Items.Add('ddsf'); MyComboBox.Items.Add('b24'); MyComboBox.Items.Add('c23sdsew'); MyComboBox.Items.Add('AAA'); end;end.
begin
ComboBox1.Style := csDropDownList;
ComboBox1.Items.Add('aaa');
ComboBox1.Items.Add('bbb');
end;procedure TForm1.ComboBox1Click(Sender: TObject);
begin
ShowMessage(ComboBox1.Text + ':' + IntToStr(ComboBox1.ItemIndex));
end;
用ApplicationEvents控件可以在OnMessage里写:
if Msg.message = WM_MOUSEMOVE then Caption:=IntToStr(ComboBox.ItemIndex);至于怎么添加事件写成控件,自己想想。
在COMBOX 的 onChange里面写代码;每当你的选择有改变时都会触发该事件2.并且能知道目前焦点在下拉列表的哪一项上?
cbx.ItemIndex 就可以获取. ItemIndex一般默认为-1; 0 的时候选择的是第一项...类推.
ShowMessage('Test')
else if SameText(ComboBox1.Items[ComboBxo1.ItemIndex], 'Ok') then
ShowMessage('Ok');
就可以了.
Edit1.Text := 'Test'
else if SameText(ComboBox1.Items[ComboBxo1.ItemIndex], 'Ok') then
Edit1.Text := 'Ok';此时你再下拉看看,效果是不是就是你讲的那样?
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TMyComboBox = class(TComboBox)
private
public
procedure WndProc(var Message: TMessage); override;
end; TForm1 = class(TForm)
Button1: TButton;
ComboBox1: TComboBox;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TMyComboBox.WndProc(var Message: TMessage);
var
buf: PChar;
index: Integer;
begin with Message do
begin
case Msg of
WM_CTLCOLORLISTBOX:
begin
GetMem(Buf, 20);
index := Integer(SendMessage( LParam, LB_GETCURSEL, 0, 0 ));
SendMessage( LParam, LB_GETTEXT, index, Integer(buf) );
Form1.Memo1.Lines.Add( buf );
FreeMem( Buf );
end;
end;
end; inherited WndProc( Message );end;procedure TForm1.Button1Click(Sender: TObject);
var
MyComboBox: TMyComboBox;begin
MyComboBox := TMyComboBox.Create(self);
MyComboBox.Top := 10;
MyComboBox.Parent := self;
MyComboBox.Items.Add('ddsf');
MyComboBox.Items.Add('b24');
MyComboBox.Items.Add('c23sdsew');
MyComboBox.Items.Add('AAA');
end;end.