Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6644245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:08:42+00:00 2026-05-26T00:08:42+00:00

I am writing a code in C# I have 2 Forms and the code

  • 0

I am writing a code in C# I have 2 Forms and the code creates textboxes and corresponding checkboxes dynamically. The code I wrote creates dynamic textboxes and checkboxes successfully. However, I am not able to delete the row of textboxes in a selected checkbox line.

public void CreateTextBox(int i, StringReader sr)
        {
        ProductForm form2 = new ProductForm();
        _cb = new CheckBox[i];
        form2.Visible = true;
        form2.Activate();

        int x = 10;
        int y = 30;
        int width = 100;
        int height = 20;


        for (int n = 0; n < i; n++)
        {

            String line = sr.ReadLine();
            String[] line_ = line.Split(new char[] {'\t'});

            String cbName = "chkBox_" + n.ToString();



            _cb[n] = new CheckBox();
            _cb[n].Name = cbName;
            _cb[n].Location = new Point(2, y);
            _cb[n].Checked = false;
            form2.Controls.Add(_cb[n]);
            if (line.Length > 3)
            {

                for (int row = 0; row < 4; row++)
                {
                    String name = "txtBox_" + row.ToString();
                    TextBox tb = new TextBox();
                    tb.Name = name;
                    tb.Text = line_[row].ToString();
                    tb.Location = new Point(x, y);
                    tb.Height = height;
                    if (row == 1)
                    {
                        tb.Width = width * row;
                    }

                    if (row == 3)
                    {
                        tb.Width = width * 5;
                    }
                    else
                    {
                        tb.Width = width - 20;
                    }
                    x += 10 + width;
                    form2.Controls.Add(tb);

                }
            }
            x = 10;
            y += 25;

        }

    }

    private void DeleteTextBoxButton_Click(object sender, EventArgs e)
    {
        //Here should I add a code in order to delete dynamically created 
        //textboxes by clicking checkbox and button

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:08:43+00:00Added an answer on May 26, 2026 at 12:08 am

    Not sure of your question. But if I am right, this could do the trick.

    SOLUTION1: While creating all the controls, add them to a List<Controls>. When you are checking the checkbox to delete the row, get the name of the checkbox, search it in the List<Controls>. So this way can get the index of the row of the checkbox clicked. Now delete the controls of that rows.

    SOLUTION2: Create your controls in a TablelayoutPanel and everything will be easy.

    EDIT

    Copy paste everything in the form1, se btn_click as a event handler for a button. Let the size of the form a bit big. Everything should work fine now. If not working, let me know.

    class MyControl
    {
        public TextBox txt1 { get; set; }
        public TextBox txt2 { get; set; }
        public TextBox txt3 { get; set; }
        public TextBox txt4 { get; set; }
        public CheckBox cb { get; set; }
    
        public MyControl(TextBox txt1, TextBox txt2, TextBox txt3, TextBox txt4, CheckBox cb)
        {
            this.txt1 = txt1;
            this.txt2 = txt2;
            this.txt3 = txt3;
            this.txt4 = txt4;
            this.cb = cb;
        }
    
    }
    
    
        List<MyControl> list = new List<MyControl>();
        public int x = 50, n = 1;
        TextBox txtTemp, txt1, txt2, txt3, txt4;
        CheckBox cbTemp;
        private void btn_Click(object sender, EventArgs e)
        {
    
            txtTemp = new TextBox();
            txtTemp.Location = new System.Drawing.Point(10, x);
            txtTemp.Name = "txt1_" + n;
            txt1 = txtTemp;
    
            txtTemp = new TextBox();
            txtTemp.Location = new System.Drawing.Point(120, x);
            txtTemp.Name = "txt2_" + n;
            txt2 = txtTemp;
    
            txtTemp = new TextBox();
            txtTemp.Location = new System.Drawing.Point(230, x);
            txtTemp.Name = "txt3_" + n;
            txt3 = txtTemp;
    
            txtTemp = new TextBox();
            txtTemp.Location = new System.Drawing.Point(350, x);
            txtTemp.Name = "txt4_" + n;
            txt4 = txtTemp;
    
            cbTemp = new CheckBox();
            cbTemp.Name = "cb1_" + n;
            cbTemp.Enter += new EventHandler(cbTemp_Enter);
            cbTemp.Location = new System.Drawing.Point(490, x);
    
            this.Controls.Add(txt1);
            this.Controls.Add(txt2);
            this.Controls.Add(txt3);
            this.Controls.Add(txt4);
            this.Controls.Add(cbTemp);
    
            list.Add(new MyControl(txt1, txt2, txt3, txt4, cbTemp));
    
            x += 40;
            n++;
        }
    
        void cbTemp_Enter(object sender, EventArgs e)
        {
            if ((MessageBox.Show("Are you Sure?", "Warning", MessageBoxButtons.YesNo)) == DialogResult.No)
                return;
    
            CheckBox cbMain = (CheckBox)sender;
            int index = Search(cbMain);
    
            this.Controls.Remove(list[index].txt1);
            this.Controls.Remove(list[index].txt2);
            this.Controls.Remove(list[index].txt3);
            this.Controls.Remove(list[index].txt4);
            this.Controls.Remove(list[index].cb);
        }
    
        private int Search(CheckBox cbMain)
        {
            int temp = 0;
            foreach (MyControl item in list)
            {
                if(cbMain.Name == item.cb.Name)
                    temp = list.IndexOf(item);
            }
            return temp;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been writing some code that creates a generic blog. Its features are
I am writing code in VS2005 using its STL. I have one UI thread
Does anyone have some good hints for writing test code for database-backend development where
I have always made a point of writing nice code comments for classes and
I am writing unit tests for some of my code and have run into
I have some code like this in a winforms app I was writing to
I always have this notion that writing SQL queries in the code behind is
I have been writing unit tests using NUnit and Moq with my Silverlight code
I have a dev, that will get around our code coverage by writing tests
I'm writing a web page in ASP.NET. I have some JavaScript code, and I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.