windows form using C# - 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

Related

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?

How to prevent clicks outside the current form?

I want the form which will open when the user click a button, I don't want the user to click anywhere else outside this form(preventing him from clicking any other buttons on the parent form), I want to restrict his actions inside this form until he close it (For sure this form is top most), and I want the form to make alert(flicker) to any clicks outside it so the user will understand that he have to close it first.
When opening the form instead of
Form form = new Form();
form.Show();
use
Form form = new Form();
form.ShowDialog()
Super easy fix!
I assume your code to show the form right now is
form.Show();
To create/show an instance of the form that is modal (restricts clicking to that form) all you need to change is that line to:
form.ShowDialog();
Liam

new form doesn't allow to go back to the main form

I'm opening a new form like this:
Form formulario = new Editar_POIs());
formulario.ShowDialog();
Now, when the new form opens and I want to go back to the Main form (with the new form opened), I can't... Either if I click in the main form or if I try to switch using Alt + Tab it won't go back to the main form. I then need to close the opened form to be able to use the Main form again.
Any ideas how to avoid this? I want the user to be able to have the 2nd form opened while he does some operations in the main form, then go back to the 2nd form and enter some values the Main form displays.
Use
formulario.Show();
ShowDialog() does not let you go back and use the main form until the 'child' form is closed.

Display small form while main form is "locked"

i'm thinking about writing a WPF program that would require login and password at the app startup.
I thought about small form with two textboxes as a login form. User will have to fill in his details and then the main form of the application will be unlocked.
How will you solve this?
Thanks for your answers, daemonsvk
This is standard behavior for the Window.ShowDialog() call. Other windows will be disabled.
I would make the initial startup form your login page and authenticate the user, if their details are correct hide the login form and show you other form.
if(user is authed)
{
this.Hide(); //Hide the login form
mainAppForm.Show(); //or Form.ShowDialog(); //Shows the main form
}
The simplest way to do this is to first show (using ShowDialog, which blocks i.e. waits until the form is closed before continuing to the next line of code) the login form. If the login is successful, you dispose the login form and then show your main form; if the login fails, you end the application.
If, however, you want your main form to be visible underneath the login form (a not unreasonable requirement), then you need to first show the main form, and then display the login form (modally) from a method in the main form. In WinForms this requires some kind of hack, since you couldn't show the login form from either the main form's constructor or its Load event (as the main form won't yet be visible when the login form appears).
WPF may handle this better now.

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