c# WinForm开发 页面之间的传值问题总结 收藏
[这里先借用别人的,有时间慢慢再变成自己的,嘿嘿~~]
一,两个毫无关系的页面之间的传值:
using System;
using System.Collections.Generic;
using System.Text;
namespace WindowsApplication5
{
static class Class1
{
public static string aa;
public static string getA
{
get { return aa; }
}
public static string setA
{
set { aa = value; }
}
}
}
在F1的Form 中点button时附值
private void button1_Click(object sender, EventArgs e)
{
Class1.setA = textBox1.Text;
}
当你在F2的Form中就可以取了
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(Class1.getA);
}
1. 使用构造函数
class Form1:Form
{
public Form1()
{
Form2 f = new Form2("我要传给你");
f.Show();
}
}
public Form2:Form
{
public Form2()
{}
public Form2(string title):this()
{
MessageBox.Show(title);
}
}
2. 使用静态变量
3. 使用委托
1 public delegate void OutLookBarDelegateClickEventHandler(string Item);
2 class OutLookBarDelegate
3 {
4 public static OutLookBarDelegate Instance;
5 public event OutLookBarDelegateClickEventHandler OutLookBarClick; //定义事件
6
7 /// <summary>
8 /// 构造器,建立一个实例。
9 /// </summary>
10 static OutLookBarDelegate()
11 {
12 Instance = new OutLookBarDelegate();
13 }
14 /// <summary>
15 /// 实现事件
16 /// </summary>
17 /// <param name="e">大样栏目点击事件</param>
18 public virtual void OnOutLookBarClick(string Item)
19 {
20 if (OutLookBarClick != null)
21 {
22 OutLookBarClick(Item);
23 }
24 }
委托事件的触发如下:
1 OutLookBarDelegate.Instance.OnOutLookBarClick(item.Text);
//触发事件委托事件的响应如下 :
1 OutLookBarDelegate.Instance.OutLookBarClick+=new OutLookBarDelegateClickEventHandler(Instance_OutLookBarClick);
1 public void Instance_OutLookBarClick(string item)
2 {
3 //to do something
4 }
四,比较典型的例子:
http://www.itwis.com/html/net/winform/20081112/2781.html
五,窗体传值可以分为两类。
1、主窗体往子窗体传值
有两种方法,一种是在子窗体提供重载构造函数,利用重载构造函数传递值,适用于 传值数量比较少;第二种是,在子窗体中定义一个主窗体对象,然后就可以接收到主窗体的属性值了,适用于传值数量大。
主窗体代码如下:
{
private string strValueA = "";
public string StrValueA
{
get
{
return this.strValueA;
}
set { this.strValueA = value; }
}
public frmParent()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.strValueA = textBox1.Text;
frmChild frmchild = new frmChild();
frmchild.Owner = this;
frmchild.ShowDialog();
frmchild.Dispose();
}
private void button2_Click(object sender, EventArgs e)
{
frmChild frmchild = new frmChild(this.textBox1.Text);
string returnValue = "";
if (frmchild.ShowDialog() == DialogResult.OK)
{
returnValue = frmchild.Str;
this.textBox1.Text = returnValue;
}
}
}
子窗体代码如下:
{
private string str;
public string Str
{
get { return this.str; }
set { this.str = value; }
}
private frmParent frmparent;
public frmChild()
{
InitializeComponent();
}
public frmChild(string str)
{
this.str = str;
InitializeComponent();
this.textBox1.Text = str;
}
private void frmChild_Load(object sender, EventArgs e)
{
frmparent = (frmParent)this.Owner;
//this.textBox1.Text = frmparent.StrValueA;
}
private void button1_Click(object sender, EventArgs e)
{
//frmparent = (frmParent)this.Owner;
this.Str = this.textBox1.Text;
this.DialogResult = DialogResult.OK;
this.Close();
}
}
2、从子窗体返回值到主窗体中
利用了子窗体的属性保存子窗体的值,在主窗体中可以访问到子窗体的属性
主窗体代码如下:
{
private string strValueA = "";
public string StrValueA
{
get
{
return this.strValueA;
}
set { this.strValueA = value; }
}
public frmParent()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
frmChild frmchild = new frmChild(this.textBox1.Text);
string returnValue = "";
if (frmchild.ShowDialog() == DialogResult.OK)
{
returnValue = frmchild.Str;
this.textBox1.Text = returnValue;
}
}
}
子窗体代码如下:
{
private string str;
public string Str
{
get { return this.str; }
set { this.str = value; }
}
private frmParent frmparent;
public frmChild()
{
InitializeComponent();
}
private void frmChild_Load(object sender, EventArgs e)
{
frmparent = (frmParent)this.Owner;
//this.textBox1.Text = frmparent.StrValueA;
}
private void button1_Click(object sender, EventArgs e)
{
//frmparent = (frmParent)this.Owner;
this.Str = this.textBox1.Text;
this.DialogResult = DialogResult.OK;
this.Close();
}
}