How do I make a Control Array in C# 2010.NET?

前端 未结 5 1397
故里飘歌
故里飘歌 2020-12-07 03:19

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

In Visual Basic 6 there was an option to put how many control arrays you would like to use by changing the \"i

5条回答
  •  误落风尘
    2020-12-07 03:32

    In .NET you would create an array of controls, then you would instance a TextBox control for each element of the array, setting the properties of the control and positioning it on 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!";
    

    Unless your layout is more simplistic (i.e. rows and columns of textboxes) you may find using the designer to be easier, less time consuming and more maintainable.

提交回复
热议问题