Search code examples
c#winformsshowdialog

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


Solution

  • 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.