Reputation: 11237
I have an application that prompts the user to enter his password into a form. However, the main form shows right after the secondary form. How do I make the main form not show up until the user entered his/her password?
Edit:
//main form:
public Form1()
{
new InputPswrd().Show();
InitializeComponent();
}
Upvotes: 2
Views: 2346
Reputation: 5380
Just my 2 cents, but you could make you Login Form the first one shown, and call your "Main" Form from the Login Form.
Assuming typical Windows Forms application, you can change the contents of Program.cs like so:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new LoginForm()); //calling the Login form instead
}
}
Seems to me like a more logical approach than trying to prevent the main form from showing up in its constructor.
Cheers
Upvotes: 4
Reputation: 51
The Form.ShowDialog method has an optional argument, owner, that can be used to specify a parent-child relationship for a form. For example, when code in your main form shows a dialog box, you can pass Me (in Visual Basic) or this (in C#) as the owner of the dialog box to establish your main form as the owner, as the following code shows.
Form f = new Form();
f.ShowDialog(this);
for further reference please refer to: http://msdn.microsoft.com/en-us/library/aa984358%28v=vs.71%29.aspx
Upvotes: 0
Reputation: 66449
Change .Show()
to .ShowDialog()
, to display the password form as a modal dialog, which will prevent the next line of code from executing until you close the password form.
var inputPswrdForm = new InputPswrd();
inputPswrdForm.ShowDialog();
I assume you'll also want to know if the user's password was valid before proceeding.
You could create a property on the second form that you could check once control returns to the first form, something like:
public bool IsUserAuthenticated { get; set; }
Then in the first form:
var inputPswrdForm = new InputPswrd();
inputPswrdForm.ShowDialog();
if (!inputPswrdForm.IsUserAuthenticated)
// password was wrong, take some action
InitializeComponent();
Upvotes: 2