4个回答
展开全部
子窗体代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1 {
//定义委托
public delegate void ChangeTextHandler(string text);
public partial class ChildFrm : Form {
//定义事件
public event ChangeTextHandler ChangeTextEvent;
public ChildFrm() {
InitializeComponent();
}
/// <summary>
///
/// </summary>
/// <param name="sender">事件源</param>
/// <param name="e">事件对象</param>
private void radioButton1_CheckedChanged(object sender, EventArgs e) {
RadioButton rdo = sender as RadioButton;
//引发事件
if (ChangeTextEvent != null) {
ChangeTextEvent(rdo.Text);
}
}
private void ChildFrm_MouseClick(object sender, MouseEventArgs e) {
//e.Button == MouseButtons.Right
}
private void ChildFrm_Load(object sender, EventArgs e)
{
}
}
}
父窗体代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e) {
ChildFrm frm = new ChildFrm();
//订阅事件
frm.ChangeTextEvent += new ChangeTextHandler(frm_ChangeTextEvent);
frm.ShowDialog();
}
void frm_ChangeTextEvent(string text) {
this.textBox1.Text = text;
}
}
}
就这样
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1 {
//定义委托
public delegate void ChangeTextHandler(string text);
public partial class ChildFrm : Form {
//定义事件
public event ChangeTextHandler ChangeTextEvent;
public ChildFrm() {
InitializeComponent();
}
/// <summary>
///
/// </summary>
/// <param name="sender">事件源</param>
/// <param name="e">事件对象</param>
private void radioButton1_CheckedChanged(object sender, EventArgs e) {
RadioButton rdo = sender as RadioButton;
//引发事件
if (ChangeTextEvent != null) {
ChangeTextEvent(rdo.Text);
}
}
private void ChildFrm_MouseClick(object sender, MouseEventArgs e) {
//e.Button == MouseButtons.Right
}
private void ChildFrm_Load(object sender, EventArgs e)
{
}
}
}
父窗体代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e) {
ChildFrm frm = new ChildFrm();
//订阅事件
frm.ChangeTextEvent += new ChangeTextHandler(frm_ChangeTextEvent);
frm.ShowDialog();
}
void frm_ChangeTextEvent(string text) {
this.textBox1.Text = text;
}
}
}
就这样
展开全部
你到form1里写一个构造函数.参数为了所要传的值..
再到form2里面实例化form1..接受里面的参数.
再到form2里面实例化form1..接受里面的参数.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
帮你解决了...
建一个Form(我的是Form2),建一个参数类,继承自EventArgs,设置一个字符型属性,用来传递画面上的textbox中的值到Form3的label中,然后定义一个委托,,在利用该委托定义一个事件,在Form2上的button点下的时候,将Form3中自己定义的一个时间注册到该委托中,利用参数e进行传值...
具体如下(Form2画面就一个textbox和一个button,Form3画面就一个label,在Form2上textbox输入内容,点击button,Form3的label显示Form2的textbox中的内容)
------------------------------form2代码-----------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace baidu
{
public delegate void myEventHandle(object sender, myEventArges e);
public partial class Form2 : Form
{
public myEventHandle myClick;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form3 frm = new Form3();
myClick += new myEventHandle(frm.MyClick);
if (myClick != null)
{
myClick(this, new myEventArges(this.textBox1.Text));
}
frm.Show();
}
}
public class myEventArges : EventArgs
{
string myString;
public string Mystring
{
get
{
return myString;
}
set
{
if (value != null)
{
myString = value;
}
else
{
myString = string.Empty;
}
}
}
public myEventArges(string ss)
{
Mystring = ss;
}
}
}
---------------------------------form3代码----------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace baidu
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
public void MyClick(Object sender, myEventArges e)
{
this.label1.Text = string.Format("来自Form2:{0}", e.Mystring);
}
}
}
建一个Form(我的是Form2),建一个参数类,继承自EventArgs,设置一个字符型属性,用来传递画面上的textbox中的值到Form3的label中,然后定义一个委托,,在利用该委托定义一个事件,在Form2上的button点下的时候,将Form3中自己定义的一个时间注册到该委托中,利用参数e进行传值...
具体如下(Form2画面就一个textbox和一个button,Form3画面就一个label,在Form2上textbox输入内容,点击button,Form3的label显示Form2的textbox中的内容)
------------------------------form2代码-----------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace baidu
{
public delegate void myEventHandle(object sender, myEventArges e);
public partial class Form2 : Form
{
public myEventHandle myClick;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form3 frm = new Form3();
myClick += new myEventHandle(frm.MyClick);
if (myClick != null)
{
myClick(this, new myEventArges(this.textBox1.Text));
}
frm.Show();
}
}
public class myEventArges : EventArgs
{
string myString;
public string Mystring
{
get
{
return myString;
}
set
{
if (value != null)
{
myString = value;
}
else
{
myString = string.Empty;
}
}
}
public myEventArges(string ss)
{
Mystring = ss;
}
}
}
---------------------------------form3代码----------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace baidu
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
public void MyClick(Object sender, myEventArges e)
{
this.label1.Text = string.Format("来自Form2:{0}", e.Mystring);
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
无聊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询