Forum: PC-Programmierung C# Steuerelemt-Array zur Laufzeit erstellen


von alekto (Gast)


Lesenswert?

Hallo,

da ich noch nicht so tief im Thema stecke, hoffe ich, Ihr könnt mir 
helfen:

Ich brauche ein paar Steuerelemnte, die sich alle gleichen und die nur 
temporär erstellt werden sollen. Vom Prinzip so:



private void cmdGetD_Click(object sender, EventArgs e) {
        System.Windows.Forms.TextBox textBox3;
        System.Windows.Forms.TextBox textBox4;

        textBox3 = new System.Windows.Forms.TextBox();
        textBox4 = new System.Windows.Forms.TextBox();

        this.panel1.Controls.Add(textBox3);
        this.panel1.Controls.Add(textBox4);
        textBox3.Font = new System.Drawing.Font("Microsoft Sans Serif", 
7.8F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 
((byte)(0)));
        textBox3.Location = new System.Drawing.Point(36, 80);
        textBox3.Name = "textBox3";
        textBox3.Size = new System.Drawing.Size(90, 22);
        textBox3.TabIndex = 0;
        textBox4.Font = new System.Drawing.Font("Microsoft Sans Serif", 
7.8F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 
((byte)(0)));
        textBox4.Location = new System.Drawing.Point(36, 110);
        textBox4.Name = "textBox4";
        textBox4.Size = new System.Drawing.Size(90, 22);
        textBox4.TabIndex = 1;

}

Nun sollen das aber etwas mehr als 2 sein und deshalb hätte ich das 
ganze gern als ein Array, damit ich die per for-loop oder so generieren 
(und später auch ansprechen) kann.
Aber irgendwie krieg ich kein Steuerelemt-Array hin, krieg - egal, wie 
ichs versuche - immer Fehlermeldungen.

Kann mir jemand sagen, wie das dann funktionierend aussehen muß?

Danke im Voraus!

von Hansi K. (Gast)


Lesenswert?

Damit wird ein Array aus 4 Textboxen erstellt:

public partial class Form1 : Form
    {
        TextBox[] tA;
        public Form1()
        {
            tA = new TextBox[4];
            for (int i = 0; i < 4; i++)
            {
                tA[i] = new System.Windows.Forms.TextBox();
                tA[i].Location = new System.Drawing.Point(10,10+ i* 50);
                tA[i].Size = new System.Drawing.Size(100, 20);
                tA[i].TabIndex = 0;
                Controls.Add(tA[i]);
            }
            InitializeComponent();
        }
    }

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.