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

前端 未结 5 1372
故里飘歌
故里飘歌 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:40

    Not exactly like VB6, but it is quite easy to write the code your self in c#.

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

    It typically looks like this

    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 that code out and paste in a method instead, returning the 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 like this

    List

提交回复
热议问题