Reputation: 3089
Here is my Scenario: (Windows Application/C#.Net)
I have a Form1 in that is doing some calculations with the option selected as Case 1 and On Button Click it Pushes all the textbox values to Form2. This is working fine.
Now if I come to Form 1 again and change the option to case 2 and calculate and press Button it erases the values that were added earlier.
I want to retain those values and add this values to another set of textboxes that are in Form2.
Code: In Form 1 After Clicking ADD button:
private void button1_Click(object sender, EventArgs e)
{
Form2 eigthForm = new Form2();
if(comboBox1.Text == "Case 1")
{
eigthForm.text2.Text = textBox1.Text;
eigthForm.text3.Text = textBox8.Text;
}
if (comboBox1.Text == "Case 2")
{
eigthForm.text4.Text = textBox1.Text;
eigthForm.text5.Text = textBox8.Text;
}
Upvotes: 0
Views: 2106
Reputation: 6552
Ok I understand what you are asking now. You can use the Singleton pattern as one way as follows
Form1 buttons
private void button1_Click(object sender, EventArgs e)
{
Form2.Instance.AddText1and2("Hello", "World");
}
private void button2_Click(object sender, EventArgs e)
{
Form2.Instance.AddText3("Foo");
}
Form2 we will change to Singleton pattern
private static volatile Form2 instance = null;
private static object lockThis = new object();
private Form2()
{
InitializeComponent();
}
public static Form2 Instance
{
get
{
if (instance == null)
{
lock(lockThis)
{
if (instance == null)
{
instance = new Form2();
}
}
}
return instance;
}
}
Now our methods to place text in the correct places
public void AddText1and2(string txt1, string txt2)
{
textBox1.Text = txt1;
textBox2.Text = txt2;
this.Show();
}
public void AddText3(string txt3)
{
textBox3.Text = txt3;
this.Show();
}
If you want to be able to close the window and reopen it saving values you need to use the form closing event
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
this.Hide();
e.Cancel = true;
}
You have to prevent the actual close. This will dispose the object and we don't want that.
This will keep all your settings in form 2.
if you don't want to do the above you can add a new class to your project doing something like this
class Global
{
public static string Value1 { get; set; }
public static string Value2 { get; set; }
}
in Form1
if(comboBox1.Text == "Case 1")
{
Global.Value1 = textBox1.Text;
Global.Value2 = textBox8.Text;
}
in Form2 load event
text2.Text = Global.Value1;
text3.Text = Global.Value2;
You should also revise your if logic The combobox will never == "Case 2" if it's "Case 1" but you Are still running the next if statement
You should use something like this
if(comboBox1.Text.Equals("Case 1"))
{
}
else if (comboBox1.Text.Equals("Case 2"))
{
}
OR
switch (comboBox1.Text)
{
case "Case 1":
//do stuff
break;
case "Case 2":
//do stuff
break;
}
Upvotes: 1
Reputation: 3680
Here's an option
Form2 inst = null;
private void button1_Click(object sender, EventArgs e)
{
if(inst == null)
inst = new Form2();
if(comboBox1.Text == "Case 1")
{
inst.text2.Text = textBox1.Text;
inst.text3.Text = textBox8.Text;
}
if (comboBox1.Text == "Case 2")
{
inst.text4.Text = textBox1.Text;
inst.text5.Text = textBox8.Text;
}
inst.Show();
}
Upvotes: 2