locked
Switch window modal/not modal RRS feed

  • Question

  • Is there a way to switch window when the window is visible between modal and not modal ?

    Example: when window is read mode window is not modal, when user click in button 'Edit mode' (button is on this window) the window should be switch to modal, and when user click 'Save' the window should be switch to not modal.
    Sunday, June 24, 2007 5:56 AM

Answers

  • Two things happen when you call ShowDialog() to display a modal form.  First, it disables all other forms, then it starts a new message loop so that execution continues after the ShowDialog() call.  You don't need that last feature.  Just disable all other forms:

        private void EnableForms(bool enable) {
          foreach (Form f in Application.OpenForms)
            if (f != this) f.Enabled = enable;
        }
        private void btnEdit_Click(object sender, EventArgs e) {
          EnableForms(false);
          btnSave.Visible = true;
          btnEdit.Visible = false;
        }
        private void btnSave_Click(object sender, EventArgs e) {
          EnableForms(true);
          btnSave.Visible = false;
          btnEdit.Visible = true;
        }

    Monday, June 25, 2007 10:06 AM

All replies

  • I don't think it really is possible. The difference between modal and modelsss windows are that madal windows are displayed using the ShowDialog() method and modeless windows use the Show() method.

    The difference between these methods is pretty important because ShowDialog() will not return until the window it is displaying has closed. But Show() will return immediately, allowing the later code to run.

     

    If you want to swap between modal and modeless then you are going to have to do something different. The only thing I can think of is to create the form as a modeless one and then use the Form's Owner property to simulate the modal behaviour.

    Setting one form to "own" another isn't quite the same as making it modal but it can get you part of the way there.

     

    Regards

    Monday, June 25, 2007 1:00 AM
  • Two things happen when you call ShowDialog() to display a modal form.  First, it disables all other forms, then it starts a new message loop so that execution continues after the ShowDialog() call.  You don't need that last feature.  Just disable all other forms:

        private void EnableForms(bool enable) {
          foreach (Form f in Application.OpenForms)
            if (f != this) f.Enabled = enable;
        }
        private void btnEdit_Click(object sender, EventArgs e) {
          EnableForms(false);
          btnSave.Visible = true;
          btnEdit.Visible = false;
        }
        private void btnSave_Click(object sender, EventArgs e) {
          EnableForms(true);
          btnSave.Visible = false;
          btnEdit.Visible = true;
        }

    Monday, June 25, 2007 10:06 AM
  • it is a good idea, but how can i change the colors of the controls on the parents windows?

    I question, because when the window is modal (by ShowDialog()) the colors of the controls on the parents windows is not change, but in your idea the colors of the controls on the parents windows are changed, becacuse the property Enable = false
    Monday, June 25, 2007 7:44 PM
  • Hmya, could be a Good Thing.  This will fix it:

        private void EnableForms(bool enable) {
          foreach (Form f in Application.OpenForms)
            if (f != this) EnableWindow(f.Handle, enable);
        }
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        private static extern bool EnableWindow(IntPtr hWnd, bool enable);

    Monday, June 25, 2007 8:29 PM