获取ComboBox的内容问题 ( 积分: 200 )

  • 主题发起人 主题发起人 bslhszy
  • 开始时间 开始时间
B

bslhszy

Unregistered / Unconfirmed
GUEST, unregistred user!
一个图书馆管理程序有两个ComboBox,怎么去获取ComboBox的数量,并且知道用户选择了哪个ComboBox?空间出问题了,无法传图,略微描绘一下.<br><br> &nbsp; &nbsp; &nbsp; &nbsp; ComboBox &nbsp; &nbsp; &nbsp; &nbsp;ComboBox<br> &nbsp; &nbsp; &nbsp; &nbsp; 学生进入 &nbsp; &nbsp; &nbsp; &nbsp;教师进入<br>以上是大概的情况.只是&quot;学生进入&quot;和&quot;教师进入&quot;两个按纽是一副图画,我用工具找了半天也没找到Button控件.<br><br>要解决的问题是,我点&quot;学生进入&quot;或者&quot;教师进入&quot;的时候,获取ComboBox的内容.<br>不好意思,我开始没说明白.谢谢大家了
 
一个图书馆管理程序有两个ComboBox,怎么去获取ComboBox的数量,并且知道用户选择了哪个ComboBox?空间出问题了,无法传图,略微描绘一下.<br><br> &nbsp; &nbsp; &nbsp; &nbsp; ComboBox &nbsp; &nbsp; &nbsp; &nbsp;ComboBox<br> &nbsp; &nbsp; &nbsp; &nbsp; 学生进入 &nbsp; &nbsp; &nbsp; &nbsp;教师进入<br>以上是大概的情况.只是&quot;学生进入&quot;和&quot;教师进入&quot;两个按纽是一副图画,我用工具找了半天也没找到Button控件.<br><br>要解决的问题是,我点&quot;学生进入&quot;或者&quot;教师进入&quot;的时候,获取ComboBox的内容.<br>不好意思,我开始没说明白.谢谢大家了
 
一:ComboBox数量:2<br>二:<br>if ActiveControl=ComboBox1 then<br> &nbsp;showmessage('选择ComboBox1')<br>else if ActiveControl=ComboBox2 then<br> &nbsp;showmessage('选择ComboBox2');
 
可以用findcomponent()函数查找来获得combobox的数量
 
这个有点特殊性,我一会抓张图上来.谢谢楼上两位.
 
你不是说有2个么?怎么还要获取多少个?
 
有什么意义吗?<br>我不明白,你要实现什么功能啊?
 
参考一下下面的代码,希望对你有帮助<br>procedure Tfrom1.Button3Click(Sender: TObject);<br>Var<br>i,j:integer;<br>begin<br> &nbsp;for i := 0 to ComponentCount -1 do<br> &nbsp;Begin<br> &nbsp; &nbsp;if Components is TComboBox &nbsp;then<br> &nbsp; &nbsp;Begin<br> &nbsp; &nbsp; &nbsp;Inc(j); &nbsp;// 在这里对 TComboBox 计数<br> &nbsp; &nbsp; &nbsp;with Components as TComboBox do<br> &nbsp; &nbsp; &nbsp;Begin<br> &nbsp; &nbsp; &nbsp; &nbsp;// 在这里可以操作TComboBox<br> &nbsp; &nbsp; &nbsp; &nbsp;Text := '' ;<br> &nbsp; &nbsp; &nbsp;End;<br> &nbsp; &nbsp;End;<br> &nbsp;End;<br>end;
 
这个就是获取ComboBox的个数<br>ComboBox.RecordCount
 
如果要判断是点击了那个ComboBox,通过给不同的ComboBox设置不同的Tag值就行了,这种方法最简单。<br>如果要统计一个窗体中有多少个ComboBox,用下面代码<br>For nIdx := 0 to ComponentCount - 1 do<br>begin<br> &nbsp;if Components[nIdx] is TComboBox then<br> &nbsp; &nbsp;计数加1<br>end;
 
procedure TForm1.Button1Click(Sender: TObject);<br>var<br> &nbsp;i:integer;<br> &nbsp;j:integer;<br>begin<br> &nbsp;J:=0;<br> &nbsp;for i:=0 to Form1.ComponentCount-1 do<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;if Form1.Components.ClassType = TComboBox then<br> &nbsp; &nbsp; &nbsp;j:=j+1;<br> &nbsp; &nbsp;end;<br> &nbsp;showmessage(inttostr(j));<br>end; &nbsp;这样可以检测FORM上面的TComboBox 数量的多少。
 
function getComponentCount():integer<br>For i := 0 to self.ComponentCount - 1 do<br>begin<br> &nbsp;if Components is TComboBox then result:=result+1;<br>end;
 
procedure TForm1.ComboBox1Change(Sender: TObject);<br>var<br> &nbsp;item:string;<br>begin<br> &nbsp;item:=ComboBox1.Text;<br> &nbsp;if item='學生進入' then<br> &nbsp;begin<br> &nbsp; &nbsp; ShowMessage(ComboBox2.Text);<br> &nbsp;end else begin<br> &nbsp; &nbsp; ShowMessage(ComboBox2.Text);<br> &nbsp;end;<br>end;
 
for i:=0 to Form1.ComponentCount-1 do<br>begin<br> &nbsp;if Form1.Components.ClassType = TComboBox then<br> &nbsp;j:=j+1;<br>end;<br>不如这个效率高,特别是窗体上控件很多的时候<br>for i:=0 to Form1.ControlCount-1 do<br>begin<br> &nbsp;if Form1.Controls.ClassType = TComboBox then<br> &nbsp;j:=j+1;<br>end;<br><br>//-------------------------------------------------------<br>//ComboBox1和ComboBox2共用同一个Change事件<br>procedure TForm1.ComboBox1Change(Sender: TObject);<br>begin<br> &nbsp;//仅是获取内容 &nbsp;<br> &nbsp;showmessage('要获取的内容(可输入模式):'+TComboBox(Sender).Text);<br> &nbsp;showmessage('要获取的内容(只选择模式):'+TComboBox(Sender).Items[TComboBox(Sender).ItemIndex]);<br> &nbsp;<br> &nbsp;//判断哪一个ComboBox<br> &nbsp;if Sender=ComboBox1 then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; ShowMessage('这是ComboBox1');<br> &nbsp; &nbsp;end<br> &nbsp;else<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; ShowMessage('这是ComboBox2');<br> &nbsp; &nbsp;end;<br>end;
 
我的意思是,另外写一个程序,当点击&quot;学生进入&quot;或者&quot;教师进入&quot;的同时,获取ComboBox的内容.不好意思啊,各位富翁,我这个表达实在不是很清楚.
 
Type<br> &nbsp;TForm=class(TForm)<br> &nbsp;public<br> &nbsp; &nbsp;ComboBoxValue:string;<br> &nbsp;end;<br><br>procedure TForm1.ComboBox1Change(Sender: TObject);<br>begin<br> &nbsp;ComboBoxValue:=+TComboBox(Sender).Text);<br> &nbsp;或<br> &nbsp;ComboBoxValue:=TComboBox(Sender).Items[TComboBox(Sender).ItemIndex]);<br>end;<br><br>----------------------------<br>Form2中<br>begin<br> &nbsp;showmessage('Form1ComboBox的值'+ Form1.ComboBoxValue );<br>end;
 
方法一<br>Table1.First;<br>while not Table1.Eof do begin<br> &nbsp;Combobox1.Items.Append(Table1.FieldByName('...').AsString);<br> &nbsp;Table1.next;<br>end;<br>方法二 <br>combobox.item.clear;<br>with table1 do begin<br> &nbsp;while not eof do begin<br> &nbsp; &nbsp; combobox.item.add(fieldbyname().value);<br> &nbsp; &nbsp; next;<br> &nbsp;end;<br>end;
 
我想楼主是指取得一个外部程序的信息!如果是的,请参考下面的代码:<br>Handle := FindWindow(nil, '你想获得信息的窗口标题');<br>if Handle &lt;&gt; 0 then EnumChildWindows(Handle, @EnumerateChildWindows, 0);<br><br>function EnumerateChildWindows(hWnd: HWND; lParam: LPARAM): BOOL; stdcall;<br>var<br> &nbsp;ClassName: &nbsp; &nbsp; array [0..255] of Char;<br> &nbsp;I: Integer;<br>begin<br> &nbsp;FillChar(ClassName, 256, 0);<br> &nbsp;GetClassName(hWnd, ClassName, 255);<br><br> &nbsp;if (StrPas(ClassName) = 'ComboBox') then<br> &nbsp;begin<br> &nbsp; &nbsp;//...这里实现你的代码<br> &nbsp;end;<br> &nbsp;<br> &nbsp;if GetFocus = hWnd then<br> &nbsp;begin<br> &nbsp; &nbsp;//...这里实现你的代码<br> &nbsp;end;<br><br> &nbsp;Result := True;<br>end;
 
接受答案了.
 

Similar threads

D
回复
0
查看
802
DelphiTeacher的专栏
D
D
回复
0
查看
747
DelphiTeacher的专栏
D
D
回复
0
查看
696
DelphiTeacher的专栏
D
后退
顶部