C# Winforms Parent Child Instances - c#

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

Related

Setting a form as an MDI child of a main form?

I have a main form application which is an MDI container. A user can press a button to make a new form pop up (not as an MDI child), and then from this new form that popped up, I want to be able to have a button that creates a different form as an MDI child.
In the main form I have:
ResSelectForm resSelectForm = new ResSelectForm();
resSelectForm.Show();
So in the resSelect form that popped up, when the user presses an OK button, I have:
ImageForm imageForm = new ImageForm();
imageForm.MdiParent = Mainform; // doesn't work
imageForm.Show();
I get the following error:
Error CS0119 'MainForm' is a type, which is not valid in the given context
The problem you are having is you were trying to access the Mainform type and not an instance of it. To fix the problem you would have to pass the instance to the ResSelectForm constructor like so:
ResSelectForm resSelectForm = new ResSelectForm(this);
Then in the ResSelectForm constructor do this:
private Mainform _mainform; //Variable to use throughout the class
public ResSelectForm(Mainform mainform)
{
_mainform = mainform;
}
Lastly whenever you need to access Mainform you'd access the variable, in your case like so:
imageForm.MdiParent = _mainform;
I just ended up using a dialog box for it, since it's essentially the same thing:
https://www.youtube.com/watch?v=8aDsXyiBLsI

Can't close a form from other form

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.

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();

Saving Data on Parent Window from a Child Window?

I have a Windows application.
The windows application has our XML Library on it.
public NetspotXMLLibV1 XMLLib;
public Form1()
{
InitializeComponent();
XMLLib = new XmlLibrary.NetspotXMLLibV1();
}
It also has a custom control.
That custom control has a button on it that has
SelectWidgitWindow widgit = new SelectWidgitWindow();
widgit.ShowDialog();
when clicking a button, it opens up the new window. I do some stuff on this new window. When I click a button on the new window I want to save stuff on the Form1 Window
How do I access this on my new Window (SelectWidgitWindow ) ?
Ie
form1.XMLLib.Add(ItemForProcessing);
or
Windows(1).XMLLib.Add(ItemForProcessing);
Please help
Use the ShowDialog overload to which you can pass an owner for the new form:
Form2 f = new Form2();
f.ShowDialog(this.Parent);
and in Form2:
((Form1)Owner).MyProperty = 11;
Couple of ways:
1) Expose the items you want the parent form to extract as properties of the child form. In the parent form's code, when ShowDialog returns you can get the values you want to save from the properties.
2) Pass a reference to the parent form to the form (perhaps when it's constructed). Then the child form can call some method or set some properties on the parent form. Note that this is not preferred as it couples the child for to the parent form, meaning it couldn't be called from any other form.

Categories

Resources