简体   繁体   English

如何从另一个窗体(类)中调用要在按钮内使用的comboBox

[英]How to call a comboBox from another form (class) to be used inside a button

I want to call a some combo-box items so i can make an if /else statements and output a form.The combo-box items are out side of my class(Form) how can i access them i tried this(below) but the error says does not exist in the current context.I also changed it the method from private to public 我想打电话给一些组合框项目,这样我就可以做出if / else语句并输出一个表格。组合框项目不在我的班级(Form)的范围内,我怎样才能访问它们(下面),但是该错误表明当前情况下不存在。我还将其方法从私有更改为公开

public void buttonFinish_Click(object sender, EventArgs e)
{           
    if(comboBoxD.Text == "Alphabet" && comboBoxType.Text == "Numbers")
    {

    }
}

Send ComboBox from form1 to form2 using constructor. 使用构造函数将ComboBox从form1发送到form2。 Here is example: 这是示例:

Form1 Class: Form1类别:

public partial class Form1 : Form
{        
   public Form1()
   {
        InitializeComponent();
   }

   private void Form1_Load(object sender, EventArgs e)
   {
       Form2 f2 = new Form2(comboBox1, comboBox2);
       f2.Show();
   }
}

Form2 Class: Form2类别:

public partial class Form2 : Form
{ 
   ComboBox comboBoxD; 
   ComboBox comboBoxType;

   public Form2(ComboBox cb, ComboBox cbType)
   {
        InitializeComponent();
        comboBoxD = cb;
        comboBoxType = cbType;
   }

   private void Form2_Load(object sender, EventArgs e)
   {

   }

   protected void buttonFinish_Click(object sender, EventArgs e)
   {           
        if(comboBoxD.Text == "Alphabet" && comboBoxType.Text == "Numbers")
        {

        }
   }
}

UPDATE : 更新

Here is another approach for accessing controls present in another form. 这是访问以另一种形式存在的控件的另一种方法。

Default Modifiers of every control is private . 每个控件的默认Modifiers都是private For controls you want to access from another form you have change Modifiers property as Public . 对于要从​​其他表单访问的控件,可以将Modifiers属性更改为Public

在此处输入图片说明

Form1 Class: Form1类别:

public partial class Form1 : Form
{        
   public Form1()
   {
        InitializeComponent();
   }

   private void Form1_Load(object sender, EventArgs e)
   {
       Form2 f2 = new Form2(this);
       f2.Show();
   }
}

Form2 Class: Form2类别:

public partial class Form2 : Form
{
    private Form1 f1;
    public Form2(Form1 f)
    {
      InitializeComponent();
      f1 = f;
    }

    protected void buttonFinish_Click(object sender, EventArgs e)
    {           
        if(f1.comboBoxD.Text == "Alphabet" && f1.comboBoxType.Text == "Numbers")
        {

        }
    }
}

You can write a public method in your ComboBox 's class and then call it from where you have instance of that form. 您可以在ComboBox的类中编写一个public方法,然后从具有该表单实例的位置调用它。

like this: 像这样:

in your main form: 以您的主要形式:

  using (var modal = new MyModal())
  {
            modal.ShowDialog();
            modal.getSomething();
  }

in your modal: 在您的模态中:

  public string getSomething()
  {
       return yourComboBox.Text;
  }     

This is because your ComboBox is only available in the codebehind file of your Form. 这是因为您的ComboBox仅在表单的代码隐藏文件中可用。

One solution would be to store a reference to your combobox as a property in your codebehind. 一种解决方案是将对组合框的引用作为属性存储在代码隐藏中。

like this: 像这样:

public ComboBox myCmbBox { get; private set; }

and access it in the codebehind of form2. 并在form2背后的代码中访问它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM