Combobox Selected item value to another form

How do I get the combobox.SelectedItem value from another form?
I'm using this...

            Object selectedItem = Form1.comboBox1.SelectedItem;

But of course, doesn't work :(
Antonio KingIT ManagerAsked:
Who is Participating?
 
Gautham JanardhanConnect With a Mentor Commented:
pass the form variable of the main form as a parameter to FrmEmployee



            FrmEmployee FrmEmployee = new FrmEmployee(this);
            FrmEmployee.Show();

employee construcotr wil be like

public void FrmEmployee (MainForm Form)
{
}

and then in the place u want to acess the variable

Form.comboBox1.SelectedItem and this combobox should be decalred public
0
 
Gautham JanardhanCommented:
u cant do that..
how are u calling this second form ? is it from the first form ?
0
 
Antonio KingIT ManagerAuthor Commented:
Main form is called Form1
This form calls FrmEmployee like so...

           FrmEmployee FrmEmployee = new FrmEmployee();
            FrmEmployee.Show();

FrmEmployee needs to display the selecteditem value of a combobox on Form1.
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
Antonio KingIT ManagerAuthor Commented:
pass the form variable of the main form as a parameter to FrmEmployee
How do I do this?

Sorry i'm quite a beginner with c#, could you explain to me exactly where each bit of code needs to go?
0
 
REA_ANDREWConnect With a Mentor Commented:
in your code for the form FrmEmployee. Overload the constructor so

public FrmEmployee(){}
public FrmEmployee(Form AnotherFormWithComboBox)
{
_yourFormVariable = AnotherFormWithComboBox;
}


This way when you instantiate the form you can make use of the second overload which accepts a form as an argument.

Andrew
0
 
REA_ANDREWCommented:
Apologies everything I have just said has been posted by gauthampj. I never saw that particular post.

Andrew :-)
0
 
Antonio KingIT ManagerAuthor Commented:
Like this?

        public FrmEmployee(Form Form1)
        {
            InitializeComponent();

            Object selectedItem = Form1.comboBox1.SelectedItem;
0
 
Gautham JanardhanCommented:
s
and u can use this selectedItem  @ where u want
0
 
Gautham JanardhanCommented:
and u can use this selectedItem  @ where u want
but this should be delcared outside the construcotr if u want to access it elsewhere
0
 
Antonio KingIT ManagerAuthor Commented:
doesnt work...

get debug error...
'System.Windows.Forms.Form' does not contain a definition for 'comboBox1' and no extension method 'comboBox1' accepting a first argument of type 'System.Windows.Forms.Form' could be found (are you missing a using directive or an assembly reference?)
0
 
Gautham JanardhanCommented:
public FrmEmployee(URMainForm   Form1)
        {
            InitializeComponent();

            Object selectedItem = Form1.comboBox1.SelectedItem;
0
 
Antonio KingIT ManagerAuthor Commented:
debug error:
The type or namespace name 'URMainForm' could not be found (are you missing a using directive or an assembly reference?)
0
 
Gautham JanardhanConnect With a Mentor Commented:
'URMainForm'  -> is the class name of the main form...


can u post the constructor of the main form
0
 
REA_ANDREWCommented:
in your other form you need to define a property of the combo box. like

public ComboBox YourBox
{
get{
return yourComboBoxInstance;
}
}
0
 
Antonio KingIT ManagerAuthor Commented:
Thanks guys, hope you find the split of points fair ;)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.