前言
一、窗体间共有字段的传递
1、效果
2、界面设计
3、代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 共有字段传递
{
public partial class Form1 : Form
{
Form2 f2;
public string Msg
{
get
{
return this.textBox1.Text.Trim();
}
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
f2 = new Form2(this);
f2.Show();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 共有字段传递
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public Form2(Form1 f1)
{
InitializeComponent();
this.label1.Text = f1.Msg;
}
}
}
二、窗体间通过构造函数传值
1、效果
2、界面设计
3、代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 构造传递
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(this.textBox1.Text.Trim());
f2.Show();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 构造传递
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public Form2(string msg)
{
InitializeComponent();
this.label1.Text = msg;
}
}
}
三、窗体间通过委托事件传值
1、效果
2、界面设计
3、代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 委托与事件传递
{
public delegate void selectChangedHandler(string s);
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnCallForm2_Click(object sender, EventArgs e)
{
CallObject co = new CallObject();
co.selectChangedEvent+=new selectChangedHandler(co_selectChangedEvent);
Form2 f2 = new Form2(co);
f2.ShowDialog();
txtEventResult.Text = "Form2传的信息:" + co.callMessage;
}
public void co_selectChangedEvent(string s)
{
this.txtF2Select.Text = s;
}
}
public class CallObject
{
public string callMessage = "";
public event selectChangedHandler selectChangedEvent;
public void callEvent(string s)
{
this.selectChangedEvent(s);
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 委托与事件传递
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private CallObject co;
public Form2(CallObject cov):this()
{
this.co = cov;
}
private void rb_A_CheckedChanged(object sender, EventArgs e)
{
co.callEvent("A");
}
private void rb_B_CheckedChanged(object sender, EventArgs e)
{
co.callEvent("B");
}
private void rb_C_CheckedChanged(object sender, EventArgs e)
{
co.callEvent("C");
}
private void btnSubmit_Click(object sender, EventArgs e)
{
this.co.callMessage = this.txtMessgae.Text.Trim();
this.Close();
}
}
}