|
发表于 2014-11-8 17:23:45
|
显示全部楼层
本帖最后由 SkyGz 于 2014-11-8 17:31 编辑
刚弄的...详见 示例代码, 剩下的自由发辉
Type
TForm1 = Class(TForm)
CmdDel: TButton;
CmdAdd: TButton;
Edit1: TEdit;
CmdCreate: TButton;
Panel1: TPanel;
Procedure CmdCreateClick(Sender: TObject);
Procedure CmdAddClick(Sender: TObject);
Procedure CmdDelClick(Sender: TObject);
Private
cbParam: Array[0..9] Of TComboBox; //定义 数组, 最大可创建10个
Procedure ItemAdd(Cb: TComboBox);
Function GetComboBoxCount: Integer;
Public
{ Public declarations }
End;
Var
Form1: TForm1;
Implementation
{$R *.dfm}
Procedure TForm1.ItemAdd(Cb: TComboBox);
Begin
Cb.Items.Add('1');
Cb.Items.Add('2');
Cb.Items.Add('3');
Cb.Items.Add('other');
End;
Procedure TForm1.CmdCreateClick(Sender: TObject);
Var
I, Max: Integer;
Begin
Max := StrToInt(Edit1.Text);
If Max > 10 Then Exit; //超出允许创建的, 取消
For I := 0 To Max - 1 Do
If Panel1.FindComponent('CbParam' + IntToStr(I)) = Nil Then //先判断 该名称的是否已创建有了
Begin
cbParam[I] := TComboBox.Create(Panel1); //创建到Panel1里
cbParam[I].Parent := Panel1; //在 Panel1里
cbParam[I].Name := 'CbParam' + IntToStr(I); //命名
cbParam[I].Left := 10; //左边距
cbParam[I].Top := I * 25 + 10; //25为每个COMBOBOX的间距, +10是到 顶层边距
ItemAdd(cbParam[I]);
cbParam[I].Style := csDropDownList;
cbParam[I].ItemIndex := 0;
End;
End;
Function TForm1.GetComboBoxCount: Integer;
Var //取当前Combobox总数
I: Integer;
Begin
Result := 0;
For I := 0 To Panel1.ComponentCount - 1 Do
If Panel1.Components[I] Is TComboBox Then
Inc(Result);
End;
Procedure TForm1.CmdAddClick(Sender: TObject);
Var
CurID: Integer;
Begin
CurID := GetComboBoxCount;
If CurID >= 10 Then Exit; //超出设定的数量 不准再创建
cbParam[CurID] := TComboBox.Create(Panel1); //创建到Panel1里
cbParam[CurID].Parent := Panel1; //在 Panel1里
cbParam[CurID].Name := 'CbParam' + IntToStr(CurID); //命名
cbParam[CurID].Left := 10; //左边距
If CurID = 0 Then
cbParam[CurID].Top := 10
Else
cbParam[CurID].Top := cbParam[CurID - 1].Top + 25;
ItemAdd(cbParam[CurID]);
cbParam[CurID].Style := csDropDownList;
cbParam[CurID].ItemIndex := 0;
End;
Procedure TForm1.CmdDelClick(Sender: TObject);
Var
CurID: Integer;
Begin
CurID := GetComboBoxCount;
If CurID = 0 Then Exit; //没有可释放的combobox
cbParam[CurID - 1].Free; //释放
cbParam[CurID - 1] := Nil; //至NIL
End;
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|