C#实现窗口之间的传值
为了解决在多个窗口之间的传值问题,我们可以通过设置静态类和静态变量的办法来实现窗口间值的传递
窗体一代码
//窗体1的代码
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading.Tasks;
usingSystem.Windows.Forms;
namespaceWindowsFormsApplication1
{
publicpartialclassForm1:Form
{
publicForm1()
{
InitializeComponent();
}
privatevoidbutton1_Click(objectsender,EventArgse)
{
sharedclass.sharedvalue=textBox1.Text.ToString();//静态变量的用法:类名.变量名赋值给静态变量
Form2frm2=newForm2();
frm2.Show();
}
}
publicstaticclasssharedclass//在命名空间设置一个静态类sharedclass,不要放置在form1前面
{
publicstaticstringsharedvalue;//设置一个静态变量sharedvalue
}
}
窗体2代码
//窗体2的代码
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading.Tasks;
usingSystem.Windows.Forms;
namespaceWindowsFormsApplication1
{
publicpartialclassForm2:Form
{
publicForm2()
{
InitializeComponent();
textBox1.Text=sharedclass.sharedvalue;//静态变量传入给窗口2的textBox
}
}
}
以上所述就是本文的全部内容了,希望大家能够喜欢。
热门推荐
8 年月日祝福语简短
9 合字的祝福语简短
10 祝福语大全简短6个
11 哥哥考大学祝福语简短
12 女神节日简短祝福语大全
13 农民朋友发财祝福语简短
14 英文祝福语简短带名字
15 老人过大寿祝福语简短
16 老师祝福语简短100字
17 关于好的祝福语简短
18 给老师祝福语简短创意