creating the windows forms with pages - c#

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?

Related

C# How to pause Form1 while Form2 is active?

As the title says; that's all I need. I have 2 forms : Form1 and Form2. At some point of my code I want to do Form2.ShowDialog() and after Form2 is closed I want to resume Form1. How can i do this?
I tried using Thread.Sleep(sometime) but this will just disable any controls,timers, etc from Form1 and resume after the period passed. The problem is that I cant know how much time it will take for my user to press something in Form2.
As per KDecker mentioned in a comment to your question:
If you want to show something Modally (that is the form behind is unusable) use the ShowDialog() method. This will make it so you can only use the form that ShowDialog() was called on.
If you want to show it Modeless, then just use the Show() method on Form. This will make it so you can use both forms
See the MSDN documentation for reference:
https://msdn.microsoft.com/en-us/library/aa984358(v=vs.71).aspx

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

Docking a WinForm inside another WinForm

I'm stuck at a problem and the old answers regarding the same problem are not quite recent enough so i thought it'd be okay to ask again.
My question is: How can i dock one form inside another form? Would it be more appropriate to use a Panel and a Form instead? Is the first option even possible?
Thanks in advance.
Create 2 forms, Form1 and Form2. Set TopLevel property of Form2 to false. In form load for Form1 add code
private void Form1_Load(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
this.Controls.Add(frm2);
}
This will include form2 in form1, you then have to set properties on form2 if you want to remove the title bar to make the form more like a Panel.
If you want to have some reusable panel that may be used on forms I think User Control will be what you are looking for.

What is the best way to invoke action in Form1 When Form2 is closed without using ShowDialogue in C# Windows Form Application?

I've a project that contain 2 forms e.g. (Form1 , Form2 ).The user should be able to use any of the 2 forms while both of them are visible(i.e. Can use Form1 While Form2 is Open).At the same time when Form2 is closed there is an action that should be invoked in Form1 related to From2_close. I can't use Form2.ShowDialogue as it trap focus only to Form2 but user should be able to edit some data in Form1 while Form2 is loaded. Hope the question is clear, please help. Thanks
Your question is not entirely clear, but I think you are trying to send a notification to a given set of forms based on any other forms actions. This type of notification scenerio can be very easily handled by implementing the Observer Pattern. Basically you have a single object that is the "observer" and it watches subscribers that register with it for messages. When the observer receives a message from one of its subscribers (ie a form) it then notifies the other subscribers that it received a message.
Hope this helps.
Handle either the FormClosed or VisibileChanged event. When one of those events fires you know the form has been closed or hidden and can react accordingly.
ShowDialog creates a modal dialog, which means that no other code in the calling thread will execute until the form closes. In this case, the dialog is blocking the GUI thread on your other form, which means that the user can't use it.
Instead of using ShowDialog to create an instance of Form1 from Form2 (or vice-versa), as I'm guessing you are currently doing it, just instantiate the other form.
Form1's Load handler:
Form2 form2 = new Form2();
form2.Show();
*EDIT
To allow each form to be closed without the other closing or the application exiting, do this:
In Program.cs, you'll see a call to Application.Run that creates a new instance of Form1 and starts the application. Replace that with the code from above, repeated twice, to create Form1 and Form2. After those two calls, add in a plain Application.Run() call.
Then, in each form, handle the FormClosing event by instead exiting the application if both forms are closed.

Show a form from another form

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.

Categories