I need to get the text value from a combo box on one form to assign to a variable in a separate class. I tried
stName=frmParent.ActiveForm.Controls["tblStatesComboBox"].Text
That generated an error that said it was not associated with an object.
What is the proper syntax for reading control values in a separate form. My class is NOT a form. It is just a small class with several values and properties.
Thanks
Loading
VulpesPosted Mar 30, 2012, 9:05 AM
Scott StewartPosted Mar 30, 2012, 12:01 PM
VulpesPosted Mar 30, 2012, 11:16 AM
It should be square brackets (for an indexer) rather than round brackets:
frmParent parent = (frmParent)Application.OpenForms["frmParent"];
Scott StewartPosted Mar 30, 2012, 10:56 AM
Scott StewartPosted Mar 30, 2012, 10:32 AM
I had added a property to my parent form so I could just "get" the StateName from the combobox. I just couldn't see it because, as you said, I didn't have the using directive at the top of my class.
Thank you for a clean, concise answer.
VulpesPosted Mar 30, 2012, 9:53 AM
using System.Windows.Forms;
You need to get a reference to the form before you can get access to its controls.
If the form is in fact the current active form, then an alternative way of getting it would be:
frmParent parent = (frmParent)Form.ActiveForm;
which is similar to what you were doing in the first place.
It's still possible to get at the combobox by first getting a reference to the toolstrip from the form's Controls collection and then getting a reference to the combobox from the toolstrip's Controls collection.
However, a more convenient way would be to create a public (or internal) read-only property within the form class which returns the combobox's Text property. All you need to do then is to invoke this property with your form reference.
Scott StewartPosted Mar 30, 2012, 9:35 AM