Delphi 选择在运行时创建的组件
我想做的是: 我在运行中创建了一个面板、2个编辑字段和一个radiobox。 当我单击单选按钮Delphi 选择在运行时创建的组件,delphi,Delphi,我想做的是: 我在运行中创建了一个面板、2个编辑字段和一个radiobox。 当我单击单选按钮Edit1时,我希望Edit1字段可见,而Edit2字段不可见 然后单击单选按钮Edit2进行反转 但它不起作用,我可能误解了什么。 你知道吗 procedure CreationPanel(L,T,H,W:integer;Nom,Titre:string {MyPanel:TPanel}); begin MyPanel
Edit1
时,我希望Edit1
字段可见,而Edit2
字段不可见
然后单击单选按钮Edit2
进行反转
但它不起作用,我可能误解了什么。
你知道吗
procedure CreationPanel(L,T,H,W:integer;Nom,Titre:string
{MyPanel:TPanel});
begin
MyPanel:=TPanel.Create(Form1);
with MyPanel do
begin
Height:=H;
Left:=L;
Width:=W;
Top:=T;
BorderStyle:=bsSingle;
BevelWidth:=3;
BevelOuter:=bvRaised;
Parent:=Form1;
Visible:=True;
TabOrder:=-1;
TabStop:=False;
Tag:=100;
Name:=Nom;
Caption:=Nom;
end;
end;
procedure Creation_Edit(L,T,FontSize:integer;
NomComp,NomPanel:String;
Bool:Boolean
{MyPanel:Tpanel});
begin
MyEdit:=TEdit.Create(MyPanel);
with MyEdit do
begin
Parent:=MyPanel;
{Relation Table Dossier avec Table Bezeichnung}
Height :=21 ;
Left :=L ;
Top :=T ;
Width :=100;
Name:=NomComp;
Font.Name:='MS Sans Serif';
Font.Size:=FontSize;
Visible:=Bool;
end;
end;
procedure Creation_RadioGroup(L,T,H,W,FontSize:integer;
RGName,NomPanel:string
{MyPanelRG:TPanel});
begin
MyRadioGroup:=TRadioGroup.Create(MyPanel);
with MyRadioGroup do
begin
Parent:=MyPanel;
Height :=H ;
Left :=L ;
Top :=T ;
Width :=W;
Font.Name:='MS Sans Serif';
Name:=RGName;
Font.Size:=FontSize;
Items.Add('Edit1);
Items.Add('Edit2');
ItemIndex:=0;
OnClick:=Form1.RadioGroupClick;
end;
end;
procedure TForm1.RadioGroupClick(Sender: TObject);
var
E1,E2:TEdit;
begin {1}
E1:=TEdit(FindComponent('Edit1'));
E2:=TEdit(FindComponent('Edit2'));
E1:=TEdit.Create(MyPanel);
E2:=TEdit.Create(MyPanel);
if E1=nil then showmessage('E1=Nil');
if E2=nil then showmessage('E2=Nil');
If MyRadioGroup.Name='RD1' then {with TEDIT}
begin {If}
If Assigned(E1) and Assigned(E2) then
begin {2}
Case MyRadioGroup.ItemIndex of
0: begin
E1.Visible:=true;
E2.Visible:=False;
end;
1:begin
E1.Visible:=False;
E2.Visible:=true;
end;
end;{Case}
end;{2}
end;{If}
end;{1}
procedure TForm1.FormCreate(Sender: TObject);
begin
{Pannel Left,Top,Hieght,Width}
CreationPanel(80,100,300,180,'Panel1','Panel 1');
Creation_Edit(30,184,10,'Edit1','Panel1',true);
Creation_Edit(30,234,10,'Edit2','Panel1',false);
{RadioGroup Left,Top,Height,Width,FontSize,Tab}
Creation_RadioGroup(30,12,90,120,12,'RD1','Panel1');
end;
end.
好吧,你的代码是一团乱麻,但我可以告诉你你的根本问题是什么(即导致你问这个问题的问题) 首先,Nasreddine关于移除的观点是正确的
E1:=TEdit.Create(MyPanel);
E2:=TEdit.Create(MyPanel);
这是一个完全挣扎的尝试,因为E1和E2返回零
你不应该试图阻止他们成为零。相反,你应该问问自己,为什么它们是零
答案是Edit1和Edit2不属于Form1。它们归MyPanel所有
MyEdit:=TEdit.Create(MyPanel);
所以你的代码应该是
procedure TForm1.RadioGroupClick(Sender: TObject);
var
E1,E2:TEdit;
begin {1}
E1:=TEdit( MyPanel.FindComponent('Edit1'));
E2:=TEdit(MyPanel.FindComponent('Edit2'));
if E1=nil then showmessage('E1=Nil');
if E2=nil then showmessage('E2=Nil');
//etc...
end;{1}
删除
E1:=TEdit.Create(MyPanel)代码>E2:=TEdit.Create(MyPanel)
和try againI已尝试,但E1和E2为零。我的问题是:如何分配这两个编辑。我尝试直接使用我创建的编辑的名称,但它不起作用。我的印象是E1.Visible ligne虽然编译了,但程序不知道它是什么,也不知道它在哪里。代码由于缺乏缩进而无法阅读,并且充满了我们不需要的东西。毫无疑问,这对你也没有帮助。请使用格式正确的最小示例重试。还要考虑调试程序。并且强烈地想一想。当TEdit(FindComponent('Edit1'))
返回nil
时,您应该解决这个问题,而不是盲目地创建另一个控件。要么使用现有控件,要么创建新控件。它是哪一个?出于目的,我很抱歉,我在开头加了4个空格。在某些情况下,比如在botom,我的意图是正确的,但在顶部它不起作用。应该使用开始标记和结束标记。当您在设计时使用组件时,可以在代码Edti1.name或Edit1.visible等中写入。但是,尽管我命名了组件,但它不希望接受没有“”的Edit1。它告诉我未知变量。所以我试着四处奔波,但没有成功。这就是我错过的。坦克