user2315477
user2315477

Reputation: 71

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

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

    }
}

Upvotes: 1

Views: 3749

Answers (3)

Hassan
Hassan

Reputation: 5430

Send ComboBox from form1 to form2 using constructor. Here is example:

Form1 Class:

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:

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. For controls you want to access from another form you have change Modifiers property as Public.

enter image description here

Form1 Class:

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:

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")
        {

        }
    }
}

Upvotes: 1

Majid
Majid

Reputation: 14253

You can write a public method in your ComboBox's class and then call it from where you have instance of that form.

like this:

in your main form:

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

in your modal:

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

Upvotes: 0

user3596113
user3596113

Reputation: 878

This is because your ComboBox is only available in the codebehind file of your Form.

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.

Upvotes: 0

Related Questions