How to create a control array in C # 2010.NET?

I recently switched from Visual Basic 6 to C # 2010.NET.

In Visual Basic 6, you can now specify how many control arrays you would like to use by changing the "index" on it.

I am wondering if this is possible in C #, if so, how will I do it with a class, for example:

func fc = new func(); 

But with more than one array in fc is this possible?

And to clarify

Visual Basic 6, when you load a control, such as a text field or a user control, has the Index option in the properties window, and if you change it to 0, 1, etc., this will allow you to use all of these indexes. without loading multiple controls 50 times.

I think this may have something to do with the arraist, but I'm not quite sure.

Thanks for any help.

+4
source share
5 answers

This piece of code will not make you very far. Creating a control array is not a problem, just initialize it in the form constructor. You can then present it as a property, although this is usually a bad idea, since you do not want to reveal implementation details. Something like that:

 public partial class Form1 : Form { private TextBox[] textBoxes; public Form1() { InitializeComponent(); textBoxes = new TextBox[] { textBox1, textBox2, textBox3 }; } public ICollection<TextBox> TextBoxes { get { return textBoxes; } } } 

Which then allows you to write:

 var form = new Form1(); form.TextBoxes[0].Text = "hello"; form.Show(); 

But don’t, let the form manage its text fields.

+8
source

In .NET, you must create an array of controls, then you must specify a TextBox control for each element of the array, setting the properties of the control and placing it in the form:

  TextBox[] txtArray = new TextBox[500]; for (int i = 0; i < txtArray.length; i++) { // instance the control txtArray[i] = new TextBox(); // set some initial properties txtArray[i].Name = "txt" + i.ToString(); txtArray[i].Text = ""; // add to form Form1.Controls.Add(txtArray[i]); txtArray[i].Parent = Form1; // set position and size txtArray[i].Location = new Point(50, 50); txtArray[i].Size = new Size(200, 25); } . . . Form1.txt1.text = "Hello World!"; 

If your layout is more simplified (i.e. rows and columns of text fields), you can find, using the constructor, simpler, less time-consuming and more maintainable.

+2
source

Not like VB6, but pretty easy to write code for yourself in C #.

If you create a control such as Button in the designer, you can copy the code from the *.Designer.cs file

It usually looks like

 private System.Windows.Forms.Button button1; ... this.button1.Location = new System.Drawing.Point(40, 294); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 14; this.button1.Text = "Button1"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); ... this.Controls.Add(this.button1); 

Cut this code and paste it instead, returning a button

 private Button CreateButton() { private System.Windows.Forms.Button button1; this.button1.Location = new System.Drawing.Point(40, 294); // <-- change location for each this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 14; // <-- increase tab index or remove this line this.button1.Text = "Button1"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); this.Controls.Add(this.button1); return button; } 

then call this method as follows

 List<Button> buttons = new List<Button>(); for(int i = 0; i < 10; i++) { buttons.Add(CreateButton()); } 
+1
source

Based on the Trigo template:

Here is an example of processing a 2-dimensional textBox array
panel1 must be created using the designer
(I have Autoscroll = true, Size = 858; 525)

 public partial class Form1 : Form { TextBox[,] txtBoxArray = new TextBox[2,100]; public Form1() { InitializeComponent(); for (int i = 0; i < txtBoxArray.GetLength(0); i++) { for (int j = 0; j < txtBoxArray.GetLength(1); j++) { // instance the control txtBoxArray[i, j] = new TextBox(); // set some initial properties txtBoxArray[i, j].Name = "txtBox_" + i.ToString() + "_" + j.ToString(); txtBoxArray[i, j].Text = txtBoxArray[i, j].Name; //""; // add to form this.Controls.Add(txtBoxArray[i,j]); txtBoxArray[i, j].Parent = panel1; // set position and size txtBoxArray[i, j].Location = new Point(50+i*333, 50 + j * 25); txtBoxArray[i, j].Size = new Size(200, 25); } } } private void Form1_Load(object sender, EventArgs e) { } //... } 

it will look like this:> </a> </p></div></body> </html>

0
source

Source: https://habr.com/ru/post/1345249/


All Articles