如何向MDIChild窗体的formshow事件传入参数?创建MDIChild窗体时,程序会自动运行MDIChild窗体的formshow事件而此时,给MDIChild窗体的参数赋值的语句还未执行,这样的话,就导致了formshow事件中调用参数无效了。
//主窗体代码
procedure btn1.onclick(sender:tobject);
begin
if form2=nil then form2:=TForm2.create(self);
if assigned(form2) then
begin
form2.strTitle:='测试';
form2.show;
end;
end;//子窗体代码
procedure TForm2.onshow(sender:tobject);
begin
lblTitle.caption:=strTitle;
end;
问题是
在执行form2.strTitle:='测试';的时候,其实已经执行了form2.show了,所以参数没有被传入。
//主窗体代码
procedure btn1.onclick(sender:tobject);
begin
if form2=nil then form2:=TForm2.create(self);
if assigned(form2) then
begin
form2.strTitle:='测试';
form2.show;
end;
end;//子窗体代码
procedure TForm2.onshow(sender:tobject);
begin
lblTitle.caption:=strTitle;
end;
问题是
在执行form2.strTitle:='测试';的时候,其实已经执行了form2.show了,所以参数没有被传入。
Create后,就调用此函数并传入参数,在这个函数内做配置动作另外,MDIChild在create后会自己显示出来,不用另外show;
是的,用自定义的函数代替了show,
感觉MDIChild的SHow事件有些问题。好像这个方法多余的。
我是在OnCreat里边做的
procedure TFrm_Base.FormCreate(Sender: TObject);
Var
i:integer;
DBGridEh_Name:string;
begin Self.Caption:=Self.LblTitle.Caption; //在这里赋值
getFormSize(Self); //Get Windows Szie; ÉèÖô°¿ÚµÄ³¤¶È¡¢¿í¶È FConn:=DM.COnn; //´Ó¸ÃÊý¾ÝÄ£¿é»ñµÃÊý¾ÝÁ¬½Ó For i:=0 To Self.ComponentCount-1 do //ÉèÖÃTDBGridEh¸ñʽ
if Self.Components[i] is TDBGrid Then
raise Exception.Create('Program Error:Only Uses DBGridEh!') //Òì³£´¦Àí
else if self.Components[i] is TDBGridEh Then
Begin
TDBGridEh(Components[i]).Constraints.MinHeight:=100;
TDBGridEh(Components[i]).Constraints.MinWidth:=100; DBGridEh_Name := TDBGridEh(Components[i]).Name; //¡¾µ÷ÊÔ¡¿Ö»ÊÇΪÁ˸ú×ÙÖ´ÐÐÄǸöDBGridEh getSeeDBGrds(TDBGridEh(Components[i]),self); //ÉèÖÃÁÐÊÇ·ñ¿É¼û
getGrdWidths(TDBGridEh(Components[i]),Self); //ÉèÖÃÁеĿí¶È
TDBGridEh(Components[i]).OnDrawColumnCell:=grdDrawColumnCell; //°ÑÊý¾ÝÐÐÏÔʾ·½·¨¸³¸ø±í¸ñ
TDBGridEh(Components[i]).OnTitleClick:=grdTitleClick; //°ÑÊý¾ÝÁÐÅÅÐò·½·¨¸³¸ø±í¸ñ InitDataSet(TDBGridEh(self.Components[i])); //³õʼ»¯Êý¾Ý¼¯ SetPopUpMenu(TDBGridEh(Self.Components[i])); //ÉèÖÃÓÒ¼üµ¯³ö´°¿Ú
End;end;
你可以设置一个“公共单元”的再设置一些
public 变量,引用即可
我也是这么做的,公共常量、变量都放公共Public.pas里边
procedure btn1.onclick(sender:tobject);
begin
//如果没有创建,则创建,这里会form2.FormCreate等
if form2=nil then form2:=TForm2.create(self);
//已经创建,赋值显示,如果申明的是公共的应该是可以赋值
if assigned(form2) then
begin
form2.strTitle:='测试';
form2.show;
end;
end