【问题标题】:How do I make a Control Array in C# 2010.NET?如何在 C# 2010.NET 中制作控件数组?
【发布时间】:2011-03-25 16:31:27
【问题描述】:

我最近从 Visual Basic 6 迁移到 C# 2010 .NET。

在 Visual Basic 6 中,有一个选项可以通过更改其上的“索引”来放置您想要使用的控件数组的数量。

我想知道这在 C# 中是否可行,如果可以,我将如何使用如下类来实现:

func fc = new func();

但是 fc 中不止一个数组,这可能吗?

为了更清楚,

Visual Basic 6 当您加载文本框或用户控件之类的控件时,它在属性窗口中有一个“索引”选项,如果您将其更改为 0、1 等...它将允许您使用所有这些索引,而不需要加载多个控件 50 次。

我认为这可能与数组列表有关,但我不完全确定。

感谢您的帮助。

【问题讨论】:

  • .NET 不像 VB6 那样拥有控制数组。也没有简单的方法可以模仿它们。

标签: c# arrays controls


【解决方案1】:

该代码 sn-p 不会让您走得太远。创建控件数组没问题,在表单构造函数中初始化即可。然后,您可以将其作为属性公开,尽管这通常不是一个好主意,因为您不想公开实现细节。像这样的:

public partial class Form1 : Form {
    private TextBox[] textBoxes;

    public Form1() {
        InitializeComponent();
        textBoxes = new TextBox[] { textBox1, textBox2, textBox3 };
    }

    public ICollection<TextBox> TextBoxes {
        get { return textBoxes; }
    }
}

然后让你写:

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

但不要,让表单管理自己的文本框。

【讨论】:

  • 没有从stringTextBox 的转换,这不会编译。
  • 我必须将 ICollection 更改为 IList 才能使索引工作
【解决方案2】:

在 .NET 中,您将创建一个控件数组,然后为数组的每个元素实例化一个 TextBox 控件,设置控件的属性并将其定位在窗体上:

    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!";

除非您的布局更简单(即文本框的行和列),否则您可能会发现使用设计器更容易、更省时且更易于维护。

【讨论】:

    【解决方案3】:

    不完全像 VB6,但用 c# 编写代码很容易。

    如果您在设计器中创建一个控件,例如 Button,您可以从 *.Designer.cs 文件中复制代码

    通常看起来像这样

    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);
    

    剪下该代码并粘贴到一个方法中,返回按钮

    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;
    }
    

    然后像这样调用这个方法

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

    【讨论】:

      【解决方案4】:

      【讨论】:

        【解决方案5】:

        基于 Trigo 的模板:

        这里是一个处理二维文本框数组的示例
        panel1 必须由设计器创建
        (我有 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)
            {
        
            }
            //...
        
        
        }
        

        【讨论】:

          猜你喜欢
          • 2011-01-13
          • 1970-01-01
          • 2018-02-23
          • 1970-01-01
          • 1970-01-01
          • 2011-09-15
          • 2011-08-28
          • 2014-10-23
          • 2012-01-14
          相关资源
          最近更新 更多