静态类和窗体传值

Posted mCat

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了静态类和窗体传值相关的知识,希望对你有一定的参考价值。

静态类StaticData.cs

技术分享图片
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 窗体传值
{
    public class StaticData
    {
        string _name;

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        int _age;

        public int Age
        {
            get { return _age; }
            set { _age = value; }
        }

        public void SayHi()
        {
            MessageBox.Show("Hi");
        }

        public void Dispose()
        {

        }
    }
}
View Code

一般类DynaData.cs

技术分享图片
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 窗体传值
{
    public class Dynadata
    {
        string _name;

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
        int age;

        public int Age
        {
            get { return age; }
            set { age = value; }
        }

        public void sayHi()
        {
            MessageBox.Show("Dynamic Hi");
        }

        public void Dispose()
        {

        }
    }
}
View Code

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 窗体传值
{
    static class Program
    {
        public static StaticData _staticData;
        
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            
            _staticData = new StaticData();

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());

            Dispose();
            
        }

        public static void Dispose()
        {
            _staticData.SayHi();//此时_data的值为Form1中两个userControl改变后的值
            _staticData.Dispose();

        }
    }
}

Form1.cs

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 窗体传值
{
    public partial class Form1 : Form
    {
        public StaticData _staticData;
        //
        public Dynadata _dynaData = new Dynadata();
        
        public Form1()
        {
            _dynaData.Name = "dyna cat ";
            _dynaData.Age = 1;

            InitializeComponent();
            this.userControl11 = new UserControl1(ref _dynaData);
            this.userControl21 = new UserControl2(ref _dynaData);
            _staticData = Program._staticData;
            _staticData.Name = string.Empty;
            _staticData.Age = 0;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = _staticData.Name + "\n";
            textBox1.Text += _staticData.Age + "\n";
            _staticData.SayHi();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox2.Text = _dynaData.Name + "\n";
            textBox2.Text += _dynaData.Age + "\n";
            _dynaData.sayHi();
        }
    }
}

userControl1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace 窗体传值
{
    public partial class UserControl1 : UserControl
    {
        public StaticData _staticData;
        //此处需要初始化,否则button1_Click(object sender, EventArgs e)出错
        public Dynadata _dynaData = new Dynadata();
        public UserControl1(ref Dynadata dynaData)
        {
            InitializeComponent();
            _staticData = Program._staticData;
            _dynaData = dynaData;
        }

        public UserControl1()
        {
            InitializeComponent();
            _staticData = Program._staticData;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            _staticData.Name = "static cat ";
            _dynaData.Name = "dynamic cat";
            
        }
    }
}

userControl2.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 窗体传值
{
    public partial class UserControl2 : UserControl
    {
        public StaticData _staticData;
        public Dynadata _dynaData = new Dynadata();
        public UserControl2(ref Dynadata dynaData)
        {
            InitializeComponent();
            _staticData = Program._staticData;
            _dynaData = dynaData;
        }

        public UserControl2()
        {
            InitializeComponent();
            _staticData = Program._staticData;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            _staticData.Age = 10;
            _dynaData.Age = 11;
        }
    }
}

程序可以有效更改静态类成员的值,userControl中更改的值也会在Program中更新,但是对于动态类,则与预期不符合

以上是关于静态类和窗体传值的主要内容,如果未能解决你的问题,请参考以下文章

片段内部静态类和gradle问题

WinForm窗体间传值的方法

winform窗体内函数调用

Winform 下,两个不同窗体的DataGridView控件中的数据间传值怎么解决?

C#-一种窗体之间传值的方法

web窗体页面之间的传值