Easily master data interactions between Windows Forms in. NET (iii)

Source: Internet
Author: User
Tags bool static class trim
window| Interactive | data

Easily master data interactions between Windows Forms in. NET (iii)

Zhzuo (Autumn Maple)

In the first and second articles we implemented data interaction using constructors, attributes, and methods with parameters, followed by the use of static classes to complete data interaction between forms. This is also a kind of data interaction method that we often use.

Three Using static Classes

The following is a defined class:

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;

}

}

}

It contains a static class member, Listdata, and a static constructor (Appdatas) to initialize the Listdata data. There is also a static property Listdata and a static Getlistdata () method, they achieve the same function is to return to Listdata.

Since the previous two articles have been talked about a lot, here is not to elaborate, the following is the complete code:

Form1.cs file

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 = "Modify";

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 file

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 = "OK";

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 = "Increase";

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 = "Delete";

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 ("Please enter the added content!");



}

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 ("Please select Delete item!");

}

}

}

In summary, I think the more static analogy is to load the application's configuration file into a static class, let all forms and other instances use this data through static properties as well as static methods, such as a three-tier structure or a multi-tier structure that can be accessed instead of being passed across multiple instances. Here we are talking about Windows Forms, and in fact interacting data between two different instances can be implemented in the scenario of three articles, unless it is a specific attribute or method of this class. Now all finished, although not what advanced things, but hope to some beginners to help, if can really solve some of the practical problems of friends, but also I did not waste time to write articles, but also welcome friends to carry out technical exchanges, and jointly improve, My email address is zhzuocn@163.com.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.