Home > OS >  Checkbox event on IEnumerable box
Checkbox event on IEnumerable box

Time:03-04

I'm making a c# windows form that from an IEnumerable list of item must create a checkbox and a Textbox for every item, and so far I hav made it without problem. Every textbox and every checkbox have a different name from a Name counter of the rotation.

I would like to make an event that if I uncheck for example the Chkbox1 make the txtbox1 not editable without know How many checkbox I can have.

I'm a little bit newbie in c#.

CodePudding user response:

Use a Dictionary to store the relationship between CheckBox and TextBox. A tag property can also do this, but I think it is ugly.

        //make the dict global, so can access in event handler
        private Dictionary<CheckBox, TextBox> chkTbRelations;

        private void Form1_Load(object sender, EventArgs e)
        {
            chkTbRelations = new Dictionary<CheckBox, TextBox>();
            //simulate your IEnumerable
            foreach (int i in Enumerable.Range(1, 5))
            {
                CheckBox chk = new CheckBox();
                TextBox tb = new TextBox();
                //add other properties such as Size, Location here
                // Controls.Add ...
                // tb.Tag = chk;
                chkTbRelations.Add(chk, tb);
                chk.CheckedChanged  = Chk_CheckedChanged;
            }
        }

        private void Chk_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox chk = sender as CheckBox;
            TextBox tb = chkTbRelations[chk];
            //tb = chk.Tag as TextBox;
            tb.Enabled = chk.Checked;
        }

CodePudding user response:

The solution of Ley Lang seams the better solution. I try to explain better: I need to make a massive revision of my database, on a single row I can find short value (code), mid value (short description), and very long value (full description). With a datagridview is more hard and looks owful to manage this. So after that I have get the list I want to circle it and create all the stuff (label, databox, checkbox....) for all the item if the list. With the checkbox I want to permit the editing of the line and when is the moment to save all the row, I skip the line with the checkbox not activated.

I hope it's more clear now

  • Related