开发者

c# run a Form then show another form over it with showDialog?

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


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.


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


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜