[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 286
  • Last Modified:

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 :(
0
Antonio King
Asked:
Antonio King
  • 6
  • 6
  • 3
3 Solutions
 
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
 
Gautham JanardhanCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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_ANDREWCommented:
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 JanardhanCommented:
'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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 6
  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now