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();
Related
I have new form opening when button is clicked, like this:
private void button8_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.ShowDialog();
//more code
}
Does this mean that, when i click Button8, form2 will be opened, but code after form2.ShowDialog() will not be executed until i close form2?
If this is not the way how to do that, please, can you tell me.
Use Show(); not ShowDialog(); That should works.
I'm trying to create a form over the parent form so that wherever the parent form is the new form will be created in the same place. I've looked around and have so far found this:
private void Cleaning_Click(object sender, EventArgs e)
{
this.Hide();
Form2 form2 = new Form2();
form2.Show();
form2.Location = new Point(this.Left, this.Top);
this.Close();
}
But I can't figure out how to fully close the old parent form without closing the newly created form.
Here is my old code:
private void Cleaning_Click(object sender, EventArgs e)
{
this.Hide();
Cleaning c = new Cleaning();
c.ShowDialog();
this.Close();
}
OK the main problem now is that the this.Close closes both the forms and I really need some advice.
Thanks for any help.
The form you are trying to close is the main form of the application so if you close it all other form will close with it
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1 form1 = new Form1();
Form1 form2 = new Form2();
Application.Run(form1);
// It will run form1 until it close then run the form2
Application.Run(form2);
}
}
}
Please try this:
private void Cleaning_Click(object sender, EventArgs e)
{
this.Hide();
Form2 form2 = new Form2();
form2.Show();
form2.Location = new Point(this.Location.X, this.Location.Y);
this.Close();
}
And change property of new form: StartPosition => Manual
Hope this help !
Use Hide property to hide the parent form after Show of child form.
Add Application.Exit() to the close button of the child form. (if you want to exit from child form)
Do not add the this.Close() in the parent form.
PS: Tell me if I missed something.
I have created a C# application.
Here I have two forms, form1 and form2.
form2 is called from form1.
Later form2 is made hidden.
Now I want to show form2 from form1.
Please give me some idea.
You need to keep reference to Form2 object, and when you want it to be visible, just call frm2.Show() - don't construct the new Form2 object with new Form2() - use the existing one.
// You need to contruct Form2 before calling Show().
Form2 frm2 = new Form2();
// Some handler somewhere
void btnShowForm2_Click(..., ...)
{
frm2.Show();
}
Edit: As Micah pointed out, you will want to hide Form2 instead of closing it:
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
this.Hide();
e.Cancel = true; // this cancels the close event.
}
You will want to use form.hide() when hiding form2 instead of form.close
keep a reference to form2 and call form.show when you want to show it again
take form2 instance variable at class level
example
Public Class Form1
{
Form frm2;
//Show form here
protected void Button1_Clik
{
frm2=new Form2();
frm2.Show();
}
//Even the form is hidden, you may show the same instance /same state of form again
protected void Button2_Click()
{
frm2.Show();
}
}
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 have 2 forms. One is the display form and the other is the where data is added to an array.
How do I start a function on the display form once the ok button has been clicked on the add to array form?
EDIT:
Let me rephrase. How would I go about calling a function that is in one .cs file, from another .cs file.
Form 1
private void menuItem1_Click(object sender, EventArgs e)
{
Form2 form2= new Form2();
form2.Owner = this;
form2.ShowDialog(this);
}
Form 2
private void button1_Click(object sender, EventArgs e)
{
Form1 form1 = new Form1();
form1.myMethod();
}
Obviously, the form2 code is generating a new instance of form1.
How do I run the method of the form that opened the modal window?
Thanks
The owner of Form2 is a instance of Form1 so you can cast the owner to Form1 and call the method:
(this.Owner as Form1).myMethod();