Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have an add user button which adds a textbox and a button. I want it so that the new button removes the user it added. My problem is I don't know how to get a dynamically added button to delete dynamically created textboxes... I think its a problem with how I defined the variables but I don't know what . Here's what I have:

    private void AddUserbtn_Click_1(object sender, EventArgs e)
    {
        TextBox[] Alias = new TextBox[n];

        Button[] Remove = new Button[n];

        int AliasX, AliasY, RemoveX, RemoveY;

        AliasX = 40;
        AliasY = 45;

        RemoveX = 946;
        RemoveY = 45;


        for (int i = 0; i < n; i++)
        {
            Alias[i] = new TextBox();
            Alias[i].Size = new Size(233, 26);
            Alias[i].Location = new Point(AliasX, AliasY + space);
            Alias[i].Font = new Font("Arial", 10);

            Remove[i] = new Button();
            Remove[i].Location = new Point(RemoveX, RemoveY + space);
            Remove[i].Text = "";
            Remove[i].Font = new Font("Arial", 10);
            Remove[i].FlatStyle = FlatStyle.Flat;
            Remove[i].BackgroundImage =Properties.Resources.btn_remove_user;
            Remove[i].FlatAppearance.BorderColor = Color.White;
            Remove[i].BackgroundImageLayout = ImageLayout.Center;
            Remove[i].Size = new Size(95, 23);
            Remove[i].UseVisualStyleBackColor = true;
            Remove[i].Click += new EventHandler(Remove_Click);

            space += 35;
        }


        for (int i = 0; i < n; i++)
        {
            Panel.Controls.Add(Alias[i]);

        }

        //for(int i=0; i <n;i++)
        //Remove[i].Click += delegate
        //{
        //    Panel.Controls.Remove(Alias[i]);
        //};



    }

    private void Remove_Click(object sender, EventArgs e)
    {
        //    Button Remove = sender as Button;

        //    //TextBox[] Alias = new TextBox[n];
        //    //for (int i = 0; i <n; i++)
        //    //{
        //    //    Panel.Controls.Remove(Alias[i]);



        //    //}
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
156 views
Welcome To Ask or Share your Answers For Others

1 Answer

Give your objects meaningful names like:

Alias[i].Name = "UserTextBox" + i;
Remove[i].Name = "UserButton" + i;

This way you can find the object to be excluded.

Panel.Controls.Remove(Panel.Controls["UserTextBox" + i]);
Panel.Controls.Remove(Panel.Controls["UserButton" + i]);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...