Can't close a form from other form - c#

Maybe this is a really dumb problem, but I can't close a form.
This is what I'm trying to do:
Start Main form -> Open second form -> Open third form and close the second form..
I use this code to open the second form:
this.Hide();
System.Threading.Thread.Sleep(200);
pauzescreen p = new pauzescreen();
p.Show();
And I use this code to open the third form:
this.WindowState = FormWindowState.Maximized;
Form1 form1 = new Form1();
form1.TopMost = true;
form1.Show();
form1.Activate();
And then I close the second form with this code: (Here is the problem, this doesn't work..)
pauzescreen pauze = new pauzescreen();
pauze.Hide();
Can't explain it very well, but what it does it creates an fullscreen screen capture at the second form (Pauzeform) and at the third form you can select an region.
The second form and third form have no border and are maximized.

You are creating two separate references to two separate forms: p and pauze. To close the original form you would need to retain the reference and call Close() or Hide() on that:
pauzescreen p = new pauzescreen();
p.Show();
// other stuff
p.Hide();

I have a multiform application, and I'm quite fond of using the Program class.
static class Program
{
// declare the forms in the program member space
static Form1 firstForm;
static Form2 secondForm;
static Form3 thirdForm;
}
What I do is in the Main method, initialize the forms.
static void Main()
{
firstForm = new Form1()
// ... so on and so forth
}
Then, whenever you want to show or hide those forms, use
// To hide a form, use its Hide method
Program.firstForm.Hide()
// To show a form, use its Show method
Program.secondForm.Show()
in your code. It's worked in my applications just fine like that. :)

As Sid pointed out, the main problem is that you're creating a new instance of your second form when writing
pauzescreen p = new pauzescreen();
As you're trying to close it in a part of the code different from where your form's instantiation is, one fittable solution, instead of handling events, is to keep reference of your object by passing it as a parameter to the class or method attempting the closure.
Here is an example of how you could work with the same object in two different classes.

Related

C# Add Form to running application

Simple question:
I used Application.Run to start my application because I need it this way.
Now I want to add a form later in the code. But it doesn't open the form if I use new Form1. Instead it runs everything else in the constructor of the class Form1. So somehow it ignores to open the form.
Just newing up a form makes it exist in memory but does not display anything.
You have to show it:
// create instance of Form1, does not show it
var myForm = new Form1();
// show the form.
myForm.Show();
see msdn documentation

Owner of Form which is not inside Application.Run

I'm trying to show a Form before an application's start and get its DialogResult, so I'm just creating it and using ShowDialog (because Application.Run's return value is void).
What I'm worried about is that it might get 'hijacked' by mistake by some other Form that might be shown at the time. Not by this application, obviously. See What is the meaning of Form.Show(null)? that it's not advisable to use the parameterless overload of ShowDialog.
I have tested and seen that the Form's Owner property was null. But will it always be so? Or should I create a Form and use that as the Owner without showing it? That seems a strange solution but logically it should avoid any problem. Or will that introduce new ones?
Not by this application, obviously
This is already taken care of by Windows, it enforces a strong separation between processes and windows owned by threads. A typical choice for the owner of a dialog for example is the window returned by GetActiveWindow(). The active window is a property of a thread. Which explains for example why a MessageBox.Show() call made from a worker thread is never modal to the rest of the windows.
Making a window modal against the windows of another process is technically possible but requires lots of effort. The app would have to call AttachThreadInput(), a very unsubtle winapi function that nobody ever calls by accident. Also a great source of deadlock.
Unless you are programming in a boat near the Somali coast, there is no good reason to fear your window getting hijacked.
[STAThread]
static void Main()
{
Form1 form1 = new Form1();
//here I suppose the form you want to show
Form1 form2 = new Form2();
form2.ShowDialog(form1);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(form1);
}
Basically you use ShowDialog() when there are no parents for this window. Usually this happens for the main window. If you are opening one window after another, while closing previous, then there will be multiple ShowDialog()s.
If you are showing dialog (which is also a window), then you can specify it's parent to achieve a certain behavior. To example, when alt-tabbing to that window, it's dialog will be shown in front. Think about this as making child-parent relations.
I don't know about the case, when multiple forms are the claiming same parent. But it sounds like a clear mistake, to example:
public Form1 FormMain = new Form1();
...
// show main form
FormMain.ShowDialog();
...
// somewhere in the main form - show dialog
Form2 form2 = new Form2();
form2.ShowDialog(FormMain);
...
// somewhere in form2 - show dialog
Form3 form3 = new Form3();
form3.ShowDialog(FormMain); // wrong, should be form2!
This is not tested because I couldn't recreate a case where the parent is ever anything but null (i.e. a reference to a third-party un-managed parent), but maybe you could do something like this on your form to set the parent to null if it changes:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
this.ParentChanged += MyParentChanged;
}
public void MyParentChanged(Object sender, EventArgs e)
{
this.Parent = null;
}
}

C# Windows Form Application --> Switching Between Multiple Forms

I am developing Windows Form Application using Microsoft Visual C# 2010
I have two forms I made through Visual Studio GUI now I want to switch to next form on Button Click event this.hide() works but when I write next form name followed by dot than show() method does not show it seems show method is not available for it.
How can I show my new Form as It is not dynamically created I have already created it through Visual Studio GUI
When I Create It Dynamically As
Form myform = new Form();
than
myform.show();
show function exists but in my case this does not exists
Kindly tell me what's the problem
You need to create an object of the Form2 and call the show method on instance of Form2
Form2 form = new Form2();
form.Show();
EDIT: Since you edited your question and added the part Form myform = new Form();. You are actually creating an instance of base class Form. You need to create the instance of your Form which is inheriting Form class.
public partial class Form2 : Form
You need to create instance of Form2 class in this case. But your myForm should have Show method. Are you sure you have System.Windows.Forms; in using statement and Form class is from the System.Windows.Forms;. Try
System.Windows.Forms.Form myform = new Form(); //Although this is wrong but it should have show method
myform.Show();
Make an object of your second form then call Show() method
SecondForm frmsecond = new SecondForm();
frmsecond.Show();
Form myform = new Form(); is not needed here because you are trying to make myform as an object of Form Class
well.. 'you have two forms (and you are not creating them dynamically), and you want to switch from one to another on a click event', isn't ?
For example,consider you have created two forms namely Form1 and Form2 through VS GUI. And if you like to do something like
Form2.Show();
then you can't. Have you looked at your application' entry point, ie. Main() method of your application(is in 'Program.cs' most of the cases). Though you have created your Form1 statically, you will find
Application.Run(new Form1);
You got it now. You cant use a 'type'. You should use a variable.For that reason only we are creating the instance of your second Form and showing it.
Form2 form=new Form2();
form.Show();

How to know if the Form App open or not c#

Does anyone know how can I know if the Windows Form Application (C#) is open or that the client closed it?
(In my App I have a Windows Form Application (Form1) that allow the user to open another Forms (Form2). I want to know if the Form2 is open or close.)
I need to know that because I run the Form2 from a thread, and I want to make the thread runnig until the user close Form2.
Many thanks!
You can check if a form of a given type is open in your application like this (using LINQ):
if (Application.OpenForms.OfType<Form2>().Count() > 0)
{
// there is an instance of Form2 loaded
}
You need to elaborate on your question a bit more. Are you talking about monitoring the application from another application? Or that one form needs to know if another one is open? Or a form needs to know when another form closes?
There are a couple of ways to monitor forms closing within the same application.
Calling ShowDialog() on your form instead of Show() will ensure that code following the ShowDialog() call doesn't get executed until the user closes the form.
The Form class has a Visible property, which returns true/false depending on whether the form is visible or not.
As for the application itself, there is an ApplicationExit event on the static Application class which gets called just before the application closes so you could listen to that event if, for example, you need to perform some clean-up on exit.
If you want to run only single instance of application, check this link.
There you will also see how to check whether a process is still active.
If you mean MDI application with its child forms:
private Dictionary<Type, Form> SingleInstanceForms = new Dictionary<Type, Form>();
public Form ActivateForm<T>() where T : Form, new()
{
Cursor.Current = Cursors.WaitCursor;
if (!this.SingleInstanceForms.ContainsKey(typeof(T)))
{
T newForm = new T();
//setup child
newForm.MdiParent = this;
newForm.WindowState = FormWindowState.Maximized;
//newForm.Icon = Icon;
newForm.FormClosed += new FormClosedEventHandler(delegate(object sender, FormClosedEventArgs e)
{
this.SingleInstanceForms.Remove(sender.GetType());
});
this.SingleInstanceForms.Add(typeof(T), newForm);
newForm.Show();
this.Refresh();
}
Form formToActivate = this.SingleInstanceForms[typeof(T)];
formToActivate.Activate();
Cursor.Current = Cursors.Default;
return formToActivate;
}
this will create the form child if hasn't been created yet and activate it if it has been created.
sample: ActivateForm<dlgChildOne>();

C# Winforms Parent Child Instances

In my application... to navigate between winforms what i do is that i make an object of the form that needs to be shown and i use
Register reg = new Register()
reg.show();
this thing has two problems
if i do it with a button, more than
one instance of same form could be
opened.
if i close through which the instance
was created, the child form stays
opend.
what is the solution....
have the child form take as a parameter the parent form:
Form2 f2 = new Form2(this);
this.hide();
f2.show();
then when you wish to close the new form you just close it and show the parent form again.
code from Form2:
private Form Fatherform;
Form2(Form father){
Fatherform = father;
}
Form2_closeevent( ... )
{
if(Fatherform != null)
Fatherform.show();
Take a look at this code sample from MSDN code gallery. If you go through the code in detail, you should be good to go

Categories

Resources