我想做的事:
我在运行时创建了一个面板、2 个编辑字段、一个单选框。当我单击 RadioButton 时,Edit1
我希望该Edit1
字段可见,并Edit2
通过单击 RadioButton 将其归档为不可见和反转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 返回 nil。
你不应该试图阻止它们为零。相反,您应该问自己为什么它们为零。
答案是 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}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句