So basically i have Form1
From Form1 I can open Form2 with this code:
private void btn_Komplexity_Click(object sender, EventArgs e)
{
Form2 kompleksaForma = new Form2();
kompleksaForma.ShowDialog();
}
When Form2 is opened there is something and at the end there is this.Close();
After this.Close(); (closing Form2) is it possible to call instant action on Form1?
If you're sticking to ShowDialog(), this function will block until the form is closed.
private void btn_Komplexity_Click(object sender, EventArgs e)
{
using (Form2 kompleksaForma = new Form2())
{
kompleksaForma.ShowDialog();
PutStuffHereAfterClose(); // (or outside the using block if it doesn't need
// to access properties of kompleksaForma)
}
}
If you're showing Form2 as a modal window using form2.ShowDialog() or form2.ShowDialog(this), then...
form2.ShowDialog(this);
if (form2.DialogResult == DialogResult.OK)
{
CallOtherStuffHere();
}
... as the ShowDialog() method will block execution until the closure of Form2, then continue.
I'm using DialogResult above to test for validity, but you could implement some other method, if you wish.
If you're showing Form2 as a non modal window, then you should pass a reference of Form1 to Form2 first. This could be done in its constructor...
var form2 = new Form2(form1);
Or, you can pass it in the Show() method, to set form1 as its parent...
var form2 = new Form2();
form2.Show(form1);
Then, you can access the parent form via form2.Parent. However, you may have to cast it to a Form1 instance before you call your methods explicitly. And this can be done in the Closing event handler of Form2.
Further info here regarding modal and modeless windows:
https://msdn.microsoft.com/en-us/library/aa984358(v=vs.71).aspx
There are some events triggered after the window is closed. You can subscribe to them and add your code to the handler method:
Form2 kompleksaForma = new Form2();
kompleksaForma.FormClosing += KompleksaForma_FormClosing;
kompleksaForma.FormClosed += KompleksaForma_FormClosed;
kompleksaForma.Deactivate += KompleksaForma_Deactivate;
kompleksaForma.ShowDialog();
And then implement one of the handlers like that:
private void KompleksaForma_FormClosing(object sender, FormClosingEventArgs e)
{
// Your code here
}
private void KompleksaForma_FormClosed(object sender, FormClosedEventArgs e)
{
// or here
}
private void KompleksaForma_Deactivate(object sender, EventArgs e)
{
// or here
}
First will trigger FormClosing, then FormClosed. Last one is Deactivate.
Related
I have this project for school which requires me to use 2 forms, but I can't have both of them open at the same time.
This means when I open one form, the other one minimizes. When you close this one, I'd like to make it so that the minimized one is restored.
This is what I have now for minimizing it:
private void Mkbtn1_Click(object sender, EventArgs e)
{
var newForm = new form1();
newForm.Show();
this.WindowState = FormWindowState.Minimized;
}
In form1, which is opening the form2 do things like this
`private void button1_Click(object sender, EventArgs e)
{
Form2 newofrm = new Form2();
newofrm.parentForm = this;
newofrm.Show();
this.Hide();
}`
And then in form2 use this
`private void Form2_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized && parentForm != null)
{
// Do some stuff
parentForm.Show();
}
}`
Here parentForm is public member of form2 of type Form, also if you want this do be done when form is closed then add form closing event and add same code in the event handler
I am wondering how can I correctly switch between forms by button click event.
I have Form1 and Form2.
Form1 have: -TextBoxForm1
-ButtonForm1
Form2 have: -TextBoxForm2
-ButtonForm2
I would like to on_click ButtonForm1 event go to the Form2. Then I want to write some message to TextBoxForm2 and press ButtonForm2 it will go to the Form1 again and message from TextBoxForm2 will appear in TextBoxForm1.
Everything works fine, but I have one problem. When I close application and I wanna debug and start it again, some errors appear like:"application is already running".
Form1:
public static string MSG;
public Form1()
{
InitializeComponent();
TextBoxForm1.Text = MSG;
}
private void ButtonForm1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
this.Hide();
//There is probably my fault but when I was trying this.Close(); everything shutted down
form2.Show();
}
Form2:
private void ButtonForm2_Click(object sender, EventArgs e)
{
Form1.MSG = TextBoxForm2.Text;
Form1 form= new Form1();
form.Show();
this.Close();
}
How can I do this correctly please? :) I am beginner, thank you!
I would not go the route of using a STATIC for passing between forms as you mention you are a beginner, but lets get the closing to work for you.
In your main form create a new method to handle an event call as Hans mentioned in the comment. Then, once you create your second form, attach to its closing event to force form 1 to just become visible again.
// Inside your Form1's class..
void ReShowThisForm( object sender, CancelEventArgs e)
{
// since this will be done AFTER the 2nd form's click event, we can pull it
// into your form1's still active textbox control without recreating the form
TextBoxForm1.Text = MSG;
this.Show();
}
and where you are creating form2
private void ButtonForm1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Closing += ReShowThisForm;
this.Hide();
form2.Show();
}
and in your second form click, you should only need to set the static field and close the form
private void ButtonForm2_Click(object sender, EventArgs e)
{
Form1.MSG = TextBoxForm2.Text;
this.Close();
}
Easy solution is to use modal form. Display Form2 temporarily, when it is displayed Form1 is invisible.
var form2 = new Form2();
this.Visible = false; // or Hide();
form2.ShowDialog(this);
this.Visible = true;
And to pass data you can define property in Form2, to example:
public string SomeData {get; set;}
Form1 has to set SomeData, which you take in Shown and display. Same property can be used to get data from Form2 (before closing).
// form 1 click
var form2 = new Form2() { SomeData = TextBoxForm1.Text; }
this.Visible = false;
form2.ShowDialog(this);
this.Visible = true;
TextBoxForm1.Text = form2.SomeData;
// form 2 shown
TextBoxForm2.Text = SomeData;
// form 2 click
SomeData = TextBoxForm2.Text;
Close();
i got 2 forms 1mdiparent, 1child
lets say mdiparent = Form1 then child = Form2 .
i got New button in Form1 calling childform (Form2) like:
private void newDocument_ItemClick(object sender, ClickEventArgs e)
{
Form2 formChild = new Form2();
Form2.Show()
}
now my question was whats the if else condition for : if Form2 == Close?
something like:
if (Form2.Close == true){ //condition }
or if (Form2 == Close){ //condition }
but i know its not the right code .so hope you could help me :) thanks .
If I understand your question correctly, you want to be notified by the system when the Form2 is closed so you could apply some internal logic to avoid the reopening of the child form and apply some kind of changes to your main interface.
If this is your problem then you could add your event handler for the FormClosing event of the Form2 directly in the code of Form1 when you open the Form2
// Flag to keep the state open/close of the child form
private bool childClosed = true;
private void newDocument_ItemClick(object sender, ClickEventArgs e)
{
if(childClosed == true)
{
Form2 formChild = new Form2();
// Setup the event handler form the Form2 closing directly here in the MDI
formChild.FormClosing += new FormClosingEventHandler(myFormClosing);
formChild.Show();
// set the flag to avoid the reopening
childClosed = false;
}
}
// Now, when the formChild closes, you will receive the event directly here in the MDI
private void myFormClosing(object sender, FormClosingEventArgs e)
{
// The child form is closing......
// Do your update here, but first check the close reason
if(e.CloseReason == CloseReason.UserClosing)
{
......
// reset the flag so you could reopen the child if needed
childClosed = true;
}
}
You will probably want to use either the Form.FormClosed event or the Form.FormClosing event. Probably the former since you don't really want to interact with the closing of the form.
You would create a method and have it called when the event is fired and this would do whatever you wanted (such as hide buttons and such like).
See http://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosed.aspx for details of the formclosed event.
When a form is closed, it's disposed too. Thus just check if it's disposed by following code:
Form2 formChild = new Form2();
// ...
if (formChild.IsDisposed) {
// Do someting
}
As #V4Vendetta pointed out: there is a FormClosing event (called before the form has closed. Used to tell the user "You haven't saved yet") and a FormClosed event (called after the form closed).
public class Form2 : Form
{
public bool hasClosed;
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
hasClosed = true;
}
}
with this, you can do
if (formChild.hasClosed)
// do something
It is wrong to take this approach, the Form 2 is initialized in the button click event, therefore, you assume that the form object was not available.
if you want to change this order, then the form should be initialized somewhere else like the constructor of the Form 1.
private Form2 form2;
public Form1()
{
form2 = new Form2();
}
private void newDocument_ItemClick(object sender, ClickEventArgs e)
{
if(!form2.Visible)
{
form2.Show();
}else
{
form2.Hide();
}
}
Hiding a form is not same as closing a form, if you really open and close the form2 and carry out some action based on that, then you should set a property on the parent form when the form2 closing event is triggered, to do this you must pass the parent form as an argument in the form2 constructor.
You can check that with the help of the following code
foreach (Form f in Application.OpenForms)
{
if (f.Text == formName)
{
IsOpen = true;
break;
}
}
if(!IsOpen)
{....do your code....}
I'm trying to change the names of the datagrid and labels in Form2 from Form1 based on the selection.
private void button1_Click_1(object sender, EventArgs e)
{
this.Hide();
Form2 frm = new Form2();
frm.Show();
}
private void button2_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.dataGridView1.Columns["FirstName"].HeaderText = "Prenom";
frm.dataGridView1.Columns["LastName"].HeaderText = "Nom";
this.Hide();
frm.Show();
}
The above approach is working fine for me but I have a problem. When I click on X in the second/Form 2 it's just closing the Form2 not the Form1. How can close all the application when i click on the X.
Is there any better way of doing this??? The reason why i'm not using I'm using Telerik and I don't find any option to add resource file in that. Please correct me if i'm wrong. Thank you.
You simply need to attach an event handler to the Closed event of the newly created form so that it closes the main form when it's closed:
private void button1_Click_1(object sender, EventArgs e)
{
this.Hide();
Form2 frm = new Form2();
frm.FormClosed += (_, args) => this.Close(); //Added this method
frm.Show();
}
Add that same method to the other click handler as well.
The easiest way to close an application is to call the static method: Application.Exit
Another method if you only want to close one single form would be to add a handler for the FormClosing event and close the main form there.
I designed two forms: Form1 and Form2. Form1 is the main form. There is a button in Form1, if I click the button, then Form2 will pop out. I want to do something on Form2.
// click button in Form1.
private void button1_Click(object sender, EventArgs e)
{
Form form2= new Form();
form2.ShowDialog();
}
But Form2 is a new form rather than an existing form.
It is wrong.
How? Thanks.
You are creating instance of Form class not the Form2 which you have in your project. Create instance of Form2 which you created earlier and then call ShowDialog in it.
You might have notice the in the program.cs something like Application.Run(new Form1()); Here we create the instance of Form1 and pass to Run method.
Do it this way by creating instance of Form2 and calling ShowDialog() method to show it
Form2 form2= new Form2();
form2.ShowDialog();
You create blank form with
Form Form2= new Form();
You should use
Form2 form2= new Form2();
Complete code:
private void button1_Click(object sender, EventArgs e)
{
Form2 form2= new Form2();
form2.ShowDialog();
}
Declare
Form2 form2= new Form2();
like your class member and use it like this:
private void button1_Click(object sender, EventArgs e)
{
form2.ShowDialog(); //blocking call
//or form2.Show() //non blocking call
}
EDIT
Based on correct comments, to make this work instead of executing the Close() on the function which will lead to Dispose() you need to use form2.Hide() to make is simply invisible
private void button1_Click(object sender, EventArgs e)
{
InputForm form1 = new InputForm();
form1.Show();
}
Here InputForm means which form you want to open.
The question is "Open an existing form from the main form"
Okay lets change it a little, Open an existing instance of form from the main form.
when you show a form
new Form2().Show();
lets say you hid it using
Form2.Hide();
you guys can use this
var Form2_instance = Application.OpenForms.OfType<Form2>().Single();
Form2_instance.Show();