C#中两个窗口互传值
C#中两个窗口A和B,假设点击窗口A中一个按钮能够将窗口A中一个文本框的值传到B中的某个LABEL,点击窗口B中的某个按钮能够将B中的某个文本框传递给A中的某个LABEL...
C#中两个窗口A 和B,假设点击窗口A中一个按钮能够将窗口A中一个文本框的值传到B中的某个LABEL,点击窗口B中的某个按钮能够将B中的某个文本框传递给A中的某个LABEL,请给出一段能够正确跑出结果的代码,我要好的!
展开
展开全部
方法一:
首先,在原窗体中代码:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form3 frm = new Form3(textBox1);
frm.Show();
}
}
接下来传值到第二个窗体接受参数:
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private TextBox txt = null;
public Form3(TextBox textbox)
{
InitializeComponent();
txt = textbox;
}
private void Form3_Load(object sender, EventArgs e)
{
label1.Text = txt.Text;
}
}
方法二:将控件设置为public,用的时候直接窗体名称.控件名称.属性 就OK啦!
首先,在原窗体中代码:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form3 frm = new Form3(textBox1);
frm.Show();
}
}
接下来传值到第二个窗体接受参数:
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private TextBox txt = null;
public Form3(TextBox textbox)
{
InitializeComponent();
txt = textbox;
}
private void Form3_Load(object sender, EventArgs e)
{
label1.Text = txt.Text;
}
}
方法二:将控件设置为public,用的时候直接窗体名称.控件名称.属性 就OK啦!
展开全部
窗体也是类,你应该知道2个类是怎么传值的吧!
你要像这么传值,我建议使用事件来做,方便也好维护。
Form1:
public partial class Form1 : Form
{
public event EventHandler<StringEventArgs> MyClick;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (MyClick != null)
{
MyClick(this, new StringEventArgs(textBox1.Text));
}
}
public void Form2_MyClick(object sender, Form2.StringEventArgs e)
{
textBox1.Text = e.String;
}
public class StringEventArgs:EventArgs
{
public String String;
public StringEventArgs(string s)
{
String = s;
}
}
}
Form2:
public partial class Form2 : Form
{
public event EventHandler<StringEventArgs> MyClick;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (MyClick != null)
{
MyClick(this, new StringEventArgs(textBox1.Text));
}
}
public void Form1_MyClick(object sender, Form1.StringEventArgs e)
{
textBox1.Text = e.String;
}
public class StringEventArgs:EventArgs
{
public String String;
public StringEventArgs(string s)
{
String = s;
}
}
}
使用他们:
Form1 f1 = new Form1();
Form2 f2 = new Form2();
f1.MyClick += new EventHandler<Form1.StringEventArgs>(f2.Form1_MyClick);
f2.MyClick += new EventHandler<Form2.StringEventArgs>(f1.Form2_MyClick);
f1.Show();
f2.Show();
看着是不是比较奇特呀,使用公开的属性传值也行,看个人的喜好吧!
你要像这么传值,我建议使用事件来做,方便也好维护。
Form1:
public partial class Form1 : Form
{
public event EventHandler<StringEventArgs> MyClick;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (MyClick != null)
{
MyClick(this, new StringEventArgs(textBox1.Text));
}
}
public void Form2_MyClick(object sender, Form2.StringEventArgs e)
{
textBox1.Text = e.String;
}
public class StringEventArgs:EventArgs
{
public String String;
public StringEventArgs(string s)
{
String = s;
}
}
}
Form2:
public partial class Form2 : Form
{
public event EventHandler<StringEventArgs> MyClick;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (MyClick != null)
{
MyClick(this, new StringEventArgs(textBox1.Text));
}
}
public void Form1_MyClick(object sender, Form1.StringEventArgs e)
{
textBox1.Text = e.String;
}
public class StringEventArgs:EventArgs
{
public String String;
public StringEventArgs(string s)
{
String = s;
}
}
}
使用他们:
Form1 f1 = new Form1();
Form2 f2 = new Form2();
f1.MyClick += new EventHandler<Form1.StringEventArgs>(f2.Form1_MyClick);
f2.MyClick += new EventHandler<Form2.StringEventArgs>(f1.Form2_MyClick);
f1.Show();
f2.Show();
看着是不是比较奇特呀,使用公开的属性传值也行,看个人的喜好吧!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
给个提示吧,把要传值的Label的Modifier属性设为Public,默认是Private,这样就能在外部访问该值了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
做一个静态类,保存值。然后不同窗体访问
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
第一种更改各个控件访问权限
Modifier设为public,这个确认窗体是否打开
public partial class Form2 : Form
{
Form3 f = new Form3();
bool bShowed = false;
public Form2()
{
InitializeComponent();
f.Load += new EventHandler(f_Load);
f.FormClosed += new FormClosedEventHandler(f_FormClosed);
f.button1.Click += new EventHandler(f_button1_Click);
}
void f_FormClosed(object sender, FormClosedEventArgs e)
{
bShowed = false;
}
void f_Load(object sender, EventArgs e)
{
bShowed = true;
}
void f_button1_Click(object sender, EventArgs e)
{
if (bShowed)
{
this.label1.Text = f.textBox1.Text;
}
}
private void button1_Click(object sender, EventArgs e)
{
if (bShowed)
{
f.label1.Text = this.textBox1.Text;
}
}
}
第二种自定义事件
public partial class Form3 : Form
{
public delegate void ExtendTextChangeHandler(object sender, ExtendTextChangedArgs e);
public event ExtendTextChangeHandler RefreshLabel;
public event ExtendTextChangeHandler RefreshText;
public Form3()
{
InitializeComponent();
this.RefreshLabel += new ExtendTextChangeHandler(Form3_RefreshLabel);
this.RefreshText += new ExtendTextChangeHandler(Form3_RefreshText);
}
void Form3_RefreshText(object sender, ExtendTextChangedArgs e)
{
}
void Form3_RefreshLabel(object sender, ExtendTextChangedArgs e)
{
this.label1.Text = e.Text;
}
private void button1_Click(object sender, EventArgs e)
{
if (RefreshText != null)
{
RefreshText(this,new ExtendTextChangedArgs(this.textBox1.Text);
}
}
}
public class ExtendTextChangedArgs : EventArgs
{
private string m_Text = "";
public string Text
{
get { return this.m_Text; }
set { this.m_Text = value; }
}
public ExtendTextChangedArgs(string text)
{
m_Text = text;
}
}
public partial class Form2 : Form
{
Form3 f = new Form3();
public Form2()
{
InitializeComponent();
f.RefreshText += new Form3.ExtendTextChangeHandler(f_RefreshText);
}
void f_RefreshText(object sender, ExtendTextChangedArgs e)
{
this.label1.Text = e.Text;
}
private void button1_Click(object sender, EventArgs e)
{
}
}
第三种定义函数传参
public void SetLable(string value)
{
...
}
Modifier设为public,这个确认窗体是否打开
public partial class Form2 : Form
{
Form3 f = new Form3();
bool bShowed = false;
public Form2()
{
InitializeComponent();
f.Load += new EventHandler(f_Load);
f.FormClosed += new FormClosedEventHandler(f_FormClosed);
f.button1.Click += new EventHandler(f_button1_Click);
}
void f_FormClosed(object sender, FormClosedEventArgs e)
{
bShowed = false;
}
void f_Load(object sender, EventArgs e)
{
bShowed = true;
}
void f_button1_Click(object sender, EventArgs e)
{
if (bShowed)
{
this.label1.Text = f.textBox1.Text;
}
}
private void button1_Click(object sender, EventArgs e)
{
if (bShowed)
{
f.label1.Text = this.textBox1.Text;
}
}
}
第二种自定义事件
public partial class Form3 : Form
{
public delegate void ExtendTextChangeHandler(object sender, ExtendTextChangedArgs e);
public event ExtendTextChangeHandler RefreshLabel;
public event ExtendTextChangeHandler RefreshText;
public Form3()
{
InitializeComponent();
this.RefreshLabel += new ExtendTextChangeHandler(Form3_RefreshLabel);
this.RefreshText += new ExtendTextChangeHandler(Form3_RefreshText);
}
void Form3_RefreshText(object sender, ExtendTextChangedArgs e)
{
}
void Form3_RefreshLabel(object sender, ExtendTextChangedArgs e)
{
this.label1.Text = e.Text;
}
private void button1_Click(object sender, EventArgs e)
{
if (RefreshText != null)
{
RefreshText(this,new ExtendTextChangedArgs(this.textBox1.Text);
}
}
}
public class ExtendTextChangedArgs : EventArgs
{
private string m_Text = "";
public string Text
{
get { return this.m_Text; }
set { this.m_Text = value; }
}
public ExtendTextChangedArgs(string text)
{
m_Text = text;
}
}
public partial class Form2 : Form
{
Form3 f = new Form3();
public Form2()
{
InitializeComponent();
f.RefreshText += new Form3.ExtendTextChangeHandler(f_RefreshText);
}
void f_RefreshText(object sender, ExtendTextChangedArgs e)
{
this.label1.Text = e.Text;
}
private void button1_Click(object sender, EventArgs e)
{
}
}
第三种定义函数传参
public void SetLable(string value)
{
...
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询