Link to home
Start Free TrialLog in
Avatar of ayha1999
ayha1999

asked on

Modal window

Hi,

I have an mdi form and a standard on C# 2010 windows application. How can I load the standard form as modal window while loading the the mdi form. i.e. mdi form on the background and the standard form as modal window. if user click the close button on the modal window, the application should exit. If user cick a button on the modal window, the modal window should close and the mdi form should be active.

Please help.

ayha
Avatar of Rimvis
Rimvis
Flag of Lithuania image

Hi ayha1999,

To display a modal form, use this:
            frmSampleModalForm f = new frmSampleModalForm();
            f.ShowDialog();

Open in new window


To handle close event:

    public partial class frmSampleModalForm : Form
    {
        bool bOKPressed = false;

        public frmSampleModalForm()
        {
            InitializeComponent();
        }

        private void cmdOK_Click(object sender, EventArgs e)
        {
            bOKPressed = true;
            this.Close();
        }

        private void frmSampleModalForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (!bOKPressed) Application.Exit();
        }
    }

Open in new window

Avatar of ayha1999
ayha1999

ASKER

I am not able to load the mdi form in the background. Only when the child form close, the mdi form comes up.

Program.cs

 static void Main ( )
       {
          Application.EnableVisualStyles ();
          Application.SetCompatibleTextRenderingDefault (false);
          Application.Run (new mdi ());
       }


MDI form

 private void mdi_Load (object sender, EventArgs e)
       {
          login f = new login ();
          f.ShowDialog ();
       }

Please check if I need to change something.

thanks
ASKER CERTIFIED SOLUTION
Avatar of Rimvis
Rimvis
Flag of Lithuania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
the mdi form is loading now but the login form(child) can be moved out of the parent window. How can I prevent it?

when I tried

login.MDIParent = this;

getting error.  For closing the application when clicking close button of the login window, it doesn't close.

thanks

ayha
You don't specify MDI parent for modal form. Just remove this line.
Even without this line, the child window can be moved out of the parent window. is there any other I set the login form as child .

thanks
Why do you want to set login form as child window? As I understand, It should be used only once, during application startup. The point of having child forms in MDI is to be able to interact with them simultaneously, or to switch between them. That's not the case with login, is it?
Yes. Actually not for login I want it, but for other forms in the application.
OK then. Just don't set MDIParent for modal forms
Thanks