如何禁用其他表单中的多个文本框?

时间:2017-04-12 09:06:13

标签: c#

我在Form1中有这个组合框,当组合框值发生变化时我需要禁用一些文本框,并且有一个按钮可以转到该文本框被禁用的那个表单。

如果没有同时显示的两种表格,我怎么能做我想要的?!

以下是代码:

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();    
    }

这是我的FindControl类:

 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);
        }
    }

到目前为止,我找到了这段代码,但它唯一可行的方法是同时显示两个表单,否则会显示此错误:

当我删除此行时会弹出错误:&#34; grading.Show();

&#34; NullReference异常未处理,未处理的类型&#39; System.NullReferenceException&#39;发生在WindowsFormsApplication2.exe&#34;

此行产生的错误:

  • for(int i = 0; i&lt; f.Controls.Count; i ++)*

1 个答案:

答案 0 :(得分:1)

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

在Garding_Section表单中,您应该创建一个这样的方法:

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

我希望这会有所帮助。