Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have multiple checkbox control & multiple textbox control . I have a textbox which is by default is enable false. when the checkbox is checked, the textbox will enable true and let user input in c#. this will done in separate checkbox checked event but i wan't it this event in one function for multiple checkbox.. i tried to but this is wrong


What I have tried:

public void chkboxinfo()
       {
           try
           {

               object item=0;
               int noOfInputOutputParameter = 80;
             for (int i = 0; i <= noOfInputOutputParameter; i++)
                   {

                       string IOName = "lblX" + i;
                        TextBox  textName = (TextBox)item ;
                        CheckBox cb = new CheckBox();


                       //if (textName.Content.ToString() == "1")
                           if (cb.Checked.ToString() == "1")
                       {

                           Controls.Find(IOName, true).IsReadOnly.ToString();

                       }
                           else if (cb.CheckState.ToString() == "0")
                       {
                           Controls.Find(IOName, false).FirstOrDefault().Enabled.ToString();
                       }
                   }

               }

           catch (Exception ex)
           {
               MessageBox.Show(ex.Message, "chkboxinfo,frmSetting", MessageBoxButtons.OK, MessageBoxIcon.Error);
   }
           }
Posted
Updated 7-Dec-20 19:15pm
v3

I suggest you create a Dictionary<CheckBox, TextBox>
private Dictionary<CheckBox, TextBox> CbxTpTBx;

// initialize in Form Load Event
CbxTpTBx = new Dictionary<CheckBox, TextBox>
{
    {checkBox1, textBox1}, {checkBox2, textBox2}, {checkBox3, textBox3}
};
... then:

1) at design time:

a) select all the CheckBoxes on your form or container control

b) open the Properties browser (F4) and select/double-click the _CheckStateChanged Event to write the event handler in your form

c) edit the event handler so it looks something like this
private CheckBox currentBox;

private void checkBox3_CheckStateChanged(object sender, EventArgs e)
{
    currentBox = sender as CheckBox;

    CbxTpTBx[currentBox].Enabled = currentBox.Checked;
}
 
Share this answer
 
Comments
Member 11893460 7-Dec-20 23:48pm    
thanks its working
The CheckBox.Checked property[^] is a bool - so calling ToString on it will return either "True" or "False", but never "1".
So your tests alway fail:
C#
if (chkBox.Checked.ToString() == "1")// If checkBox is checked True
C#
else if (chkBox.CheckState.ToString() == "0")
and nothing happens.

Use the debugger: put a breakpoint on the first line of that method, and single step through your code: you will see what I mean!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900