国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

.Net中掌握Windows窗體間數據交互(3)

2019-11-18 12:06:31
字體:
來源:轉載
供稿:網友

  在第一篇和第二篇文章中我們使用帶參數的構造函數、屬性以及方法實現了數據的交互,接下來要講的是使用靜態類來完成窗體間的數據交互。這個也是我們經常要用到的一種數據交互方法。
  
  三.使用靜態類
  
  下面是定義的一個類:
  
  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());
    }
    private void InitializeComponent()
    {
    this.buttonEdit = new System.Windows.Forms.Button();
    this.listBoxFrm1 = new System.Windows.Forms.ListBox();
    this.SuspendLayout();
    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);
    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;
    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);
    }
    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 );
    }
    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();
    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);
    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;
    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);
    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);
    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 = "";
    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);
    }
    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("請選擇刪除項!");
    }
  }
  }
  
  總結,

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 酉阳| 桐柏县| 阳泉市| 甘孜县| 赤水市| 金湖县| 莎车县| 桂阳县| 区。| 蓬莱市| 社会| 宜章县| 昌宁县| 延长县| 志丹县| 大悟县| 开原市| 昌图县| 吉首市| 新余市| 梧州市| 个旧市| 乌兰县| 大连市| 曲水县| 吐鲁番市| 桃园市| 台州市| 军事| 泰安市| 徐汇区| 蓝山县| 杭州市| 观塘区| 黔西县| 宁晋县| 闽清县| 公主岭市| 北流市| 红安县| 惠州市|