我想实现如下功能:父窗体生成一个子窗体,在自窗体内的文本框内输入一个值,点击确定,自窗体关闭,并将文本框内的值传给父窗体,父窗体利用这个值改变自身某个控件的内容。
我再网上搜了好久还没找到答案。
哪位前辈能教下小弟,小弟感激不敬。
我再网上搜了好久还没找到答案。
哪位前辈能教下小弟,小弟感激不敬。
解决方案 »
- Fatal error encountered during command execution.错误
- C#网络通信问题
- 在VS中连接数据库的步骤是什么?
- C# 连接Oracle的问题,高手来救命啊!
- 动态webservice类的GetProperties()
- vs2008创建的安装项目无法在WIN2000下安装
- 用C#编程遇到错误 说是产生相同参数类型的成员
- C# Winform中,BindingNavigator如何綁定到IList<>數據源?
- 当父类无法标记为可序列化的时候,子类如何进行二进制序列化
- 在installshield developer 8.0中加上.net framework组件安装时报错
- C#如何做成setup文件
- sqlConnection棘手问题(经常提示不支持并行事务附代码)
首先
form1.cs文件中又一个button事件和一个方法。
private void button5_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();//第一种方法,把form2的setText事件交给form1的GetText(string text)方法。
f2.T += new Form2.SetText(GetText);
f2.ShowDialog();//第2中方法把form1的对象传给form2
Form2 ff = new Form2(this);
} public void GetText(string text)
{
string value = text;
}然后就是
form2.cs文件
public partial class Form2 : Form
{
//第一种方法就是代理。
public delegate void SetText(string text);
public event SetText T; Form1 f1 = null; public Form2()
{
InitializeComponent();
}//第2中方法
public Form2(Form1 parentForm)
{
this.f1 = parentForm;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//第2中方法,这里可以调用form1的公开方法和属性,随便你喜欢那一种都可以
f1.GetText(textBox1.Text);//第一种方法
if (T != null)
T(textBox1.Text); this.Close();
}
}
Form2 ff = new Form2();
if (ff.ShowDialog() == DialogResult.OK)
{
MessageBox.Show(ff.s);
}子窗体
public string s;
private void button1_Click(object sender, EventArgs e)
{
s = this.textBox1.Text;
this.DialogResult = DialogResult.OK;
}
子窗体按钮事件:uc.strVar=textBox1.Text;
子窗体关闭以后,父窗体可以接收到该变量的值,也使用uc.strVar。
asp.net下可刷新父窗体,并传值过去来实现
public void updataControl(string txt)
{
TextBox1.Text=txt;
}private void button1_Click(object sender, EventArgs e)
{
SubForm frmSub= new SubForm ();
//SubForm frmSub= new SubForm (this);//构造函数传对象 frmSub.frmMain= this;
frmSub.show();
}在SubForm public MainForm frmMain;private void buttonOK_Click(object sender, EventArgs e)
{
frmMain.updataControl (TextBox1.Text) ;
this.Close()
}等等类似