网上有关于窗体之间传值的解答,方法各异
自己呢记性不好,参考别人的方法,实践了下,写下来,以后自己找的时候也就没那么麻烦了
窗体之间的传值方法
一 传一个公有变量过去
以下是代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//发送窗体 即需要传值出去的窗体
namespace 窗体之间传值
{
public partial class FormSender : Form
{
public FormSender()
{
InitializeComponent();
}
//声明一个静态公有的变量msg
public static string msg = "";
//按钮单击事件
private void BtnSender_Click(object sender, EventArgs e)
{
//取到文本框的值并赋值给msg
msg = txtSender.Text.Trim();
///以下为现实接受窗体 以及关闭该窗体
FormAccept Frmaccept = new FormAccept();
Frmaccept.ShowDialog();
this.Close();
}
}
}
---------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//接受窗体 即需要显示来自发送窗体的值
namespace 窗体之间传值
{
public partial class FormAccept : Form
{
public FormAccept()
{
InitializeComponent();
}
private void BtbAccept_Click(object sender, EventArgs e)
{
//打击鼠标 将传过来的值赋值给文本框
txtAccept.Text = FormSender.msg;
}
}
}
二 通过窗体的公有属性和owner属性(不是很理解)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//发送窗体 即需要传值出去的窗体
namespace 窗体之间传值
{
public partial class FormSender : Form
{
public FormSender()
{
InitializeComponent();
}
//声明变量
public string msg;
private void BtnSender_Click(object sender, EventArgs e)
{
//给变量赋值
msg = txtSender.Text;
//把FormSender作为FormAccept所有者传递给FormAccept
FormAccept frmaccept = new FormAccept();
frmaccept.ShowDialog(this);
}
}
}
-------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//接受窗体 即需要显示来自发送窗体的值
namespace 窗体之间传值
{
public partial class FormAccept : Form
{
public FormAccept()
{
InitializeComponent();
}
private void BtbAccept_Click(object sender, EventArgs e)
{
FormSender frmsender = (FormSender)this.Owner;
txtAccept.Text = frmsender.msg;
}
}
}
三 通过静态变量传递
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//发送窗体 即需要传值出去的窗体
namespace 窗体之间传值
{
public partial class FormSender : Form
{
public FormSender()
{
InitializeComponent();
}
//声明一个静态字段
static string msg ;
//设置其公有属性
public string Msg
{
get { return msg; }
set { msg = value; }
}
//按钮单击事件
private void BtnSender_Click(object sender, EventArgs e)
{
//取到文本框的值并赋值给msg
Msg = txtSender.Text.Trim();
///以下为现实接受窗体 以及关闭该窗体
FormAccept Frmaccept = new FormAccept();
Frmaccept.ShowDialog();
this.Close();
}
}
}
---------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//接受窗体 即需要显示来自发送窗体的值
namespace 窗体之间传值
{
public partial class FormAccept : Form
{
public FormAccept()
{
InitializeComponent();
}
private void BtbAccept_Click(object sender, EventArgs e)
{
//打击鼠标 将传过来的值赋值给文本框
FormSender frmSender = new FormSender();
txtAccept.Text = frmSender.Msg;
}
}
}
--------------------
自己的一点小看法而已 欢迎批评指正