Add control array in .NET

zhaozj2021-02-16  51

Add control array

In .NET I don't seem to find a description of the array of controls, but I saw an article on how to implement an array of controls in .NET two days ago (please refer to MSDN). Remember when the university When using VB, an array is used, but it seems that the era of .NET does not seem to be. At that time, you can use a lot of cumbersome things to generate some features and a list of text boxes and a bunch of labels. These control arrays respond to the same event, we can access them directly through an index when using it. I use .NET as if it makes everything so simple, as if the control array is not used, but in front of the development I still accidentally think of using the array of controls, I don't know how to declare it. Net doesn't support this stuff. In fact, if we know the FCL, I think this is not difficult! I don't say it. Let's start our creation. This time we create Button array (also an example on MSDN. This article is from MSDN, if you want to know more details, please refer to MSDN help), first we With CollectionBase (please refer to http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemcollectionscollectionBaseClasstopic.asp), which provides an abstract stronger type Collection base class. We can use it to implement our control array. First we need to build a class inherited from CollectionBase, we call it ButtonArray. It is used to store Button's container. The following is the implementation code:

Namespace ButtonArrayProject {

Using system;

///

/// ButtonArray's summary description.

///

Public class buttonArray: system.collections.collectionBase {

// A reference to the container, reference to a reference to the form of the array of controls

Private readonly system.windows.forms.form Hostform;

// Add a new button

Public system.windows.Forms.Button AddNewButton () {

// Create a new button instance

System.windows.Forms.Button Abutton = new system.windows.Forms.Button ();

// Add the new control to the (this) list

This.List.Add (Abutton);

// Add controls to the control collection of the parent form

Hostform.controls.add (abutton);

/ / Set the properties of the control

Abutton.top = count * 25;

Abutton.Left = 100;

// Use the Button's TAG to represent the index in the control array

Abutton.tag = this.count;

Abutton.text = "Button" this.count.toString ();

// Add an event response

Abutton.Click = New System.EventHandler (ClickHandler);

/ / Return to the new control

Return Abutton;

}

/ / Delete the last element in the control array

Public void remove () {

// Test if there is a control in the array of controls

IF (this.count> 0) {

// If there is, first delete the last element from the parent container

// Note: This is accessed by indexing

This.hostform.Controls.remove (this [this.count-1]);

/ / Delete the last control in the control array

This.List.Removeat (this.count-1);

}

}

/ / Add an event coming out

Public void ClickHandler (Object Sender, System.EventArgs E) {

// Return to a message with MessageBox

System.windows.Forms.MessageBox.show ("You click"

(System.Windows.Forms.Button) .tag.tostring ());

}

// Texture function with the father form

Public ButtonArray (System.Windows.Forms.form Host) {

/ / Assignment for the reference of the parent

THIS.HOSTFORM = Host;

// Add a default button

This.AddnewButton ();

}

// The index of the array of controls can be accessed using index (only available)

Public system.windows.Forms.Button this [int index] {

Get {

Return (System.Windows.Forms.Button) this.list [index];

}

}

}

}

For test programs, we add a WinForm project. Putting two Button in Form1 in this project to add or delete controls in the control array. At the same time, we also need to declare a ButtonArray object in Form1. Structure function in Form1 In the case, the code is as follows:

Using system;

Using system.drawing;

Using system.collections;

Using system.componentmodel;

Using system.windows.forms;

Using system.data;

Namespace ButtonArrayProject

{

Public class form1: system.windows.forms.form {

Private buttonArray btnarray;

Private system.windows.Forms.Button btnadd;

Private system.windows.Forms.Button Btnremove;

Private system.componentmodel.Container Components = NULL;

Public form1 () {

InitializationComponent ();

THIS.BTNARRAY = New ButtonArray (this);

}

Protected Override Void Dispose (BOOL Disposing) {

IF (Disposing)

{

IF (Components! = NULL)

{

Components.dispose ();

}

}

Base.dispose (Disposing);

}

#Region Windows

Private vidinitiRizeComponent () {

This.btnadd = new system.windows.Forms.Button ();

This.btnremove = new system.windows.Forms.Button ();

THIS.SUSPENDLAYOUT ();

//

// btnadd

//

This.btnadd.location = new system.drawing.point (352, 136);

THIS.BTNADD.NAME = "btnadd";

this.btnadd.size = new system.drawing.size (112, 23);

this.btnadd.tabindex = 0;

this.btnadd.text = "addnewbutton";

This.btnadd.click = new system.eventhandler (this.btnadd_click); //

// btnremove

//

this.btnremove.location = new system.drawing.point (352, 192);

this.btnremove.name = "btnremove";

this.btnremove.size = new system.drawing.size (112, 23);

this.btnremove.tabindex = 1;

this.btnremove.text = "removebutton";

This.btnremove.click = new system.eventhandler (this.btnremove_click);

//

// Form1

//

THIS.AUTOSCALEBASESIZE = New System.drawing.size (6, 14);

This.ClientSize = new system.drawing.size (512, 381);

This.Controls.add (this.btnremove);

This.Controls.add (this.btnadd);

THIS.NAME = "Form1";

THIS.TEXT = "Form1";

This.ResumeLayout (false);

}

#ndregion

[Stathread]

Static void

Main

() {

Application.run (New Form1 ());

}

Private void btnadd_click (object sender, system.eventargs e) {

this.btnarray.addnewbutton ();

// This is an example of an index access

THIS.BTNARRAY [0]. backcolor = color.blue;

}

Private void btnremove_click (object sender, system.eventargs e) {

this.btnarray.remove ();

}

}

}

转载请注明原文地址:https://www.9cbs.com/read-23925.html

New Post(0)