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
}
}
}
以上所述就是本文的全部内容了,希望大家能够喜欢。
热门推荐
10 五一公司放假祝福语简短
11 追星女孩的祝福语简短
12 给长辈端午祝福语 简短
13 生日祝福语有诗意简短
14 团圆日祝福语简短
15 中秋商务祝福语简短最新
16 温暖文艺简短祝福语短句
17 男孩上学祝福语简短的
18 入住酒店文案祝福语简短