unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
ComboBox1: TComboBox;
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
end;
procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
var strlen:integer;
i:integer;
begin
strlen:=length(combobox1.Items[0]);
for i:=1 to combobox1.Items.Count-1 do//计算最大长度
begin
if length(combobox1.Items[i])>strlen then
strlen:=length(combobox1.Items[i]);
end;
combobox1.Width:=strlen*9;//控制width属性
end;
end.
我把代码写在窗体的鼠标移动事件中了,你也可以写在其他过程中。主要是控制组合框的width属性,通过列表框中的每个items的长度来改变。倒数第三行的strlen*9是可以改变的,乘以几都可以,自己试试乘几合适。