Home > Software design >  Dynamically created controls names not increasing its value through loops C#
Dynamically created controls names not increasing its value through loops C#

Time:08-31

I'm working on a personal project where I'm building a notes generator, and I have this TextBox dynamically created everytimes a button is clicked, it works fine just like how I expected but things gets weird when I tried to name each of these TextBox to different name by using a loop Name = "Note" i where i is the loop variable.

So what I was expecting to happens is that each of the TextBox names to be something like Note1 Note2 Note3 ... but instead when I retrieved each of the TextBox name to a MessageBox in the same loop which is used to generates the TextBox, the MessageBox throws this: Note 1 Note 1 Note 1 ... instead when I clicked the button thrice.

int curr = 0;
private void guna2Button1_Click(object sender, EventArgs e) {
    int top = 25;
    int h_p = 170;
    curr  ;
    for(int i=1; i<curr 1; i  ) {
        // Notes
        var new_note = new Guna2TextBox() {
            Text = "Title\n",
            Name = "Note"   i,
            Multiline = true,
            AcceptsTab = true,
            AcceptsReturn = true,
            WordWrap = false,
            ScrollBars = ScrollBars.Vertical,
            Width = 220,
            Height = 110,
            BorderRadius = 8,
            Font = new Font("Bahnschrift", 13),
            ForeColor = Color.White,
            FillColor = ColorTranslator.FromHtml("#1E1E1E"),
            BorderColor = ColorTranslator.FromHtml("#2C2C2C"),
            Location = new Point(450,top)
        };

        MessageBox.Show(i.ToString());

        top  = h_p;
        flowLayoutPanel1.Controls.Add(new_note);
        curr = 0;
    }
}

CodePudding user response:

The problem is caused by the fact that inside the loop you set always curr back to zero. And the loop is not needed at all because you want to add a textbox at each click. So you just need to look at the Count property of the FlowLayoutPanel and use that value to prepare the name.
Another problem to solve is how to position the next control inside the panel but again you could calculate easily with a the Count property

private void guna2Button1_Click(object sender, EventArgs e) 
{
    int nextTop = 25   (flowLayoutPanel.Controls.Count * 170);
    var new_note = new Guna2TextBox() {
        Text = "Title\n",
        Name = "Note"   flowLayoutPanel.Controls.Count   1,
        .....
        Location = new Point(450,nextTop)
    };
    flowLayoutPanel1.Controls.Add(new_note);
}

CodePudding user response:

I don't think you need to use the For loop in this case. You can just use the variable curr for each textboxes. So instead of the For loop, you can use this :

int top = 25;
int h_p = 170;
curr  ;
var new_note = new Guna2TextBox() {
    Text = "Title\n",
    Name = "Note"   curr,
    Multiline = true,
    AcceptsTab = true,
    AcceptsReturn = true,
    WordWrap = false,
    ScrollBars = ScrollBars.Vertical,
    Width = 220,
    Height = 110,
    BorderRadius = 8,
    Font = new Font("Bahnschrift", 13),
    ForeColor = Color.White,
    FillColor = ColorTranslator.FromHtml("#1E1E1E"),
    BorderColor = ColorTranslator.FromHtml("#2C2C2C"),
    Location = new Point(450,top)
    
    top  = h_p;
    flowLayoutPanel1.Controls.Add(new_note);
};
  • Related