私はこのコンボボックスを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);
}
}
は、これまでのところ、私はこのコードを見つけたが、それが動作することを唯一の方法は、フォームの両方を示すことですそれ以外の場合は、このエラーが表示されます。
この行を削除すると、エラーが表示されます。ポップアップ: "grading.Show();
は
にこのラインからの未収エラー "NullReference例外は、 'System.NullReferenceException' 種類の未処理の例外がWindowsFormsApplication2.exeで発生した、未処理だった":(iは0 = int型のため
- 。 i < f.Controls.Count;私はこれが役に立てば幸い
public void DisableTextBoxes(string value) { if(value == "a") { //disabele related texboxes } else if(value == "b") { //disable related textboxes. } }
:私はあなたがこのようなメソッドを作成する必要があり、フォームGarding_Sectionで)*
ええ、オープンフォームコレクションを経由しているため、両方のフォームが表示されている場合のみ、コードが機能します。 2番目のフォームでそれを無効にする場合は、そのような他のフォームのコンストラクタなどにいくつかのパラメータを渡す必要があります... – Nino