Show a form from another form - c#

When I want to Display a form (C#) by clicking a button in another form I usually create an object from the form that I want to show and use the show method :
Form2 f2 = new Form2();
f2.Show();
or I work with the "Owner" :
Form2 tempForm = new Form2();
this.AddOwnedForm(tempForm);
tempForm.Show();
the two ways generate the same results but what is the best and what are the differences between them?

The only difference apart from the naming is that in the second you call AddOwnedForm, and in the first you do not. Looking at the documentation we see:
When a form is owned by another form, it is minimized and closed with the owner form. For example, if Form2 is owned by form Form1, if Form1 is closed or minimized, Form2 is also closed or minimized. Owned forms are also never displayed behind their owner form. You can use owned forms for windows such as find and replace windows, which should not be displayed behind the owner form when the owner form is selected.
So if you want this behavior of the forms being minimized together, and one always being shown above the other, use AddOwnedForm. If you don't want this behavior, don't use it.

Microsoft uses Form f = new Form(); f.Show(); by default when creating a new Windows Forms project to show the main form, and there is probably negligible difference (in performance) between such methods. Using the Show() method, instead of just setting f.Visible = true; is also more logical.
When you use AddOwnedForm() you essentially lock the forms together in such way that when one form is minimized, the other is also. The form is also always displayed on top of the owning form, similar to a modal dialog.

Related

Closed form still showing in taskbar

Creating a multi-form program linked to a database with login features in c#.
Tried moving between forms using show(), showdialog() and close(), dispose(), hide().
Once past the login form, the program will not properly close the forms.
It closes the form so the user can no longer access its controls, however, the 'closed' form remains completely visible in the windows taskbar and tab menus.
The user can even hover over the taskbar icon for said 'closed' form and see all the information there!
As the program will be handling sensitive personal information. I need help to stop this problem from happening.
Code from a back button on secondary form aimed to completely close the active form and open the main form.
"Curform" is defined outside the method as it is used in multiple buttons within the program.
Form CurForm = Form.ActiveForm;
public void Btn_Back_Click()
{
var MainForm = new MainForm();
MainForm.Show();
CurForm.Close();
}
Use directly active form instead of curForm
Form.ActiveForm.Close();
If you want to use curForm in other parts of the code update curForm when you have created the new form, like this:
Form CurForm;
public void Btn_Back_Click()
{
CurForm= new MainForm();
CurForm.Show();
// Do some things
CurForm.Close();
}

creating the windows forms with pages

I have created the multiple windows form in C#.For EX. We have two forms and we have next button in Form1 and previous button in Form2 . But when we try to go form Form2 to Form1 using previous button Form2 is not closing and Form1 is appearing on Form2. To show forms we are using ShowDialog. So how can we create the form one linked to another as pages.
What you can do is like this:
In form1's button handler,
form2 f2=new form2();
f2.show();
this.hide();
And in Form2's button handler,
form1 f1=new form1();
f1.show();
this.hide();
But make sure you write proper code in close button handler as the forms are just hidden. Not closed.
It looks like you're trying to implement a "wizard" in your application.
The more typical approach is to have each "page" as a control/user control and load the appropriate step in the same dialog as the user moves along.
You may benefit from looking at some examples leveraging existing libraries to make this simpler. One example is here: https://www.codeproject.com/articles/120607/simple-wizard-for-winforms
This answer also covers this topic and provides a few more resources:
Which wizard control can I use in a WinForms application?

C# Accessing controls from one form that is opened in another form that was opened by that form

so I have looked this up so much and nothing fits exactly what I want.
I have one form open called "Main" it has a button in it that opens up a form using the code
AddModification modification = new AddModification();
Modification.Show();
Now this opens the form correctly and all. But the problem is that both forms are open. So the AddModification form is a popup form that changes things in the Main Form.
So the AddModification form is opened / shown but the form itself is broken because I can't put Main main = new Main(); in the AddModification form without a stack overflow problem.
So how the heck do I access any of Main's controls in AddModification if I need to keep main and AddModification open and not disposed?
You need to pass the first form as a constructor parameter to the second form.
Have you tried something like 'this.Owner' ?

windows form using C#

I am new to windows form using c#. I have to create a login form and then need to show logged in user relevant data from reading xml.
Should I create two different form for login and showing data or should I create only on form and make disappear login control and show data in the same form?
If I go with having two forms than how to make disappear login form and show second window form?
You must create two diferents forms and hide the login form
for hide the form you can use
this.hide()
for show the form you must create an instance the next form:
Form2 form = new Form2();
form.Show();
I think it would be easier with two forms. If you use Windows Forms you could show your loginform as Dialog: https://msdn.microsoft.com/de-de/library/c7ykbedk%28v=vs.110%29.aspx
After that you could load your mainform like this: https://msdn.microsoft.com/de-de/library/ws1btzy8%28v=vs.90%29.aspx

How can I have multiple forms start at once in C# .NET?

In C#, I'm trying to get two forms (but probably three eventually) to start at the same time... I've tried adding a new "Application.Run" to my Program.cs file, but it only starts the second form after the first one closes.
So how could I create something like that? Similar to a program like Lazarus.
You simply have to show your form before invoking Application.Run().
var form1 = new Form1();
var form2 = new Form2();
form1.Show();
form2.Show();
Application.Run();
Word of warning here, since no form is tied to the Application.Run call, you will need a way to tell the application to exit when all your forms are closed.
To display a Form you have 2 methods:
Show() - display a non modal dialog (is what you want); also you need to add Application.Run for to work.
ShowDialog() - display a modal(some blocking) dialog; a modal dialog capture all the input for the current thread.
If you want a interface like Lazarus, google by the "MDI application".
I think you probably should decide on one form to be your "primary" form, and then have the other form(s) be member variables of your main form. Then just have the Load event of your primary form also show the secondary form(s).
so,
class MainForm : Form {
readonly Form _otherForm = new OtherForm();
override OnLoad(EventArgs args) {
_otherForm.Closed += // add a handler for what happens when otherForm is closed.
_otherForm.Show();
base.OnLoad(args);
}
}
There may be a better way of doing this but that's what I would do as a first shot.

Categories