Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 1 main form in that form i have one combobox named Select Treatment with no items in it i.e it is empty and below there is a button named Start new treatment. On button click a new form appears and in that form i have one combobox with some items in it and a save button. the problem is when i clicked the save button the selected item from combobox must transfer to the main form combobox . how do i do ? please help !

What I have tried:

Start new treatment Button on Main form code:

public void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2 .Show ();
}

On button click new form appears with combobox with some items in it and a save button.
Save Button Code:

private void button1_Click(object sender, EventArgs e)
{
TreatmentInformation tn = new TreatmentInformation();
tn.comboBox2.Items.Add (comboBox1.SelectedItem);

}
Posted
Updated 9-Feb-18 3:57am
Comments
Ziee-M 9-Feb-18 9:30am    
whats are you using (WPF, Winforms, asp.net)?
Member 13670141 9-Feb-18 9:49am    
vsiual c# windows form application

Here is 2 solutions for your problem.
-1-Sol 1 : Add a parentForm refernce for all non main form.
=> when you create a new form, you init your Parentform prop :
C#
Form2 f2 = new Form2();
f2.ParentForm = myParentForm;
f2 .Show ();

When you are about to close the Form2 using f2_Closing event, you call your parent method that fills the Combobox.
-2-Sol 2 : Subscribe to f2_closing event in your main form:
Form2 f2 = new Form2(); 
f2.FormClosed += new FormClosedEventHandler(f2_Closed);

When the form2 is closed the
f2_Closed
event will be executed and you can init your combobox.
 
Share this answer
 
In Program.cs create a public variable e.g. comboVar:
public static class Program
{
    public static string comboVar;

And access it in your forms like this:
tn.comboBox2.Items.Add (Program.comboVar);
 
Share this answer
 
v2

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