码迷,mamicode.com
首页 > Windows程序 > 详细

静态类和窗体传值

时间:2017-12-05 14:27:54      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:reading   event   eve   name   false   app   窗体   send   ati   

静态类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中更新,但是对于动态类,则与预期不符合

静态类和窗体传值

标签:reading   event   eve   name   false   app   窗体   send   ati   

原文地址:http://www.cnblogs.com/my-cat/p/7986524.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!