C#中两个窗口互传值

2025-01-05 02:29:07
推荐回答(5个)
回答1:

方法一:
首先,在原窗体中代码:
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:

窗体也是类,你应该知道2个类是怎么传值的吧!

你要像这么传值,我建议使用事件来做,方便也好维护。

Form1:
public partial class Form1 : Form
{
public event EventHandler 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 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(f2.Form1_MyClick);
f2.MyClick += new EventHandler(f1.Form2_MyClick);
f1.Show();
f2.Show();

看着是不是比较奇特呀,使用公开的属性传值也行,看个人的喜好吧!

回答3:

给个提示吧,把要传值的Label的Modifier属性设为Public,默认是Private,这样就能在外部访问该值了。

回答4:

做一个静态类,保存值。然后不同窗体访问

回答5:

第一种更改各个控件访问权限
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)
{
...
}