关于子窗体向父窗体取值问题

65426 2003-09-15 12:31:06
请问各位大侠:
我在子窗体的textbox中如何索取父窗体comboxBox的值?代码如下:mainfrm frm = new mainfrm(); textBox1.text = frm.comboBox1.text 为什么取不到?我已将父窗体中的comboBox设为public ,什么办法可以取到,谢谢!有没有更好的办法?
...全文
43 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
TheAres 2003-09-15
  • 打赏
  • 举报
回复
看这个FAQ:

http://expert.csdn.net/Expert/FAQ/FAQ_Index.asp?id=4687
65426 2003-09-15
  • 打赏
  • 举报
回复
谢谢楼上几位大侠!!!
chenghaofeng 2003-09-15
  • 打赏
  • 举报
回复
在子窗口设置一个属性,用来传递参数。
例如:
子窗口:
private string strName1 = "";
public string strName
{
get
{
return strName1
}
set
{
strName1 = value;
}
}


在主窗口:
frmChild frmChild = new frmChild();
frmChild.strName = this.comboBox1.text;
frmChild.show();

然后在loading子窗体的方法中,为你的textBox 初始化:textBox1.Text = strName1;


或者直接把子窗口的textBox设置为public(我认为不是很好啦。)
frmChild frmChild = new frmChild();
frmChild.textBox1.Text = this.comboBox1.text;
frmChild.show();
xixigongzhu 2003-09-15
  • 打赏
  • 举报
回复
如果斑竹这两句:
mainfrm frm = new mainfrm();
textBox1.text = frm.comboBox1.text
是在一起的话,无论怎么写都是空的。

有几点不明白:
1.那个是父窗体呢?是mainfrm还是另一个?
2.如果mainfrm是父窗体,为什么在子窗体启动父窗体呢?
3.如果mainfrm不是父窗体,第二句话就不应该写在这里。
65426 2003-09-15
  • 打赏
  • 举报
回复
91bct(行人)大哥:
小弟笨,能把下面的代码再贴出来吗?谢谢
91bct 2003-09-15
  • 打赏
  • 举报
回复
也可以通过在父窗体中设置一个属性,在子窗体中访问这个属性来获得此值:
在MainForm中:
public string strParameter
{
get
{
return this.ComboBox1.Text;
}
}
在ChildForm 中:
MainForm frm=new MainForm();
strResult=frm.strParameter;
91bct 2003-09-15
  • 打赏
  • 举报
回复
在构造函数中:
public ChildForm(string para)
{
strResult=para;
}
这样就可以直接访问strResult来获取comboBox1的text值了。
对了这个this.comboBox1.SelectedText;
应该改为this.comboBox1.Text;
91bct 2003-09-15
  • 打赏
  • 举报
回复
public ChildForm(string para)
{
strResult=para;
}
这样直接访问strResult就可以取得父窗体的你所要的值了
Alton1981 2003-09-15
  • 打赏
  • 举报
回复
设Form1为父窗体,Form2为子窗体;
1.把父窗体中的ComboBox设为public;
2.在子窗体中加上public Form1 f1;
3.在父窗体打开子窗体时这样写:
Form2 f2=new Form2();
Form2.f1=this;
f2.Show();
4.在Form2_Load时写:
string s=f1.ComboBox.Text;
//s即为取到的Form1中的ComboBox的值;
91bct 2003-09-15
  • 打赏
  • 举报
回复
方法一:
在父窗体中定义一个变量,取得comboBox1的值,
将此值传进子窗体中。
private string strParameter;
某个事件中:
strParameter=this.comboBox1.SelectedText;
在new出子窗体的事件或方法中:
ChildForm chfrm=new ChildForm(this.strParameter);
在子窗体中:
private string strResult;
在构造函数中:
public ch
storm97 2003-09-15
  • 打赏
  • 举报
回复
textBox1.text = frm.comboBox1.Selectedtext

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧