C#实现窗口之间的传值_C#教程-查字典教程网
C#实现窗口之间的传值
C#实现窗口之间的传值
发布时间:2016-12-28 来源:查字典编辑
摘要:为了解决在多个窗口之间的传值问题,我们可以通过设置静态类和静态变量的办法来实现窗口间值的传递窗体一代码//窗体1的代码usingSystem...

为了解决在多个窗口之间的传值问题,我们可以通过设置静态类和静态变量的办法来实现窗口间值的传递

窗体一代码

//窗体1的代码 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { sharedclass.sharedvalue = textBox1.Text.ToString(); //静态变量的用法:类名.变量名 赋值给静态变量 Form2 frm2 = new Form2(); frm2.Show(); } } public static class sharedclass //在命名空间设置一个静态类sharedclass,不要放置在form1前面 { public static string sharedvalue; //设置一个静态变量sharedvalue } }

窗体2代码

//窗体2的代码 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form2 : Form { public Form2() { InitializeComponent(); textBox1.Text = sharedclass.sharedvalue; //静态变量传入给窗口2的textBox } } }

以上所述就是本文的全部内容了,希望大家能够喜欢。

相关阅读
推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
  • 大家都在看
  • 小编推荐
  • 猜你喜欢
  • 最新C#教程学习
    热门C#教程学习
    编程开发子分类