簡單做到窗體間(Form To Form)傳送變數 - 建立全域變數

分別建立Form 1和Form 2。

 

Form1 原碼

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. namespace GlobalData_FormToForm
  9. {
  10.     public struct Global            //全域變數
  11.     {
  12.         public static byte g_Counter;
  13.     }
  14.     public partial class Form1 : Form
  15.     {
  16.         public Form1()
  17.         {
  18.             InitializeComponent();
  19.         }
  20.         private void Form1_Load(object sender, EventArgs e)
  21.         {
  22.             Global.g_Counter = 0;
  23.         }
  24.         private void button1_Click(object sender, EventArgs e)
  25.         {
  26.             Global.g_Counter++;
  27.             lbl_Msg.Text = Global.g_Counter.ToString();
  28.         }
  29.         private void button2_Click(object sender, EventArgs e)
  30.         {
  31.             Form2 frm_2 = new Form2();
  32.             frm_2.Tag = this;
  33.             frm_2.Show();
  34.         }
  35.     }
  36. }

Form2 原碼

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. namespace GlobalData_FormToForm
  9. {
  10.     public partial class Form2 : Form
  11.     {
  12.         public Form2()
  13.         {
  14.             InitializeComponent();
  15.             Form1 frm_1 = (Form1)this.Tag;   
  16.         }
  17.         private void button1_Click(object sender, EventArgs e)
  18.         {
  19.             Global.g_Counter++;
  20.             lbl_Msg.Text = Global.g_Counter.ToString();
  21.         }
  22.     }
  23. }

好了,現在可以按Form1 button 和 Form2 button,變數值相互累加,是不是很簡單 ^-^

重要的地方就是this.Tag 及 public struct Globa 不能放在class Form裡哦~~

 

執行結果:

 

3個Form也行,再多也無所謂。

 

建立Form 3(原碼和Form2 一樣)

  1. using System.Data;
  2. using System.Drawing;
  3. using System.Text;
  4. using System.Windows.Forms;
  5. namespace GlobalData_FormToForm
  6. {
  7.     public partial class Form3 : Form
  8.     {
  9.         public Form3()
  10.         {
  11.             InitializeComponent();
  12.             Form1 frm_1 = (Form1)this.Tag;
  13.         }
  14.         private void button1_Click(object sender, EventArgs e)
  15.         {
  16.             Global.g_Counter++;
  17.             lbl_Msg.Text = Global.g_Counter.ToString();
  18.         }
  19.     }
  20. }

Form 1稍做修改

  1.         private void button2_Click(object sender, EventArgs e)
  2.         {
  3.             Form2 frm_2 = new Form2();
  4.             frm_2.Tag = this;
  5.             frm_2.Show();
  6.         }

改成

  1.         private void button2_Click(object sender, EventArgs e)
  2.         {
  3.             Form2 frm_2 = new Form2();
  4.             frm_2.Tag = this;
  5.             frm_2.Show();
  6.             
  7.             Form3 frm_3 = new Form3();
    •             frm_3.Tag = this;
    •             frm_3.Show();
    •         }

執行結果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值