How to close a form and come back to mainform - c#

My question seems very easy but it's actually quite tricky. I search a lot on here and I dont find anything conclusive.
I have a mainform which need to be constantly open. I have a button which call a form2, I do some action on it, then call a form3 which let the user choose some item then close it and come back to mainform. The tricky part is i need to pass data from f3 to f1.
My problem is, on the second form I have a next & cancel buttons (and a close button) . Next button hide f2, form1 "come back" & call the form3. Why not call directly the form3? because I need to pass data from form3 to form1 so i Need to call form3 from form1.
So i want Cancelbutton (and closing button) to come back to form1 without calling form3. I am not sure to be understood so here's my code
public partial class Form1 : Form
{
private void bouya_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.ShowDialog();
Form3 form3 = new Form3(); //it opens when form2 closes!!!
form3.ShowDialog();
string data1 = form3.label2.Text; //the data i need, without calling form3 from here i can't pass data !!!
//do some action with data1
}
}
form2 is very simple
public partial class Form2 : Form
{
private void Next_Click(object sender, EventArgs e)
{ this.Hide(); }
private void Cancel_Click(object sender, EventArgs e)
{//i just want here to cancel winform3 to be called}
private void Form2_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{ // Actually this command does'nt work at all i tried
//to put a messagebox.Show("bla") nothing happens but i would like to do the same}
}
And form3 I select an item from a list to be send to form1
public partial class Form3 : Form
{
private void button1_Click(object sender, EventArgs e)
{
foreach (Object obj in listBox1.SelectedItems)
{
label2.Text = obj.ToString();
this.Hide();
}
}
}
Ok for some of you my problem would seem useless and i complicated my code for nothing but i really need these form. (and i need to open form3 from form1 to send data 3 to 1)
Forget about *Form1 form1 = new Form1(); it calls a new form1 and i don't want it !

You actually do not need to call form3 from form1 just to pass a value. If I understand you correctly, the desired flow should be like this
form1
opens
form2
opens
form3
|
| value
|
form1 <---+
So form1 opens form2 which opens form3 and the value from form3 should be passed back to form1, right?
What I'd do is declare a property ValuePickedInForm3 or whatever in form2 and get this in form1 when form2 closes.
Example
This pseudo-code is in the form2 class
public string ValueFromForm3
{
get;
private set;
}
public void ShowForm3()
{
using (Form3 f3 = new Form3())
{
if (f3.ShowDialog(this) == DialogResult.OK)
ValueFromForm3 = f3.TheValueYouNeed;
}
}
This pseudo-code goes into the form1 class:
public void ShowForm2()
{
using (Form2 f2 = new Form2())
{
if (f2.ShowDialog(this) == DialogResult.OK)
DoSomethingWith(f2.ValueFromForm3);
}
}

Related

Remove last item in datagridview with another form C# [duplicate]

I have two forms. First, Form1 has a group box, some labels and a listbox. I press a button and new Form2 is opened and contains some text. I want to transfer the text in Form2 to the listbox in the Form1.
So far, what I have done is make modifier of listbox to public and then put this code in the button of Form2
Form1 frm = new Form1();
frm.ListBox.items.Add(textBox.Text);
But amazingly, this does not add any value. I thought I was mistaken with the insertion so I made the same procedure. This time, I made a label public and added textbox value to its Text property but it failed.
Any ideas?
Try adding a parameter to the constructor of the second form (in your example, Form1) and passing the value that way. Once InitializeComponent() is called you can then add the parameter to the listbox as a choice.
public Form1(String customItem)
{
InitializeComponent();
this.myListBox.Items.Add(customItem);
}
// In the original form's code:
Form1 frm = new Form1(this.textBox.Text);
Let's assume Form1 calls Form2. Please look at the code:
Form1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.Show();
frm.VisibleChanged += formVisibleChanged;
}
private void formVisibleChanged(object sender, EventArgs e)
{
Form2 frm = (Form2)sender;
if (!frm.Visible)
{
this.listBox1.Items.Add(frm.ReturnText);
frm.Dispose();
}
}
}
Form2:
public partial class Form2 : Form
{
public string ReturnText { get; set; }
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.ReturnText = this.textBox1.Text;
this.Visible = false;
}
}
The answer is to declare public property on Form2 and when form gets hidden. Access the same instance and retrieve the value.
Below code working perfect on my machine.
private void button1_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
f1.listBox1.Items.Add(textBox1.Text );//ListBox1 : Modifier property made public
f1.ShowDialog();
}
Ok, If you are Calling Sequence is like, Form1->Form2 and Form2 updates the value of Form1 then you have to use ParentForm() or Delegate to update the previous form.
Form1 frm = new Form1();
frm is now a new instance of class Form1.
frm does not refer to the original instance of Form1 that was displayed to the user.
One solution is, when creating the instance of Form2, pass it a reference to your current instance of Form1.
Please avoid the concept of making any public members like you said
>>i have done is make modifier of listbox to public and then in form2 in button code<<
this is not a good practice,on the other hand the good one is in Brad Christie's Post,I hope you got it.
This code will be inside the form containing myListBox probably inside a button click handler.
Form2 frm2 = new Form2();
frm2.ShowDialog();
this.myListBox.Items.Add(frm2.myTextBox.Text);
frm2.Dispose();

Refreshing/Reload From form 1 to form 2

private void Form1_Load(object sender, EventArgs e)
{
f2 = new Form2();
f2.Show();
}
I have that code on my Form1. So that will automatically run Form2_Load. Is it possible that when I click a button on Form1. Form2_Load will run again without having to close Form2.
In response to your comment:
When a button on Form1 is clicked it will send a message update to form2. And a textbox in Form2 will have the message.
(First of all, this isn't the Observer Pattern. But that's ok, it doesn't need to be.)
First, Form1 needs to retain its reference to the instance of Form2:
public class Form1
{
private Form2 Form2Instance { get; set; }
private void Form1_Load(object sender, EventArgs e)
{
Form2Instance = new Form2();
Form2Instance.Show();
}
// the rest of your code
}
Now other code on Form1 can reference that instance. This is because you don't want to actually reload all of Form2 any time anything changes. You just want to "send a message update to form2".
Now, how will Form2 receive that message? It can expose a method. Something like this:
public class Form2
{
public void Update(string newValue)
{
someTextBox.Text = newValue;
}
// the rest of the code
}
Now code on Form1 can call that method on the instance of Form2 that it references. So when you click a button on Form1, you might do this:
Form2Instance.Update("some value");
This would "send a message" to the instance of Form2.

Why do my C# Form closed when displayed?

i have 2 forms, those are form1.cs and form2.cs
on the form1, it has button1, which will call form2 to show
here's the button1 code
private void button1_Click(Object sender, EventArgs e )
{
form2 form = new form2();
form2.show(); // to call form2
this.dispose(); //to dispose form1
}
and then form2 showed, and it closed suddenly. anyone know how to solve this ?
When you close your main form with this.dispose() you are terminating the program causing form2 to be disposed also because you are diposing the reference to form2. You would be better off passing a reference to your form1 to form2 and using this.Hide() instead.
You can try something like this:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form = new Form2();
form.setParent(this);
form.Show();
this.Hide();
}
}
And in form2 to go back to form1
public partial class Form2 : Form
{
Form parentForm;
public Form2()
{
InitializeComponent();
}
public void setParent(Form value)
{
parentForm = value;
}
private void button1_Click(object sender, EventArgs e)
{
parentForm.Show();
this.Close();
}
}
private void button1_Click(Object sender, EventArgs e )
{
form2 form = new form2();
form2.show(); // to call form2
this.hide(); //to hide form1
}
if form1 is program starter then application will close . Hence instead
this.dispose();
U just write
this.hide();
Show() does not wait for form2 to close before continuing to the next command (dispose).
This will end up in closing form2 because it's probably running on a background thread.
Use ShowDialog to hold the execution of Dispose until the second form closes.
Also, you can set the second form to run on a foreground thread. This way the second form will not be dependent on the life of the first.
You can either use this.Hide() which should hide your current form or use a thread to open the new form.
Example: C# open a new form, and close a form...

Open Form2 from Form1, close Form1 from Form2

Now, I have two forms, called form1 and form2, in the form1 there's a button, when I click it, then open the form2
Question: in the form2, I want to create a button when I click it, the form2 close and the form1 close. How to do?this
Form1:
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2(this);
frm.Show();
}
Form2:
public partial class Form2 : Form
{
Form opener;
public Form2(Form parentForm)
{
InitializeComponent();
opener = parentForm;
}
private void button1_Click(object sender, EventArgs e)
{
opener.Close();
this.Close();
}
}
This works:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Hide()
Form2.Show()
Your question is vague but you could use ShowDialog to display form 2. Then when you close form 2, pass a DialogResult object back to let the user know how the form was closed - if the user clicked the button, then close form 1 as well.
on the form2.buttonclick put
this.close();
form1 should have object of form2.
you need to subscribe Closing event of form2.
and in closing method put
this.close();
if you just want to close form1 from form2 without closing form2 as well in the process, as the title suggests, then you could pass a reference to form 1 along to form 2 when you create it and use that to close form 1
for example you could add a
public class Form2 : Form
{
Form2(Form1 parentForm):base()
{
this.parentForm = parentForm;
}
Form1 parentForm;
.....
}
field and constructor to Form2
if you want to first close form2 and then form1 as the text of the question suggests, I'd go with Justins answer of returning an appropriate result to form1 on upon closing form2
I did this once for my project, to close one application and open another application.
System.Threading.Thread newThread;
Form1 frmNewForm = new Form1;
newThread = new System.Threading.Thread(new System.Threading.ThreadStart(frmNewFormThread));
this.Close();
newThread.SetApartmentState(System.Threading.ApartmentState.STA);
newThread.Start();
And add the following Method. Your newThread.Start will call this method.
public void frmNewFormThread)()
{
Application.Run(frmNewForm);
}
//program to form1 to form2
private void button1_Click(object sender, EventArgs e)
{
//MessageBox.Show("Welcome Admin");
Form2 frm = new Form2();
frm.Show();
this.Hide();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 m = new Form2();
m.Show();
this.Visible = false;
}

I would like to control Form1 from Form2

So I want basically the user to login in first in order to use the other form. However, my dilemma is that the login box is in Form2, and the main form is Form1.
if ((struseremail.Equals(username)) && (strpasswd.Equals(password)))
{
MessageBox.Show("Logged in");
form1.Visible = true;
form1.WindowState = FormWindowState.Maximized;
}
else
{
MessageBox.Show("Wow, how did you screw this one up?");
}
However, Form1 doesn't become visible, (since I launch it as visble = false) after they log in. Can someone help?
EDIT:
Brilliant response, but my problem is still here. I basically want to load Form2 First, (which is easy I run Form1 and set it to hide) But when Form2 is closed, I want Form1 to be closed as well.
private void Form2_FormClosing(Object sender, FormClosingEventArgs e)
{
Form1 form1 = new Form1();
form1.Close();
MessageBox.Show("Closing");
}
this doesn't seem to work...
You will need to pass the reference of one form to another, so that it can be used in the other form. Here I've given an example of how two different forms can communicate with each other. This example modifies the text of a Label in one form from another form.
Download Link for Sample Project
//Your Form1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2(this);
frm.Show();
}
public string LabelText
{
get { return Lbl.Text; }
set { Lbl.Text = value; }
}
}
//Your Form2
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private Form1 mainForm = null;
public Form2(Form callingForm)
{
mainForm = callingForm as Form1;
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
this.mainForm.LabelText = txtMessage.Text;
}
//Added later, closing Form1 when Form2 is closed.
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
mainForm.Close();
}
}
(source: ruchitsurati.net)
(source: ruchitsurati.net)
When you log in and do Form1.visible = true; have you also tried Form1.Show(); that should show form2
However, Personally, I would prefer setting the application to run form2 directly in the program.cs file.
static void Main()
{
Application.Run(new Form2());
}
then when user successfully logs in, do
form1.Show();
this.Hide(); // this part is up to you
mind you, in form2, when / after you instantiate form1, you might want to also add this :
newform1.FormClosed += delegate(System.Object o, FormClosedEventArgs earg)
{ this.Close(); };
this closes form2 when form1 is closed
better yet do form1.Show() in a new thread, and then this.Close(); for form2. this removes the need of adding to the form2's FormClosed event: you can thus close form2 immediately after starting form1 in a new thread. But working with threads might get a little complicated.
EDIT:
form2 is form1's parent. if form2 is your main application form, closing it closes your program (generally). Thus you either want to just hide and disable form2, and close it only after form1 is closed, or start form1 in a new thread. Your edit pretty much opens form1, then immediately closes it.

Categories