C# Windows窗体间的数据交互的几种方法(三)

来源:http://blog.sina.com.cn/s/blog_8d9200eb01011veh.html
Windows  窗体是用于 Microsoft Windows 应用程序开发的、基于 .NET Framework 的新平台。此框架提供一个有条理的、面向对象的、可扩展的类集,它使您得以开发丰富的 Windows 应用程序。一个Windows窗体就代表了.NET架构里的System.Windows.Forms.Form类的一个实例。
笔者在CSDN技术论坛.NET板块下的C#分类经常看到有人问起如何在两个Form间传递数据,访问修改对方窗体里面的值。对于有经验的程序员来说不是什么高深的东西,而对于初学者来说这些基础的东西往往是一个问题,并且存在这种现象,往往比较复杂的东西他们会,要用什么了就去学什么,实际上并没有真正的去理解掌握它,基础不扎实,所以就有了想通过自己对窗体编程积累的经验来写一些这方面的文章,以供学.NET的朋友参考,也借此机会同各位朋友进行交流,写得不合理的地方请各位朋友提宝贵意见,下面我分了三个部分来讲。

三.使用静态类
     在第一节和第二节中我们使用带参数的构造函数、属性以及方法实现了数据的交互,接下来要讲的是使用静态类来完成窗体间的数据交互。这个也是我们经常要用到的一种数据交互方法。
 
下面是定义的一个类:
using  System;
using  System.Collections;
namespace  ZZ
{
    public class AppDatas
    {
        // 静态数据成员
        private static ArrayList listData;
        // 静态构造函数
        static AppDatas()
        {
            listData = new ArrayList();
            listData.Add("DotNet");
            listData.Add("C#");
            listData.Add("Asp.net");
            listData.Add("WebService");
            listData.Add("XML");
        }
        // 静态属性
        public static ArrayList ListData
        {
            get{return listData;}
        }
        // 静态方法
        public static ArrayList GetListData()
        {
            return listData;
        }
    }
}
上面包含了一个静态类成员,listData,一个静态构造函数static AppDatas(),用来初始化listData的数据。还有一个静态属性ListData和一个静态GetListData()方法,他们实现了同样的功能就是返回listData。
另外两个窗体没有改变布局可参看前两篇文章还是如下:

由于前面两篇文章已经讲了很多,这个不细说了,下面是完整的代码:
Form1.cs 文件
using  System;
using  System.Drawing;
using  System.Collections;
using  System.ComponentModel;
using  System.Windows.Forms;
 namespace  ZZ
{
    public class Form1 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.Button buttonEdit;
        private System.Windows.Forms.ListBox listBoxFrm1;
        private System.ComponentModel.Container components = null;
        public Form1()
        {
            InitializeComponent();
            this.listBoxFrm1.DataSource = AppDatas.ListData;
           
        }
       
 
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if(components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }
 
        [STAThread]
        static void Main()
        {
            Application.Run(new Form1());
        }
 
        #region  Windows  窗体设计器生成的代码
        private void InitializeComponent()
        {
            this.buttonEdit = new System.Windows.Forms.Button();
            this.listBoxFrm1 = new System.Windows.Forms.ListBox();
            this.SuspendLayout();
            //
            // buttonEdit
            //
            this.buttonEdit.Location = new System.Drawing.Point(128, 108);
            this.buttonEdit.Name = "buttonEdit";
            this.buttonEdit.TabIndex = 1;
            this.buttonEdit.Text = " 修改";
            this.buttonEdit.Click += new System.EventHandler(this.buttonEdit_Click);
            //
            // listBoxFrm1
            //
            this.listBoxFrm1.ItemHeight = 12;
            this.listBoxFrm1.Location = new System.Drawing.Point(12, 8);
            this.listBoxFrm1.Name = "listBoxFrm1";
            this.listBoxFrm1.Size = new System.Drawing.Size(108, 124);
            this.listBoxFrm1.TabIndex = 2;
            //
            // Form1
            //
            this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
            this.ClientSize = new System.Drawing.Size(208, 141);
            this.Controls.Add(this.listBoxFrm1);
            this.Controls.Add(this.buttonEdit);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
        }
        #endregion
        private void buttonEdit_Click(object sender, System.EventArgs e)
        {
            Form2 formChild = new Form2();
            formChild.ShowDialog();
            this.listBoxFrm1.DataSource = null;
            this.listBoxFrm1.DataSource = AppDatas.ListData;
        }
    }
}
 
Form2.cs 文件
using  System.Drawing;
using  System.Collections;
using  System.ComponentModel;
using  System.Windows.Forms;
namespace  ZZ
{
    public class Form2 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.Button buttonOK;
        private System.ComponentModel.Container components = null;
        private System.Windows.Forms.ListBox listBoxFrm2;
        private System.Windows.Forms.Button buttonAdd;
        private System.Windows.Forms.Button buttonDel;
        private System.Windows.Forms.TextBox textBoxAdd;
 
        public Form2()
        {
            InitializeComponent();
            foreach(object o in AppDatas.ListData)
                this.listBoxFrm2.Items.Add(o);
        }
 
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if(components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }
        #region  Windows  窗体设计器生成的代码
        private void InitializeComponent()
        {
            this.buttonOK = new System.Windows.Forms.Button();
            this.listBoxFrm2 = new System.Windows.Forms.ListBox();
            this.buttonAdd = new System.Windows.Forms.Button();
            this.buttonDel = new System.Windows.Forms.Button();
            this.textBoxAdd = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            //
            // buttonOK
            //
            this.buttonOK.Location = new System.Drawing.Point(188, 108);
            this.buttonOK.Name = "buttonOK";
            this.buttonOK.TabIndex = 0;
            this.buttonOK.Text = " 确定";
            this.buttonOK.Click += new System.EventHandler(this.buttonOK_Click);
            //
            // listBoxFrm2
            //
            this.listBoxFrm2.ItemHeight = 12;
            this.listBoxFrm2.Location = new System.Drawing.Point(8, 8);
            this.listBoxFrm2.Name = "listBoxFrm2";
            this.listBoxFrm2.Size = new System.Drawing.Size(168, 124);
            this.listBoxFrm2.TabIndex = 2;
            //
            // buttonAdd
            //
            this.buttonAdd.Location = new System.Drawing.Point(188, 44);
            this.buttonAdd.Name = "buttonAdd";
            this.buttonAdd.TabIndex = 3;
            this.buttonAdd.Text = " 增加";
            this.buttonAdd.Click += new System.EventHandler(this.buttonAdd_Click);
            //
            // buttonDel
            //
            this.buttonDel.Location = new System.Drawing.Point(188, 76);
            this.buttonDel.Name = "buttonDel";
            this.buttonDel.TabIndex = 4;
            this.buttonDel.Text = " 删除";
            this.buttonDel.Click += new System.EventHandler(this.buttonDel_Click);
            //
            // textBoxAdd
            //
            this.textBoxAdd.Location = new System.Drawing.Point(188, 12);
            this.textBoxAdd.Name = "textBoxAdd";
            this.textBoxAdd.Size = new System.Drawing.Size(76, 21);
            this.textBoxAdd.TabIndex = 5;
            this.textBoxAdd.Text = "";
            //
            // Form2
            //
            this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
            this.ClientSize = new System.Drawing.Size(272, 141);
            this.Controls.Add(this.textBoxAdd);
            this.Controls.Add(this.buttonDel);
            this.Controls.Add(this.buttonAdd);
            this.Controls.Add(this.listBoxFrm2);
            this.Controls.Add(this.buttonOK);
            this.Name = "Form2";
            this.Text = "Form2";
            this.ResumeLayout(false);
 
        }
        #endregion
 
        private void buttonOK_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }
        private void buttonAdd_Click(object sender, System.EventArgs e)
        {
           
            if(this.textBoxAdd.Text.Trim().Length>0)
            {
                 AppDatas.ListData.Add(this.textBoxAdd.Text.Trim());
                this.listBoxFrm2.Items.Add(this.textBoxAdd.Text.Trim());
            }
            else
                MessageBox.Show(" 请输入添加的内容!");
           
        }
        private void buttonDel_Click(object sender, System.EventArgs e)
        {
            int index = this.listBoxFrm2.SelectedIndex;
            if(index!=-1)
            {
                 AppDatas.ListData.RemoveAt(index);
                this.listBoxFrm2.Items.RemoveAt(index);
               
            }
            else
                MessageBox.Show(" 请选择删除项!");
        }
    }
}
调试可以看到实现了同样的功能。
 
     总结,笔者认为使用静态类比较多的地方就是把应用程序的配置文件装载到一个静态类里面,让所有的窗体和其他实例都可以通过静态属性以及静态方法使用这些数据,比如三层结构或多层结构都可以访问它,而不是在多个实例间传来传去。在这里我们讨论的是Windows窗体,其实在两个不同的实例间交互数据,都可以采用文章中的方案实现,除非是这个类特有的属性或着方法。

你可能感兴趣的:(C#)