D
D
de_iiah_teji9_iiace2014-12-19 21:06:12
Windows
de_iiah_teji9_iiace, 2014-12-19 21:06:12

How to dynamically create new buttons?

Here I have buttons, like a squeak:
11button
12button
13button
I press 1 button -> all the old buttons disappear and new ones appear, like
21button
22button
23button I press
some other one, again everything disappears and new ones appear, like
31button
32button
33button
may not clearly defined the question, but I think the essence is clear. So how is this to be implemented?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
cjey, 2014-12-19
@cjey

private static int n = 0;

        private void button1_Click(object sender, EventArgs e)
        {
            var newButton = new Button()
            {
                Name = "btn" + n,
                Text = n.ToString(),
                Location = new Point(10, 10 + n*30),
                Size = new Size(50, 25),
                TabIndex = 100 + n,
            };
            n++;
            this.Controls.Add(newButton);
        }

Created buttons can be stored in a static array. To remove use this.Controls.Remove(obj);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question