erfan
erfan

Reputation: 57

how to disable multiple textboxes from another form?

I have this combo box in Form1 and I need to disable some of the textboxes when the combo box value changes and there is a button to go to that form which its textboxes are getting disabled.

how can I do what I want without both of the forms showing at the same time?!

Here is the Code:

public partial class Form1 : Form
{
    internal Grading_Section grading;
    internal TextBox te;
    public Form1()
    {
        InitializeComponent();
        grading = new Grading_Section();
        grading.Show();
        te = TxtAddress;
    }
    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        FindControl fc = new FindControl();
        Grading_Section gr = new Grading_Section();
        if (comboBox1.SelectedItem == "MSC")
        {
            ((TextBox)fc.Ctrl(fc.TheForm("Grading_Section"), "textBox1")).Enabled = false;
            ((TextBox)fc.Ctrl(fc.TheForm("Grading_Section"), "textBox2")).Enabled = false;
            ((TextBox)fc.Ctrl(fc.TheForm("Grading_Section"), "textBox3")).Enabled = false;

        }
        else if (comboBox1.SelectedItem == "CSP")
        {
            ((TextBox)fc.Ctrl(fc.TheForm("Grading_Section"), "textBox1")).Enabled = true;
            ((TextBox)fc.Ctrl(fc.TheForm("Grading_Section"), "textBox2")).Enabled = true;
            ((TextBox)fc.Ctrl(fc.TheForm("Grading_Section"), "textBox3")).Enabled = true;
        }

    }
    private void BtnNext_Click(object sender, EventArgs e)
    {
        this.Visible = false;
        Grading_Section g = new Grading_Section();
        g.Show();    
    }

And this is my FindControl class:

 public class FindControl
    {
        Control c = null;
        Control f = null;
        public FindControl()
        {
        }
        public Control TheForm(string name)
        {
            FormCollection fc = Application.OpenForms;

            //---------------------------------------------------------------------------------
            for (int i = 0; i < fc.Count; i++)
            {

                c = null;
                if (fc[i].Name == name)
                {
                    f = fc[i];
                    break;
                }
            }
            return ((Control)f);
        }

        //---------------------------------------------------------------------------------
        public Control Ctrl(Control f, string name)
        {
           * for (int i = 0; i < f.Controls.Count; i++)*
            {
                if (f.Controls[i].Name == name)
                {
                    c = f.Controls[i];
                    break;
                }
                if (c == null)
                {
                    if (f.Controls[i].Controls.Count > 0)
                        Ctrl(f.Controls[i], name);
                }
                if (c != null)
                    break;
            }
            return (c);
        }
    }

So far I find this code but the only way that it works is to show both of the forms at the same time otherwise it shows this error:

when I delete this line the error pop up:"grading.Show();

"NullReference Exception was unhandled,An unhandled exception of type 'System.NullReferenceException' occurred in WindowsFormsApplication2.exe"

The Error accrued from this line:

Upvotes: 0

Views: 497

Answers (1)

Coskun Ozogul
Coskun Ozogul

Reputation: 2469

private void BtnNext_Click(object sender, EventArgs e)
{
    this.Visible = false;
    Grading_Section g = new Grading_Section();
    g.DisableTextBoxes(comboboxValue);
    g.Show();    
}

In the form Garding_Section, you should create a method like this :

public void DisableTextBoxes(string value)
{
 if(value == "a")
  {
    //disabele related texboxes
  }
  else if(value == "b")
  {
   //disable related textboxes.
  }
}

I hope this helps.

Upvotes: 1

Related Questions