如何禁用form2组合框基于form1组合框值? [英] How to disable form2 combo boxes based on form1 combo box value?
问题描述
我试图在窗体2中禁用一些组合框。如果comboxbox的值为0
禁用表单2中的某些组合框
我在form1中做了什么
public partial class Form1:Form
{
public Form1()
{
InitializeComponent();
}
public ComboBox combo
{
get {return dropdown; }
}
到目前为止形式2
private void Form2_Load(object sender,EventArgs e)
{
Form1 f = new Form1();
if(f.combo.SelectedIndex == 0)
{
comboBox1.Enabled = false;
}
这不工作,我不能排序(新到c#)
如果使用以下语法在Form中创建Form1:
Form1 f = new Form1();
您将获得Form1(或copy)的新实例,而不是已经打开的实例。您需要将Form1的引用传递给Form2。
将此代码添加到Form2中:
private Form1 myParentForm;
public Form2(Form1 parentForm)
{
myParentForm = parentForm;
}
然后你可以通过变量myParentForm使用Form1。像这样:
private void Form2_Load(object sender,EventArgs e)
{
if(myParentForm。 combo.SelectedIndex == 0)
{
comboBox1.Enabled = false;
}
}
在Form1中有一个类似这样的代码: / p>
Form2 mySecondForm = new Form2();
更改为:
form2 mySecondForm = new Form2(this);
I am trying to disable some of the combo boxes in form 2 based in form 1 selected value.
Lets say In Form1 if comboxbox value is 0 Disable certain combo boxes in form 2
What I have done in form1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public ComboBox combo
{
get { return dropdown; }
}
and in form 2 so far
private void Form2_Load(object sender, EventArgs e)
{
Form1 f = new Form1();
if (f.combo.SelectedIndex == 0)
{
comboBox1.Enabled = false;
}
This is not working and I cant sort it out (new to c#)
If you create Form1 at Form with this syntax:
Form1 f = new Form1();
You will get new instance of Form1 (or "copy"), not the one thats already open. You need to pass the reference of Form1 to Form2.
Put this code into Form2:
private Form1 myParentForm;
public Form2(Form1 parentForm)
{
myParentForm = parentForm;
}
Then you can use Form1 through a variable myParentForm. Like this:
private void Form2_Load(object sender, EventArgs e)
{
if (myParentForm.combo.SelectedIndex == 0)
{
comboBox1.Enabled = false;
}
}
In Form1 you have a code something like this:
Form2 mySecondForm = new Form2();
change that to:
Form2 mySecondForm = new Form2(this);
这篇关于如何禁用form2组合框基于form1组合框值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!