how to remove buttons created dynamically

Loyal

I want to create number of Buttons dynamically by giving the range in a TextBox.

The Problem is that when i enter the range e.g (3). It creates 3 buttons but then when i give the range less than the range given before e.g (2). it does not show me 2 buttons it shows me the previous 3 buttons. My code works for the range greater than the previous range but it fails when the new range is less than the previous range.

Here is my code:

private void button2_Click(object sender, EventArgs e)
{
    int number = int.Parse(textBox3.Text);
    Button[] textBoxes = new Button[number];
    int location = 136;

    for (int i = 0; i < textBoxes.Length; i++)
    {
        location += 81;
        var txt = new Button();
        textBoxes[i] = txt;
        txt.Name = "text" + i.ToString();
        txt.Text = "textBox" + i.ToString();
        txt.Location = new Point(location, 124);
        txt.Visible = true;
        this.Controls.Add(txt);
    }
}
BlackBear

You aren't removing the previous controls. You have to store them in an array then removing them before creating the new ones:

class Form1 : Form {
    private Button[] _textBoxes;

    private void button2_Click(object sender, EventArgs e) {
        int number = int.Parse(textBox3.Text);
        if(_textBoxes != null) {
            foreach(Button b in _textBoxes)
                this.Controls.Remove(b);
        }

        _textBoxes = new Button[number];
        int location = 136;

        for (int i = 0; i < textBoxes.Length; i++) {
            location += 81;
            var txt = new Button();
            _textBoxes[i] = txt;
            txt.Name = "text" + i.ToString();
            txt.Text = "textBox" + i.ToString();
            txt.Location = new Point(location, 124);
            txt.Visible = true;
            this.Controls.Add(txt);
        }
    }
}

I haven't tested this code but I hope you get the idea.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

how to kill zombie processes created by multiprocessing module?

来自分类Dev

How to merge two array object dynamically in typescript?

来自分类Dev

How to define User defined buttons in alert for Rhomobile Application

来自分类Dev

How to space vertically stacked buttons in Bootstrap 3

来自分类Dev

VB - How to dynamically add items on TOP of listview

来自分类Dev

How to remove an object in a JSON object?

来自分类Dev

Disable buttons created dynamically based on their name in wxPython

来自分类Dev

How to use selector when we swap on piano type buttons in android

来自分类Dev

how to make round buttons like the iOS 7 lock screen

来自分类Dev

How can I enable/disable a UITextField's inputAccessoryView buttons?

来自分类Dev

How to add a list to search field dynamically?

来自分类Dev

How to use a MOBAC created atlas with OpenLayers for an offline map?

来自分类Dev

How to include a html string dynamically

来自分类Dev

How to remove � from a String?

来自分类Dev

How to pass a object though function-created-button

来自分类Dev

Call controller function from HTML dynamically created

来自分类Dev

How to set and get tag in xaml dynamically?

来自分类Dev

How to remove apostrophe from text?

来自分类Dev

How to use a common fuction for all the buttons in VBScript?

来自分类Dev

How to switch focus between buttons in MacVim's 'file changed' popup

来自分类Dev

Rails - fields_for and javascript to dynamically add and remove fields in a form (railscasts ep. 196-197)

来自分类Dev

How to dynamically create regex to use in .match Javascript?

来自分类Dev

How to rename branch to remove capitalization?

来自分类Dev

How could the first programming language be created?

来自分类Dev

addEventListener does not work on dynamically created button

来自分类Dev

How to dynamically insert the list data into an html table

来自分类Dev

How to start jupyter in an environment created by conda?

来自分类Dev

How to remove percentage labels in a pie chart created using plot_ly in R

来自分类Dev

How to change buttons' size of MessageDlg?

Related 相关文章

  1. 1

    how to kill zombie processes created by multiprocessing module?

  2. 2

    How to merge two array object dynamically in typescript?

  3. 3

    How to define User defined buttons in alert for Rhomobile Application

  4. 4

    How to space vertically stacked buttons in Bootstrap 3

  5. 5

    VB - How to dynamically add items on TOP of listview

  6. 6

    How to remove an object in a JSON object?

  7. 7

    Disable buttons created dynamically based on their name in wxPython

  8. 8

    How to use selector when we swap on piano type buttons in android

  9. 9

    how to make round buttons like the iOS 7 lock screen

  10. 10

    How can I enable/disable a UITextField's inputAccessoryView buttons?

  11. 11

    How to add a list to search field dynamically?

  12. 12

    How to use a MOBAC created atlas with OpenLayers for an offline map?

  13. 13

    How to include a html string dynamically

  14. 14

    How to remove � from a String?

  15. 15

    How to pass a object though function-created-button

  16. 16

    Call controller function from HTML dynamically created

  17. 17

    How to set and get tag in xaml dynamically?

  18. 18

    How to remove apostrophe from text?

  19. 19

    How to use a common fuction for all the buttons in VBScript?

  20. 20

    How to switch focus between buttons in MacVim's 'file changed' popup

  21. 21

    Rails - fields_for and javascript to dynamically add and remove fields in a form (railscasts ep. 196-197)

  22. 22

    How to dynamically create regex to use in .match Javascript?

  23. 23

    How to rename branch to remove capitalization?

  24. 24

    How could the first programming language be created?

  25. 25

    addEventListener does not work on dynamically created button

  26. 26

    How to dynamically insert the list data into an html table

  27. 27

    How to start jupyter in an environment created by conda?

  28. 28

    How to remove percentage labels in a pie chart created using plot_ly in R

  29. 29

    How to change buttons' size of MessageDlg?

热门标签

归档