Home > Back-end >  How do i create a class (other file) that will enable and Check my Checkboxes?
How do i create a class (other file) that will enable and Check my Checkboxes?

Time:07-28

I am new to C#.

I have written a code that makes the other Checkbox true and RichTextBox enable. (Successfully working fine).

Now I don't want to repeat this whole coding so I decided to create a class (this class is a new file - [like a function in JS] ) but I don't know how to execute it effectively.

The CheckBoxes and RichTextBoxes are all in my Main Form. So this Class will be called out in my Main Form.

below is my coding kindly correct my code.

Public Static String checkBox_RichtextBox(int x, string cb_name, string rtb_name){
   for (int i = x; i < 21; i  )
   {
      var cb_ctrl = Main_Form.Controls.Find("L_CB"   i, true).FirstOrDefault() as CheckBox;
      var rtb_ctrl = Main_Form.Controls.Find("textBox_L"   i, true).FirstOrDefault() as RichTextBox;
      if (i == x)
      {
        if (cb_ctrl != null)
        {
           return cb_ctrl.Checked = true;
           return rtb_ctrl.Enabled = true;
        }
        else if (i > 1)
        {
           return cb_ctrl.Checked = false;
           return rtb_ctrl.Enabled = false;
        }
      }
   }
}

CodePudding user response:

First create a new class and write the following code inside its body.

class YourClassName
{
    public static void checkBox_RichtextBox(Form form, int x)
    {
        CheckBox cb_ctrl = form.Controls.Find("L_CB"   x, true).FirstOrDefault() as CheckBox;
        var rtb_ctrl = form.Controls.Find("textBox_L"   x, true).FirstOrDefault() as RichTextBox;
        if (cb_ctrl != null)
        {
            cb_ctrl.Checked = true;
            rtb_ctrl.Enabled = true;
        }
    }
}

Now in the Main_Form you only have to pass the Main_Form object and CheckBox_RichTextBox group number like YourClassName.checkBox_RichtextBox(this, number);. The good thing is that you can now use it in any class now.

CodePudding user response:

You can create a class something like below

class Checkbox_RichTextBoxHandler
{
    public void checkBox_RichtextBox(int x, CheckBox cb_ctrl, RichTextBox rtb_ctrl)
    {
        if (cb_ctrl != null)
        {
            cb_ctrl.Checked = true;
            rtb_ctrl.Enabled = true;
        }
        else if (x > 1)
        {
            cb_ctrl.Checked = false;
            rtb_ctrl.Enabled = false;
        }
    }
}

Then in your function you can call like below

Public Static String checkBox_RichtextBox(int x, string cb_name, string rtb_name){
Checkbox_RichTextBoxHandler cbHandler;   
for (int i = x; i < 21; i  )
   {
      var cb_ctrl = Main_Form.Controls.Find("L_CB"   i, true).FirstOrDefault() as CheckBox;
      var rtb_ctrl = Main_Form.Controls.Find("textBox_L"   i, true).FirstOrDefault() as RichTextBox;
      if (i == x)
      {
        cbHandler.checkBox_RichtextBox(i, cb_ctrl, rtb_ctrl);
      }
   }
   //Return status string per your needs
}
  • Related