我的窗体上有70个Edit控件,怎样判断它们的Text属性都不为空?难道要and +and 的一个一个加下去?(30分)

  • 主题发起人 主题发起人 tripleh
  • 开始时间 开始时间
T

tripleh

Unregistered / Unconfirmed
GUEST, unregistred user!
我的窗体上有70个Edit控件,怎样判断它们的Text属性都不为空?难道要and +and 的一个一个加下去?
 
你是在同一个form?<br>那样的话你可以取所有的edit控件来一次循环,每个循环作一次检查事件(每个edit的检查事件总是同一个函数名吧?)
 
self.Contorls
 
var flag:boolean;<br>flag:=false;<br>for i:=0 to controlcount-1 do<br>&nbsp;if controls.classname=TEdit then<br>&nbsp; &nbsp; if controls.text='' then flag:=true<br>&nbsp; &nbsp; else flag:=false;
 
同一个窗体中有70多个编辑框呀,厉害,不过楼上的方法可行.
 
1。 &nbsp;那七十个Edit是动态加入的吗?---&gt;&gt;如果是动态加入的,就可以把它们存入一个数组,然后用一个For ... To 循环遍历这个数据组元素,就可以判断出哪一个的Text属性为空了。<br>2。 &nbsp;如果这些组件是你一个一个地搬上Form的,那就太笨了些。不过也很好遍历:<br>var i : integer;<br>begin<br>&nbsp;for i := 1 to Form1.ComponentCount - 1 do<br>&nbsp;begin<br>&nbsp; if Form1.Components is TEdit then<br>&nbsp; &nbsp; if TEdit(Form1.Components).Text = '' then<br>&nbsp; &nbsp; &nbsp; Showmessage('这个TEdit的Text为空:'+ Form1.Components.Name);<br>&nbsp;end;<br>end
 
上面写错一句,是for i := 0 to Form1.ComponentCount - 1 do
 
大概如此! &nbsp; &nbsp;<br>for i:=0 to ComponentCount-1 do<br>&nbsp; &nbsp; &nbsp; if Components is TEdit then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;with (Components as TEdit) do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (Components as TEdit).Text='' then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;showmessage('不能为空');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;exit;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end;<br>
 
不是动态加入的,只是放置了70个Edit控件!
 
疯子的行为
 
呵呵,建议你动态建立TEdit,你那样做太笨了,有人都要骂你是疯子了,呵呵呵^^^^^<br>
 
精神可嘉
 
下面是我的源码,直接调用就可以。<br>&nbsp;function TForm_luru.IsBlank(frm:Tpanel):Boolean;<br>&nbsp; &nbsp; &nbsp; var i:integer;<br>&nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp;result:=false;<br>&nbsp; &nbsp; &nbsp; &nbsp; for i:=0 to frm.ControlCount-1 do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (frm.Controls is TEdit) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (frm.Controls as TEdit).Text='' &nbsp;then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result:=true;//showmessage('不能为空!');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (frm.Controls as TEdit).SetFocus;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Break;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; end;
 
动态加入Tedit到窗体示例:<br>procedure TForm1.Button1Click(Sender: TObject);<br>var myEdit : array [1..70] of TEdit;<br>&nbsp; &nbsp; i, j, theTop , theLeft : integer;<br>begin<br>&nbsp; for i := 1 to 70 do<br>&nbsp; begin<br>&nbsp; &nbsp; MyEdit := TEdit.Create(Owner);<br>&nbsp; &nbsp; MyEdit.Parent := self;<br>&nbsp; &nbsp; MyEdit.Width := 40;<br>&nbsp; &nbsp; MyEdit.Height := 20;<br>&nbsp; end;<br>&nbsp; theTop := 20;<br>&nbsp; for i := 0 to 6 do //7行<br>&nbsp; begin<br>&nbsp; &nbsp; theLeft := 20;<br>&nbsp; &nbsp; for j := 1 to 10 do &nbsp;//10列<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; MyEdit[i*10+j].Top := theTop;<br>&nbsp; &nbsp; &nbsp; MyEdit[i*10+j].Left := theLeft;<br>&nbsp; &nbsp; &nbsp; theLeft := theLeft + 60;<br>&nbsp; &nbsp; &nbsp; MyEdit.Visible := true;<br>&nbsp; &nbsp; &nbsp; MyEdit.Enabled := true;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; theTop := theTop + 50;<br>&nbsp; end;<br>end;<br><br>
 
改进一点:<br>var flag:boolean;<br>flag:=false;<br>for i:=0 to Self.controlcount-1 do<br>&nbsp;if Components.classname=TEdit then<br>&nbsp; &nbsp; if Components.text='' then <br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp;flag:=true;<br>&nbsp; &nbsp; &nbsp;Break;<br>&nbsp; &nbsp; end<br>&nbsp; &nbsp; else flag:=false;
 
用COMPONENT &nbsp;,呵呵:) &nbsp; 楼上高手真多。
 
bjaman的办法有问题
 
动态加入Tedit到窗体示例:<br>procedure TForm1.Button1Click(Sender: TObject);<br>var myEdit : array [1..70] of TEdit;<br>&nbsp; &nbsp; i, j, theTop , theLeft : integer;<br>begin<br>&nbsp; for i := 1 to 70 do<br>&nbsp; begin<br>&nbsp; &nbsp; MyEdit := TEdit.Create(Owner);<br>&nbsp; &nbsp; MyEdit.Parent := Self;<br>&nbsp; &nbsp; MyEdit.Width := 40;<br>&nbsp; &nbsp; MyEdit.Height := 20;<br>&nbsp; end;<br>&nbsp; theTop := 20;<br>&nbsp; for i := 0 to 6 do //7行<br>&nbsp; begin<br>&nbsp; &nbsp; theLeft := 20;<br>&nbsp; &nbsp; for j := 1 to 10 do &nbsp;//10列<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; MyEdit[i*10+j].Top := theTop;<br>&nbsp; &nbsp; &nbsp; MyEdit[i*10+j].Left := theLeft;<br>&nbsp; &nbsp; &nbsp; theLeft := theLeft + 60;<br>&nbsp; &nbsp; &nbsp; MyEdit[i*10+j].Visible := true;//呵呵,上面示例中此处数组下标手误!<br>&nbsp; &nbsp; &nbsp; MyEdit[i*10+j].Enabled := true;//现在这个程序可是运行通过的哦!<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; theTop := theTop + 50;<br>&nbsp; end;<br>end;
 
后退
顶部