Display small form while main form is "locked" - c#

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.

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

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

Why hiding login form is closing Mainform unexxpectedly?

I have two forms in my application. When user opens up application, login Form appears where user enters login information and after he enters correct information, the login form should disappear and the Main Form must appear. The logic i am using to show Main form is:
if (this.userNameFld.Equals(this.userName) && this.pwdFld.Equals(this.pwd))
{
MainForm domain = new MainForm();
domain.Show();
this.Hide();
}
PROBLEM
The problem i am facing is that as the Login form disappears, suddenly the Main form appears and gets vanished at once. And for what reason its happening, i don't know. Please help me find one. Moreover, I don't have any FormClosing event in my Login form. So what could be the bug?
To resolve this you need to call the Main form from the main application thread instead of inside of the login form.
One option to do this is to run the application from a standard class (not a form) that calls the login form and then have some logic that creates the main form.
Another way is to create the Main form first but call the login form from the Main form before it is visible.
Form frmMain = new frmMain();
Then inside of frmMain , instantiate the login form
this.Hide();
StuRec_frm domain = new StuRec_frm();
domain.ShowDialog();
This solved my problem. Thanks everyone.
this.Hide();
is perhaps what is closing - remove this.Hide(); and try again
This code is just to demonstrate login and subsequent form usage(read comment to scott_f's response). Not really a response to actual question.
// Assuming correct login
// This will close the main form
//MainForm mainForm = new MainForm();
//mainForm.Show();
//Hide();
// After some more code
//Close();
// this will not close the main form as control execution will not move ahead until Main form is closed
MainForm mainForm = new MainForm();
Hide();
mainForm.ShowDialog();
Close();

Login screen using user custom control

I have followed some advices I got here today and would need a bit more help now:
I have an user control with login logic.
In mainForm I am using this:
private void Form1_Load(object sender, EventArgs e)
{
LoginScreen login = new MainMenu();
login.Parent = this;
login.Dock = DockStyle.Fill;
login.Show();
}
But I guess it is not modal and thus does not stop the original Form application. Sure I would need the main Form to do not continue until the login form is closed (and login sucessfull).
Would using an event correct? Let login object to raise an event that login was successfull and let the MainForm handle it - run the app?
EDIT: This is user control, no ShowDialog method available.
Simply use ShowDialog() instead of Show().
Besides, ShowDialog() returns a DialogResult, so you can check if user doesn't press OK and close the form in that case:
if(form.ShowDialog() != DialogResult.OK)
{
this.Close();
}else
{
//if login it's ok continue with main form loading...
}
EDIT:
Given that it's a usercontrol:
Create a new form class and add the user control in it (e.g. using designer)
Instanciate it and call ShowDialog() in your FormLoad code (the code is similar to the one you posted, but you need to instanciate the login form instead of your usercontrol)
Instead of login.Show(); you might uselogin.ShowDialog(this);, making it a modal pop-up for the form.
If it's not derived from the Form class (which is odd), just create a windows form and put your control on it, then call ShowDialog method of the form. You'll have to wire closing of the form to the controls on your login screen somehow, though. If it fires some events it shouldn't be a problem.

Categories