We help IT Professionals succeed at work.

Combobox Selected item value to another form

Antonio King
Antonio King asked
on
389 Views
Last Modified: 2007-10-01
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 :(
Comment
Watch Question

CERTIFIED EXPERT

Commented:
u cant do that..
how are u calling this second form ? is it from the first form ?
Antonio KingIT Manager

Author

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.
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Antonio KingIT Manager

Author

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?
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Apologies everything I have just said has been posted by gauthampj. I never saw that particular post.

Andrew :-)
Antonio KingIT Manager

Author

Commented:
Like this?

        public FrmEmployee(Form Form1)
        {
            InitializeComponent();

            Object selectedItem = Form1.comboBox1.SelectedItem;
CERTIFIED EXPERT

Commented:
s
and u can use this selectedItem  @ where u want
CERTIFIED EXPERT

Commented:
and u can use this selectedItem  @ where u want
but this should be delcared outside the construcotr if u want to access it elsewhere
Antonio KingIT Manager

Author

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?)
CERTIFIED EXPERT

Commented:
public FrmEmployee(URMainForm   Form1)
        {
            InitializeComponent();

            Object selectedItem = Form1.comboBox1.SelectedItem;
Antonio KingIT Manager

Author

Commented:
debug error:
The type or namespace name 'URMainForm' could not be found (are you missing a using directive or an assembly reference?)
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
in your other form you need to define a property of the combo box. like

public ComboBox YourBox
{
get{
return yourComboBoxInstance;
}
}
Antonio KingIT Manager

Author

Commented:
Thanks guys, hope you find the split of points fair ;)
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.