Hi,
I am stuck with one problem at passing combobox value from one FORM1 to FORM2.
I wrote code in form2 like this:
Form1 frm1= new Form1();
frm1.combobox1.SelectedItem;
I am getting null value.
How can I pass values?
Thanks,
Darma
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jignesh TrivediRank #16 · 50.3K reputationPosted Feb 12, 2013, 5:38 PM EST
hi,
If you open form2 from the form1 then you may pass combo value in constructior of form2
like....
public Form2(string value)
{
}
private void callingFunction()
{
Form2 obj = new Form2(passcomboselected value);
}
hope this will help you.
VulpesRank #55 · 29.2K reputationPosted Feb 12, 2013, 6:03 AM EST
darma tejaRank #3,199 · 449 reputationPosted Feb 12, 2013, 6:12 AM EST
Thanks for the reply.
I am getting error at: ComboBox cb1 = (ComboBox)frm1.Controls["comboBox1"];
this is the error: Object reference not set to an instance of an object.
Thanks,
Darma
VulpesRank #55 · 29.2K reputationPosted Feb 12, 2013, 6:36 AM EST
1. The exact name of the combobox isn't comboBox1; or
2. The combobox hasn't been placed directly on the form but is inside some container such as a panel or groupbox.
Does either of these ring a bell?
darma tejaRank #3,199 · 449 reputationPosted Feb 12, 2013, 6:43 AM EST
you are right. It applies in 2nd point.
The combobox hasn't been placed directly on the form but is inside some container such as a panel or groupbox.
Actually it is in TAB and groupbox.
But I changed those two controls modifiers as "public"
Thanks,
Darma
VulpesRank #55 · 29.2K reputationPosted Feb 12, 2013, 6:48 AM EST
darma tejaRank #3,199 · 449 reputationPosted Feb 12, 2013, 6:59 AM EST
After changing modifier to "Public" also, it is not working.
Thanks
Darma
VulpesRank #55 · 29.2K reputationPosted Feb 12, 2013, 7:53 AM EST
If it is null, then are you sure that there is currently a selected item in the combobox?
darma tejaRank #3,199 · 449 reputationPosted Feb 12, 2013, 10:57 PM EST
I am getting null error.
Anyway thanks allot.
It is working now with Jignesh Idea.
Darma
Gopal Mahadak136 reputationPosted Feb 16, 2013, 6:26 PM EST
public static string str;
then In first form take combox & load items in it..
& combox selectedValueChanged event write follwing code:
Class1.str = comboBox1.SelectedItem.ToString();
& to show form2 on button write code in form1...
Then in form2 load event:
comboBox1.Items.Add(WindowsFormsApplication1.Class1.str);
If not get check attachment