Reputation: 8628
I want to run my form (with all controls disabled) and over it there will be another form for username and password run as showDialog! people will not be able to go to the main form without login!
private void Form1_Load(object sender, EventArgs e)
{
this.Show();
Form2 f2 = new Form2 ();
f2.ShowDialog();
}
I tried the code above and it dose not work as it should!
how I can achieve it the way I need?
cheers
Upvotes: 0
Views: 4705
Reputation: 19612
I typically use the following pattern if I want to do sth. after the Form has fully loaded:
public partial class BaseForm : Form
{
public event EventHandler Loaded;
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
Application.Idle += OnLoaded;
}
protected void OnLoaded(object sender, EventArgs e)
{
Application.Idle -= OnLoaded;
if (Loaded != null)
{
Loaded(sender, e);
}
}
}
If I derive my main form from BaseForm I have a Loaded
event which, in your case, I would use as follows.
static class Program
{
[STAThread]
static void Main()
{
var mainForm = new MainForm();
mainForm.Loaded += (sender, e) => { new LoginDialog().ShowDialog(mainForm); };
Application.Run(mainForm);
}
}
Upvotes: 0
Reputation: 12606
From the parent form:
childForm.ShowDialog(this);
That will make the child form modal to the parent form. As far as the location goes, there is a property off of the form (that you will want to set on the child form) that tells it where to start (center screen, center parent, etc)
System.Windows.Forms.Form implements IWin32Window, this is why it works.
Upvotes: 2
Reputation: 1064204
It isn't clear what the issue/question is, but you could try making sure you pass in the parent-form, i.e.
using(var childForm = new MySpecialLoginForm(...)) {
childForm.ShowDialog(this);
}
Upvotes: 2
Reputation: 1107
erm...
DialogResult result = mySecondForm.ShowDialog()
That will disable the parent form until this one is closed. DialogResult will be an enum value that is something like OK/Cancel/YesNo etc
Upvotes: 0