急!急!急!有关窗体调用问题?Or 消息?(200分)

  • 主题发起人 acespound
  • 开始时间
A

acespound

Unregistered / Unconfirmed
GUEST, unregistred user!
&nbsp;一个 common 窗体被多个(不定)的窗体调用,问题是:<br>&nbsp; 请问如何将该窗体一个如String返回给调用窗体的如 Edit,ListBox... ????????
 
common 窗体 Form1(unit1.pas) &nbsp;调用窗体 Form2 &nbsp; (unit2.pas)<br>在unit2.pas中 uses unit1;<br>Form2.edit1:=Form1.edit1; 或者在unit1.pas里声明全局变量在Form2里也可以直接使用<br>
 
在Common中声明一个Public的变量。<br>其他窗体可以通过“窗口名.变量名”来访问。
 
分几种情况,方法也有很多种!!!!你的问题描述的太简单了!!!<br>是 Show, 还是 ShowModal?<br>&gt;&gt; 一个 common 窗体被多个(不定)的窗体调用<br>Common 窗体是不是被临时创建的,调用的时候它们之间是父子关系吗?<br>是想在子窗体的该 String 变化时父窗体立刻反映,还是直到子窗体关闭后再反映变化?<br>可以采用属性传递(可以传父窗口的控件名,子窗口的字符串名......),发消息等,<br>kao,问问题你都这么懒?
 
wjiachun 和 tanglu 的方法都不是 00 的,<br>唉,OO 的知音太少了
 
定义全局变量最爽,可西我只会bcb的
 
最好用public的过程访问。
 
可以在被调用窗口的公有部分加一个参数用来保存主调程序的窗口句柄.然后通过PostMessage<br>或者SendMessage来向主调窗口发送消息返回字符串或其它一些信息.<br>
 
是不是可以设置一个公用变量publicstr:string;<br>或者一个公用变量publicedit:tedit;<br>调用者将本窗体的一个edit控件的指针传递给tedit(如果是定义publicedit的话),然后用<br>commonform.showmodal;<br>等commonform关闭后检查publicstr或者tedit的text。<br>也可以用commonform.show方法,然后在edit的onchange事件中进行处理!
 
将这个变量说明为全局变量,调用的时候用类似form1.变量名来调用,不知道可不可以
 
在Form1 的 Public 中定义一个 String 的变量str,然后在 Form2 中如下操作 <br><br>&nbsp; &nbsp;Form1.str := Edit1.Text
 
在被调用窗体 common 中定义一个公共变量不就可以了?
 
关于:<br>&nbsp; 一个 common 窗体被多个(不定)的窗体调用,问题是:<br>&nbsp; 请问如何将该窗体一个如String返回给调用窗体的如 Edit,ListBox... ????????<br>&gt;&gt;是 ShowModal<br>&gt;&gt;Common 窗体不是被临时创建的<br>&gt;&gt;调用的时候它们之间不是父子关系<br>&gt;&gt;直到子窗体关闭后再反映变化<br><br>我觉得用消息传递比较好?<br>
 
用函数吧<br>在窗体中定义一个PUBLIC变量(sResult)<br>(note 为了保证窗体在SHOW 方式下能释放,在他的OnClose event里面Action:=CaFree;)<br>-------------------------------<br>nShowType &nbsp;//0 &nbsp;ShowModal ;1 Show<br>Function GetCommFormStr(Const nShowType:integer):Sring;<br>begin<br>&nbsp; &nbsp;Application.CreateForm(TComm,CommForm);<br>&nbsp; &nbsp;with CommForm do <br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Case nShowType of <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0:begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Showmodal;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Result:=sResult;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Free;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1:begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Show;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result:=sResult; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end<br>&nbsp; &nbsp; &nbsp; end;<br>end;
 
多人接受答案了。
 
无功不受禄,我谈谈我的方法吧,可能烦了一点,但我个人认为比较符合 OO 的思想:<br><br>Commom 窗体:<br><br>type<br>&nbsp; &nbsp; TCommonForm = Class(TForm)<br>&nbsp; &nbsp; ......<br>&nbsp; &nbsp; &nbsp; &nbsp; private<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ........<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; procedure SetSpcString(value: string);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; function &nbsp;GetSpcString: string;<br>&nbsp; &nbsp; &nbsp; &nbsp; .......<br>&nbsp; &nbsp; &nbsp; &nbsp; published<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ......<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Property SpcString: string read GetSpcString write SetSpcString;<br>&nbsp; &nbsp; end;<br><br>implementation<br><br>......<br><br>procedure TCommonForm.SetSpcString(value: string);<br>begin<br>&nbsp; &nbsp; if Edit1.Text &lt;&gt; value then<br>&nbsp; &nbsp; &nbsp; &nbsp; Edit1.Text:= value;<br>end;<br><br>function TCOmmonForm.GetSpcString: string;<br>begin<br>&nbsp; &nbsp; Result:= Edit1.Text;<br>end;<br><br>调用窗体:<br><br>procedure TFormPatrol.Button1Click(Sender: TObject);<br>begin<br>&nbsp; &nbsp; with TCommonForm.Create(Application) do<br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; &nbsp; SpcString:= 'Initialize value'; &nbsp; &nbsp;//根据需要可以不要这一步<br>&nbsp; &nbsp; &nbsp; &nbsp; if ShowModal = mrOK then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Edit2.Text:= SpcString; &nbsp; &nbsp; &nbsp; &nbsp;//得到字符串的值进行处理<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ......<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; finally<br>&nbsp; &nbsp; &nbsp; &nbsp; Free;<br>&nbsp; &nbsp; end;<br>end;<br><br>根据你的情况,处理方法很多,大致的思路还是用属性传递比较好,<br>在 Common 窗体中也可以用一个 Private 变量 FSpcString 来表示 SpcString;<br>在其 OK 按钮按下的时候再设置 FSpcString 的值
 
我是用消息搞定的,但是还有的不明白,先看代码再说吧<br>unit common;<br>interface<br>uses<br>&nbsp; ......<br>const<br>&nbsp; &nbsp;WM_USER_CallCommon=WM_USER+100;<br>type<br>&nbsp; TfrmCommon = class(TForm)<br>&nbsp; ......<br>&nbsp; &nbsp;private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; &nbsp; procedure CallCommon(var Msg:Tmessage);Message WM_USER_CallCommon;<br>&nbsp; public<br>&nbsp; &nbsp; CallFrom:TMessage;<br>&nbsp; &nbsp; { Public declarations }<br>end;<br>&nbsp; &nbsp;implementation<br><br>uses <br>&nbsp; &nbsp; ......;<br><br>{$R *.DFM}<br><br>procedure TfrmCommon.CallCommon(var Msg:TMessage);<br>begin<br>&nbsp; &nbsp; // ......<br>&nbsp; &nbsp; CallFrom:=Msg;<br>end;<br>procedure TfrmTelKey.FormClose(Sender: TObject; var Action: TCloseAction);<br>begin<br>&nbsp; &nbsp; // ......<br>&nbsp; &nbsp; SendMessage(CallFrom.WParam,WM_USER_GetInfo,CallFrom.LParam,0)<br>end;<br>end.<br><br>unit main;<br>interface<br>uses<br>&nbsp; ......<br>const<br>&nbsp; &nbsp;WM_USER_GetInfo=WM_USER+101;<br>type<br>&nbsp; TfrmMain = class(TForm)<br>&nbsp; ......<br>&nbsp; &nbsp;private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; &nbsp; procedure GetInfo(var Msg:Tmessage);Message WM_USER_GetInfo;<br>&nbsp; public<br>&nbsp; &nbsp; CallFrom:TMessage;<br>&nbsp; &nbsp; { Public declarations }<br>end;<br>&nbsp; &nbsp;implementation<br><br>uses <br>&nbsp; &nbsp; ......;<br><br>{$R *.DFM}<br><br>procedure TfrmMain.GetInfo(var Msg:TMessage);<br>begin<br>&nbsp; &nbsp; // ......<br>&nbsp; &nbsp; CallFrom:=Msg;<br>end;<br>procedure TfrmMain.cmdCallCommon(Sender: TObject);<br>begin<br>&nbsp; &nbsp; // ......<br>&nbsp; &nbsp; SendMessage(frmCommon.Handle,WM_USER_CallCommon,frmMain.Handle,0);<br>&nbsp; &nbsp; frmCommon.ShowModal;<br>end;<br>end.<br>&nbsp;<br>如果有多个窗体调用的话,在每个窗体都得添加消息声明,不知有没有只声明一次的方法?<br>
 
也可以将Common窗口的Create异构放入回传参数的地址。<br>...<br>public<br>&nbsp; constructor Create(AOwner: TComponent; ResultStringAdd: ^String); overload;<br>end;<br>使用自定义的方法来创建窗口,就可以满足你OO的爱好。:)
 
顶部