Click here to Skip to main content
15,118,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to set textbox to readonly when checkbox checked?<pre> <pre lang="c#">
multiple checkbox control & multiple textbox control I have a textbox which is by default is read only. when the checkbox is checked, the textbox will readonly=false and let user input. anyway to do it? in c#.this will be done.in this way but i wan't create all event in one function.
CheckStateChanged event
private void stn1Bcd1Prnt_CheckStateChanged(object sender, EventArgs e)
        //{
        //    txSt1Bcd1Name.Enabled = (stn1Bcd1Prnt.CheckState == CheckState.Checked);
        //    txSt1Bcd1From.Enabled = (stn1Bcd1Prnt.CheckState == CheckState.Checked);
        //    txSt1Bcd1To.Enabled = (stn1Bcd1Prnt.CheckState == CheckState.Checked);
        //    txSt1Bcd1Name.Clear();
        //    txSt1Bcd1From.Clear();
        //    txSt1Bcd1To.Clear();
        //}


checkbox checked event
//txSt1Bcd1Name.ReadOnly = false;
           //txSt1Bcd1From.ReadOnly = false;
           //txSt1Bcd1To.ReadOnly = false;
           //txSt1Bcd1Name.Clear();
           //txSt1Bcd1From.Clear();
           //txSt1Bcd1To.Clear();


What I have tried:

private void chkboxinfo(object sender, EventArgs e)
       {

           try
           {
         int noOfInputOutputParameter = 80;
              for (int i = 0; i <= noOfInputOutputParameter; i++)
                 {
                    var textBox = sender as TextBox;
                     string IOName = "txtX";
                      CheckBox chkBox = (CheckBox)sender;
                    if (chkBox.Checked.ToString() == "1")// If checkBox is checked True
                   {
                       Controls.Find(IOName, true).IsReadOnly.ToString();

                       //textBox.ReadOnly = false;
                    }
                   else if (chkBox.CheckState.ToString() == "0")
                   {
                  //txSt7Bcd1Name.Enabled = (stn7Bcd1Prnt.CheckState == CheckState.Checked);

                       //textBox.Enabled = (chkBox.CheckState == CheckState.Checked);
                       Controls.Find(IOName, false).FirstOrDefault().Enabled.ToString();
                       textBox.Clear();
                       //textBox.ReadOnly = false;

                   }
               }
           }
            catch (Exception ex)
           {
               MessageBox.Show(ex.Message, "chkboxinfo,frmSetting", MessageBoxButtons.OK, MessageBoxIcon.Error);

           }
       }
Posted
Updated 30-Nov-20 21:45pm
Comments
Maciej Los 1-Dec-20 2:01am
   
What's wrong with your code?
Member 15008265 1-Dec-20 4:37am
   
actually i try.. I have a textbox which is by default is read only. when the checkbox is checked, the textbox will readonly=false and let user input.

1 solution

Your code is ... um ... odd.
Ignoring the commented out stuff - which will make it not compile so I assume you did it for presentation to us - you have lines that do nothing other than perhaps throw an exception:
C#
Controls.Find(IOName, true).IsReadOnly.ToString();
If it finds the control, it will convert it to "true" or "false" and then discard the string. If it doesn't, it'll throw a null reference exception.
This code:
C#
var textBox = sender as TextBox;
will always result in null since a CheckBox isn't derived from TextBox.
This line will always be false:
C#
if (chkBox.Checked.ToString() == "1")
As will this one:
C#
else if (chkBox.CheckState.ToString() == "0")
Since bool.ToStringwill always return a string "true" or "false" and never "0" or "1"
YOu have no code to cope with a if ... else if that doesn't match either: you need to use if ... else if ... else to check.
Your loop does nothing of any use since you don't use i inside it, and nothing else changes from iteration to iteration.
Try this:
C#
private void chkboxinfo(object sender, EventArgs e)
   {
   if (sender is CheckBox cb)
      {
      textBox.ReadOnly = cb.Checked == CheckState.Checked;
      }
   }
   
Comments
Member 15008265 1-Dec-20 4:34am
   
thanks
OriginalGriff 1-Dec-20 5:02am
   
You're welcome!

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