在窗口Form1中用 dalg。Show();显示一个模态对话框,如何在这个模态对话框中改变Form1的文本 在窗口Form1中用 dlg.Show();显示一个非模态对话框(其中dlg是一个对话框对象),如何在这个非模态对话框中改变Form1的文本 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 在dlg的构造式中传入Form1的引用,通过它来改变 既然C#有事件这个东西,为啥不用呢,而且事件在窗体通信方面,有着更为方便的作用,我们知道事件实际上就是状态的捕获,在最后我会举一个捕获状态的例子,先看数据互相操作的例子。 Form2://定义一个需要string类型参数的委托publicdelegate void MyDelegate(string text);public partial class Form2 :Form1 { //定义该委托的事件 public event MyDelegate MyEvent; public Form2(string text) { InitializeComponent(); this.textBox1.Text = text; } private void btnChange_Click(object sender, EventArgs e) { //触发事件,并将修改后的文本回传 MyEvent(this.textBox1.Text); this.Close(); } } Form1:public partial class Form1 :Form { public int index = 0; public string text = null; public Form1() { InitializeComponent(); } private void listBox1_SelectedIndexChanged(object sender, EventArgse) { if (this.listBox1.SelectedItem != null) { text = this.listBox1.SelectedItem.ToString(); index = this.listBox1.SelectedIndex; Form2 form2 = new Form2(text); //注册form2_MyEvent方法的MyEvent事件 form2.MyEvent += new MyDelegate(form2_MyEvent); form2.Show(); } } //处理 void form2_MyEvent(string text) { this.listBox1.Items.RemoveAt(index); this.listBox1.Items.Insert(index, text); } } 可以看出,使用事件做是很方便的,并且不需要传递那么多参数,不需要有继承关系,且提高了代码重用,因此在一般的需求下,建议这么使用。最后说一下事件的状态捕获。我随手帮你搜了个,自己也温习一下:http://www.cnblogs.com/netserver/archive/2009/10/31/1593413.html 刚学C#,对不起了,弱弱问下 Form1 Origin = new Form1();之后用Origin=Original,这样会造成内存泄露吗?new了,没有显示释放。 asp.net 链接地址改变问题 c# winform自定义控件 vs中怎么选择要执行的windows窗体 什么是范式 定义一个实体类,对DataTime类型报错,不解 ActiveX 控件 c#中如何使用SComm控件 关于C#自己封装的DLL库 WinForm TreeView 用鼠标滚轮滚动行数问题 请问各位,怎样可以设置picturebox为透明 关于SqlParameter的一个小问题? c#顺序问题,高手们!
Form2:
//定义一个需要string类型参数的委托publicdelegate void MyDelegate(string text);public partial class Form2 :Form1
{ //定义该委托的事件
public event MyDelegate MyEvent;
public Form2(string text)
{
InitializeComponent();
this.textBox1.Text = text;
}
private void btnChange_Click(object sender, EventArgs e)
{ //触发事件,并将修改后的文本回传
MyEvent(this.textBox1.Text);
this.Close();
}
}
Form1:
public partial class Form1 :Form
{
public int index = 0;
public string text = null;
public Form1()
{
InitializeComponent();
} private void listBox1_SelectedIndexChanged(object sender, EventArgse)
{
if (this.listBox1.SelectedItem != null)
{
text = this.listBox1.SelectedItem.ToString();
index = this.listBox1.SelectedIndex;
Form2 form2 = new Form2(text); //注册form2_MyEvent方法的MyEvent事件
form2.MyEvent += new MyDelegate(form2_MyEvent);
form2.Show();
}
} //处理 void form2_MyEvent(string text)
{
this.listBox1.Items.RemoveAt(index);
this.listBox1.Items.Insert(index, text);
}
}
可以看出,使用事件做是很方便的,并且不需要传递那么多参数,不需要有继承关系,且提高了代码重用,因此在一般的需求下,建议这么使用。最后说一下事件的状态捕获。我随手帮你搜了个,自己也温习一下:
http://www.cnblogs.com/netserver/archive/2009/10/31/1593413.html