Click here to Skip to main content
16,017,857 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Helloo..
I'v combobox in form, dynamically i wanna get combobox selected value in class

What I'v done is
I wrote the following code in class..

using experiments;

experiments.Form1 formvalue=new experiments.Form1();


But unable to get the value through "formvalue" object
Can u help me?
Posted

Initialize in the constructor.

Solution : A field initializer cannot reference the nonstatic field, method, or property[^]

read ps2goat's 2nd reply.
 
Share this answer
 
Please use:
C#
experiments.Form1 formvalue=new experiments.Form1();
string cbValue = string.Empty;
if(formvalue.cbLoginCompany.SelectedValue != null)
{
   cbValue = formvalue.cbLoginCompany.SelectedValue.ToString();
}
 
Share this answer
 
v2
Comments
krishna kishore58 29-Dec-10 5:00am    
I did the same long back, its throwing error at "SelectedValue". The issue is its not bringing the value from that control...Displaying as "cbLoginCompany" has 0 items i.e count=0;
Can u help me to come out of this issue...
jerrykid 29-Dec-10 5:31am    
Please re-view the answer, I updated it.
Change the combobox modifier to public and the you should be able to access it like this: formvalue.combobox1.SelectedValue.
 
Share this answer
 
Comments
krishna kishore58 29-Dec-10 2:47am    
Well the issue is its throwing the error as A field initializer cannot reference the non-static field, method, or property 'experiments.Form1.cbLoginCompany'
jerrykid 29-Dec-10 3:28am    
Please use:
<pre lang="cs">
experiments.Form1 formvalue=new experiments.Form1();
string cbValue = formvalue.cbLoginCompany.SelectedValue.ToString();
</pre>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900