动态生成的组件怎么加事件!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!(50分)

  • 主题发起人 主题发起人 fhuibo
  • 开始时间 开始时间
F

fhuibo

Unregistered / Unconfirmed
GUEST, unregistred user!
我在窗体上动态生成了 Edit 和 ComboBox 组件,请问怎么给动态的Edit加 OnDblClick事件和动态的给ComboBox 加 OnChange 事件!!!!!!!!!!!!谢谢!!!!!
 
edit.ondblclick:=myclick;<br>然后过程myclick进行判断和操作
 
Godfear 大哥不行呀,能不能具体点<br>myclick 是过程吗?
 
type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Edit1: TEdit;<br>&nbsp; &nbsp; procedure FormCreate(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; procedure DlClickEvent(Sender: TObject);<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure TForm1.DlClickEvent(Sender: TObject);<br>begin<br>//...........<br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br>&nbsp; Edit1.OnDblClick := self.DlClickEvent;<br>end;
 
published<br>&nbsp;procedure DlClickEvent(Sender: TObject);
 
贴一段我给动态生成的菜单响应单击事件的代码,希望对你有帮助:<br>声明并定义: <br>procedure MenuItemClick(Sender: TObject);<br>procedure Txtsz.MenuItemClick(Sender: TObject);<br>begin<br>&nbsp; ADOQTFreeEMail.First;<br>&nbsp; ADOQTFreeEMail.MoveBy((Sender as TMenuItem).tag);<br>&nbsp; ShellExecute(Handle, 'open', PChar(ADOQTFreeEMail.FieldByName('URL').AsString), nil, nil, SW_SHOW);<br>end;<br>//生成:<br>&nbsp; ADOQTFreeEMail.Open;<br>&nbsp; SetConfig;<br><br>&nbsp; if ADOQTFreeEMail.RecordCount&gt; 0 then<br>&nbsp; begin<br>&nbsp; &nbsp; PopupMenu1.Items.Clear;<br>&nbsp; &nbsp; for i:=0 to ADOQTFreeEMail.RecordCount-1 do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; Item:=TMenuItem.Create(NIL);<br>&nbsp; &nbsp; &nbsp; Item.Caption:=ADOQTFreeEMail.FieldByName('ServerName').AsString;<br>&nbsp; &nbsp; &nbsp; Item.OnClick:=MenuItemClick;//主要是一这句<br>&nbsp; &nbsp; &nbsp; Item.Tag:=i;<br>&nbsp; &nbsp; &nbsp; PopupMenu1.Items.Add(Item);<br>&nbsp; &nbsp; &nbsp; ADOQTFreeEMail.Next;<br>&nbsp; &nbsp; end;<br>&nbsp; end;
 
楼上正解!<br>在组件create的时候,对组件命名也很重要,对与动态事件的判断有帮助!<br>MyClick是自定义过程,<br>procedure MyClick;<br>begin<br>&nbsp; if (sender as TEdit).name='edit1' then<br>&nbsp; begin<br>&nbsp; &nbsp; edit1.text:='aa';<br>&nbsp; end<br>&nbsp; else if (sender as TEdit).name='edit2' then<br>&nbsp; begin<br>&nbsp; &nbsp; edit2.text:='bb';<br>&nbsp; end; <br>&nbsp; .......<br>end;<br>这样就解决了!
 
能不能在procedure DlClickEvent(Sender: TObject); 带个参数<br>例如:procedure DlClickEvent(xgdm: string;Sender: TObject);<br>但是这样的话在调用的时候报错
 
能不能在procedure DlClickEvent(Sender: TObject); 带个参数<br>例如:procedure DlClickEvent(xgdm: string;Sender: TObject);<br>但是这样的话在调用的时候报错 <br><br>那如果需要传一个参数的话怎么做!!! 谢谢!!!
 
通过 Sender 或者用全局变量。
 
我的代码:<br>procedure TForm_Add.FormShow(Sender: TObject);<br>var<br>&nbsp; Edit1: array of TEdit;<br>&nbsp; Label1: array of TLabel;<br>&nbsp; i,j: integer;<br>begin<br>&nbsp; with ADOQPb do<br>&nbsp; begin<br>&nbsp; &nbsp; Close;<br>&nbsp; &nbsp; SQL.Clear;<br>&nbsp; &nbsp; SQL.Text := 'select * from trylb where typeid=''1''';<br>&nbsp; &nbsp; Open;<br>&nbsp; &nbsp; rylb := FieldByName('rylbid').AsString;<br>&nbsp; end;<br>&nbsp; Enbmc := 'A001';<br>&nbsp; i := 0;<br>&nbsp; j := 0;<br>&nbsp; with ADOQAdd do<br>&nbsp; begin<br>&nbsp; &nbsp; Close;<br>&nbsp; &nbsp; SQL.Clear;<br>&nbsp; &nbsp; SQL.Text := 'SELECT * FROM TGSSZ WHERE ENBMC='''+Enbmc+''' AND RYLB='''+rylb+''' AND SFXS=''1'' ORDER BY XSSX';<br>&nbsp; &nbsp; Open;<br>&nbsp; &nbsp; setlength(Edit1,RecordCount);<br>&nbsp; &nbsp; setlength(Label1,RecordCount);<br>&nbsp; &nbsp; while not eof do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; Edit1 := TEdit.Create(Self);<br>&nbsp; &nbsp; &nbsp; Label1 := TLabel.Create(Self);<br>&nbsp; &nbsp; &nbsp; Edit1.Text := '';<br>&nbsp; &nbsp; &nbsp; Label1.Caption := FieldByName('cnzd').AsString;<br>&nbsp; &nbsp; &nbsp; if i &lt;= 20 then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; j := i+1;<br>&nbsp; &nbsp; &nbsp; &nbsp; Edit1.Left := 100;<br>&nbsp; &nbsp; &nbsp; &nbsp; Label1.Left := 100-Label1.Width-10;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; if (i&gt;20) and (i&lt;=40) then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; j := i - 20;<br>&nbsp; &nbsp; &nbsp; &nbsp; Edit1.Left := 320;<br>&nbsp; &nbsp; &nbsp; &nbsp; Label1.Left := 320-Label1.Width-10;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; if (i &gt; 40) and (i &lt;= 60) then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; j := i-40;<br>&nbsp; &nbsp; &nbsp; &nbsp; Edit1.Left := 540;<br>&nbsp; &nbsp; &nbsp; &nbsp; Label1.Left := 540-Label1.Width-10;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; Edit1.Top := j*25;<br>&nbsp; &nbsp; &nbsp; Label1.Top := j*25+5;<br>&nbsp; &nbsp; &nbsp; Edit1.Parent := Form1;<br>&nbsp; &nbsp; &nbsp; Label1.Parent := Form1;<br>&nbsp; &nbsp; &nbsp; if FieldByName('xgdm').AsString &lt;&gt; '' then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; Edit1.Color := clred;<br>&nbsp; &nbsp; &nbsp; &nbsp; Edit1.OnDblClick := getdd;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; i := i + 1;<br>&nbsp; &nbsp; &nbsp; Next;<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>end;<br>生成了好多 EDIT 有一部分Edit需要有双击事件,打开一个窗体(新窗体里有TreeView),然后给Edit赋值,因为参数(FieldByName('xgdm').AsString )不一样打开的窗体的内容(TreeView显示的内容)也不一样,然后通过双击 TreeView 把TreeView 节点内容传给相应的Edit
 
在 getdd 里把 Edit 的指针的指针传给新打开的窗口。
 
在 getdd 里把 Edit 的指针的指针传给新打开的窗口。<br>不能带参数怎么传呀?
 
新窗体加入一个属性<br>&nbsp; public<br>&nbsp; &nbsp; property MyEdit: TEdit read FMyEdit write SetMyEdit;<br><br>然后 getdd 事件<br>procedure TForm1.getdd(Sender: TObject);<br>begin<br>&nbsp; Form2 := TForm2.Create(self);<br>&nbsp; try<br>&nbsp; &nbsp; Form2.MyEdit := TEdit(Sender);<br>&nbsp; &nbsp; Form2.ShowModal;<br>&nbsp; finally<br>&nbsp; &nbsp; Form2.Close;<br>&nbsp; &nbsp; Form2.Free;<br>&nbsp; end;<br>end;
 
sender啊,sender就是edit控件本身啊!<br>var abc:string; &nbsp; &nbsp; &nbsp; &nbsp; //全局变量<br>procedure getdd;<br>begin<br>&nbsp; if (sender as TEdit).name='edit1' then<br>&nbsp; begin<br>&nbsp; &nbsp; abc:=(sender as TEdit).name; &nbsp; &nbsp; &nbsp; &nbsp;//根据<br>&nbsp; &nbsp; //然后用变量newfrm对新窗体显示进行处理<br>&nbsp; &nbsp; ......<br>&nbsp; &nbsp; newform.show;<br>&nbsp; end<br>end;<br>//里面处理的过程是个例子,新窗口打开显示的内容可以用全局变量来处理,自己也是半桶水,不过问题应该能够解决的!:)
 
谢谢!liyinwei<br>再问一下 我 FieldByName('xgdm').AsString 值怎么传到下一页,每个Edit的双击事件的时候,FieldByName('xgdm').AsString 是不一样的。
 
"传到下一页"是什么意思?
 
FieldByName('xgdm').AsString &nbsp;就是怎么传到 Form2里
 
就跟传 Edit 进去的方法是一样的。<br><br>property Value: String read FValue write SetValue;<br><br>&nbsp; &nbsp; Form2.MyEdit := TEdit(Sender);<br>&nbsp; &nbsp; Form2.Value := ADOQAdd.FieldByName('xgdm').AsString;<br>&nbsp; &nbsp; Form2.ShowModal;
 
后退
顶部