0

Yesterday, I asked a similar question and people told me to use a List and a controller object. I changed my code accordingly now added a MyContols Class and a List in order to control my items in a better way.

This is my class which hold my checkbox and textboxes:

    private List<MyControls> _myControls = new List<MyControls>();

    class MyControls
      {
    int x=5;
    int y=30; 
    public CheckBox cb = new CheckBox();
    public TextBox tb1 = new TextBox();

    public TextBox tbSpecs = new TextBox();
    public TextBox tb3 = new TextBox();
    public TextBox tb4 = new TextBox();



    public void initElements(String name)
    {
        cb.Width = 10;
        cb.Height = 10;
        cb.Name = "cb_" + name;
        cb.Location = new Point(x, y+5);
        Form.ActiveForm.Controls.Add(cb);
        x += 15;

        tb1.Width = 50;
        tb1.Height = 20;
        tb1.Location = new Point(x, y);
        tb1.Name = "tb1_" + name;
        Form.ActiveForm.Controls.Add(tb1);
        x += 60;

        tbSpecs.Width = 150;
        tbSpecs.Height = 20;
        tbSpecs.Name = "tb2_" + name;
        tbSpecs.Location = new Point(x, y);
        Form.ActiveForm.Controls.Add(tbSpecs);
        x += 160;

        tb3.Width = 40;
        tb3.Height = 20;
        tb3.Name = "tb3_" + name;
        tb3.Location = new Point(x, y);
        Form.ActiveForm.Controls.Add(tb3);
        x += 50; 

        tb4.Width = 450;
        tb4.Height = 20;
        tb4.Name = "tb4_" + name;
        tb4.Location = new Point(x, y);
        Form.ActiveForm.Controls.Add(tb4);

        x = 0;
    }

    public int SetX(int i)
    {
        x = i;
        return x;
    }

    public int SetY(int Y)
    {
        y = Y;
        return y;
    }

}

also I have a ProductForm Class which is a second from in my application which let me view data in textboxes and edit and delete these data.

My problem is how can I delete a row?

My Second question is for each row do I have to create a new instance of my MyControl class? Probably my questions are very simple. Sorry for taking your time guys!

This function is just for test purposes because I am still trying to add an delete my rows.

        public void CreateFormElements()
        {
        ProductForm form2 = new ProductForm();
        form2.Visible = true;
        form2.Activate();

        MyControls mc = new MyControls();

        _myControls.Add(mc);
        _myControls[0].initElements("1");
        mc = new MyControls();
        _myControls.Add(mc);

        mc.SetY(55);
        _myControls[1].initElements("2");



    }

How can I delete an entire row if the checkbox corresponding is selected?

can you tell me a code which can do that?

1 Answer 1

1

In your Init, add a handler for cb.Checked event and then delete the row.

In Init:

 cb.CheckedChanged += cb_CheckedChanged;

Handler - not sure of the exact implementation in your case but here is some rough code:

private void cb_CheckedChanged(Object sender, EventArgs e) {
   string NameSet  = (sender as CheckBox).Name.Split(new char[]{'_'})[1];
   Form.ActiveForm.Controls.Remove("ch_" + NameSet);
   Form.ActiveForm.Controls.Remove("tb1_" + NameSet);
   Form.ActiveForm.Controls.Remove("tb2_" + NameSet);
   Form.ActiveForm.Controls.Remove("tb3_" + NameSet);
   Form.ActiveForm.Controls.Remove("tb4_" + NameSet);


}
Sign up to request clarification or add additional context in comments.

4 Comments

can you write me a code how to add a cb.Checked and delete row code ??
I am getting error for Remove Method. It says cannot conver string to System.Windows.Forms.Control
You'll likely need to do something like Form.ActiveForm.Controls.Remove(Form.ActiveForm.Controls.FindControl("ch_" + NameSet));
Joe finally I prepared my code :) Many thanks for your great help and assistance. Now I am heading towards the next step in my code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.