locked
Modal Form RRS feed

  • Question

  • Hi,

    I am creating and showing a Form from a C# form application. I want to show this form as a modal, so that, as long as this form is shown I cannot access the parent form, but I dont want to use the ShowDialog() because the application stops there. I dont want to stop the application.

    How can I show a form as a modal form without using ShowDialog() function?

    Saturday, August 28, 2010 2:17 PM

Answers

  • One possible is to override the Forms Show(...) method thus:

      public new void Show(IWin32Window owner)
      {
       Parent = (Form) owner;
       ((Form)Parent).Activated += ParentActivated;
      }
    
      private void ParentActivated(object sender, EventArgs e)
      {
       Activate();
      }
    

    DMAC
    • Marked as answer by Kikeman Saturday, September 4, 2010 8:46 PM
    Tuesday, August 31, 2010 4:04 PM

All replies

  • I do not understand fully, what you mean. What is stopping what you want to keep running?

    If you need something running in the background, then maybe a BackgroundWorker is something you want to have a look at. Or maybe you do not want a modal form - maybe you just want to have it always on top? But Modal form means, that other forms does not have to work on their events.

    With kind regards,

    Konrad

    Saturday, August 28, 2010 2:27 PM
  • I believe that when the OP says ShowDialog is stopping the application, he's referring to the fact that its a blocking call.

    As Konrad suggests, you could make the form appear always on top by setting the TopMost property of the form to true, however this won't make the form modal.

    Kev 

    Saturday, August 28, 2010 2:33 PM
  • Hi,

    I am creating and showing a Form from a C# form application. I want to show this form as a modal, so that, as long as this form is shown I cannot access the parent form, but I dont want to use the ShowDialog() because the application stops there. I dont want to stop the application.

    How can I show a form as a modal form without using ShowDialog() function?

    What do you mean by the application stop there?

    for you to do what you talk about you need to edit your cs file that belong to your program. It's called Program.cs.

    There you add the run of others form, something like this:

     

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
      static class Program
      {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
          Application.EnableVisualStyles();
          Application.SetCompatibleTextRenderingDefault(false);
          Application.Run(new Form1());
          Application.Run(new Form2());
          Application.Run(new Form3());
        }
      }
    }
    

     

    After that you can do something like this:

     private void button6_Click(object sender, EventArgs e)
      {
        Form3 frm2 = new Form3();
        frm2.MdiParent = frm1.MdiParent;
        frm2.Show();
        this.Close();
      }

    For more information have a look here: http://msdn.microsoft.com/en-us/library/aa984358(VS.71).aspx


    Just Be Humble Malange!
    Saturday, August 28, 2010 2:40 PM
  • By stopping I mean that when I call:

    MyForm.ShowDialog();   //The application waits here, it does not go to the next instruction.

    InstanceOfObjectX.DoSomething();     //It is called only when the MyForm is closed.

    ...

    ...

    //somewhere in the application:

    MyForm.Close();   //The parent gui now can be accessed

     

    I need to show the form, and continuing the execution of the program but the GUI must be blocked until the form is closed from somewhere else in the application. Maybe an external event could close the window like ending of reading a database.

    Saturday, August 28, 2010 3:56 PM
  • Start a one shot timer and show the modal form in the tick event.  A modal form only prevents UI interaction.  Other code can continue to run.
    Saturday, August 28, 2010 4:01 PM
  • By stopping I mean that when I call:

    MyForm.ShowDialog();   //The application waits here, it does not go to the next instruction.

    InstanceOfObjectX.DoSomething();     //It is called only when the MyForm is closed.

    ...

    ...

    //somewhere in the application:

    MyForm.Close();   //The parent gui now can be accessed

     

    I need to show the form, and continuing the execution of the program but the GUI must be blocked until the form is closed from somewhere else in the application. Maybe an external event could close the window like ending of reading a database.

    Sounds like you should think or use Threads....
    Just Be Humble Malange!
    Sunday, August 29, 2010 1:08 PM
  • Sounds like you should think or use Threads....
    Bad thinking in this situation.  Strictly UI.
    Sunday, August 29, 2010 1:45 PM
  • I need to show the form, and continuing the execution of the program but the GUI must be blocked until the form is closed from somewhere else in the application. Maybe an external event could close the window like ending of reading a database.

    Thanks John...

    But i dont know or understand, what the OP is really trying to achieve or do. Can you?

     


    Just Be Humble Malange!
    Sunday, August 29, 2010 5:42 PM
  • or understand, what the OP is really trying to achieve or do. Can you?

    Show a modal form but continue processing:

    using System;
    using System.Windows.Forms;
    namespace WindowsFormsApplication1
    {
      
    public partial class Form1 : Form
      {
        
    public Form1()
        {
          InitializeComponent();
          Tmr.Tick += Tmr_Tick;
        }
        
    private Timer Tmr = new Timer();
        
    private Form2 Frm2 = new Form2();
        
    private int TmrCnt;
        
    private void button1_Click(object sender, EventArgs e)
        {
          Tmr.Interval = 1;
          TmrCnt = 0;
          Tmr.Start();
          Frm2.ShowDialog();
        }
        
    public void Tmr_Tick(object sender, EventArgs e)
        {
          Tmr.Stop();
          Frm2.Text = 
    "Counting " + TmrCnt.ToString();
          
    //Do time consuming process
          System.Threading.Thread.Sleep(1000);
          TmrCnt += 1;
          
    if (TmrCnt < 10)
          {
            Tmr.Start();
          }
          
    else
          {
            Frm2.Hide();
          }
        }
        
    private class Form2 : Form
        {
        }
      }
    }


    A modal form blocks user input and the method containing the ShowDialog call, but it doesn't block the thread.  The UI can be updated and other processing performed.
    • Proposed as answer by Alan_chen Wednesday, September 1, 2010 5:59 AM
    Sunday, August 29, 2010 6:09 PM
  • If I use the timer event to call ShowDialog, and then close it from some other event in the application, lets say, from a button event just to test. Is it ok to program like this?
    Monday, August 30, 2010 2:58 PM
  • If I use the timer event to call ShowDialog, and then close it from some other event in the application, lets say, from a button event just to test. Is it ok to program like this?

    It's a modal form.  Closing it from a button on the form would be the normal way to close it.
    Monday, August 30, 2010 3:05 PM
  • Yes but this button would not be in the same modal form, it would be somewhere else in the application, maybe from another form button
    Monday, August 30, 2010 5:26 PM
  • Yes but this button would not be in the same modal form, it would be somewhere else in the application, maybe from another form button

    It doesn't matter.  Only buttons on the modal form are active.  That's the definition of modal.  If you want the entrie UI to be active, show the form non-modally using Show() which is non-blocking.
    Monday, August 30, 2010 5:59 PM
  • One possible is to override the Forms Show(...) method thus:

      public new void Show(IWin32Window owner)
      {
       Parent = (Form) owner;
       ((Form)Parent).Activated += ParentActivated;
      }
    
      private void ParentActivated(object sender, EventArgs e)
      {
       Activate();
      }
    

    DMAC
    • Marked as answer by Kikeman Saturday, September 4, 2010 8:46 PM
    Tuesday, August 31, 2010 4:04 PM
  • Maybe show another form on another thread, and in that other form have a timer or a mechanism to close it.

     private void ShowForm()
        {
          ModalScreen s = new ModalScreen(); //this is another form
          Process p = Process.GetCurrentProcess();
          s.ShowDialog(new WindowWrapper(p.MainWindowHandle.ToInt32()));
        }
    
    then call it:
    
    new Thread(ShowForm).Start();
    

     

    Tuesday, August 31, 2010 9:13 PM
  • Maybe show another form on another thread, and in that other form have a timer or a mechanism to close it.
    You seem to have a total grasp of Windows messaging and threading.
    Tuesday, August 31, 2010 9:20 PM
  • Thanks John. I have unfortunately gone through some pains to do what he is asking!
    Wednesday, September 1, 2010 12:58 PM
  • Thanks John. I have unfortunately gone through some pains to do what he is asking!

    What is he asking?  He wants to show a modal form, but close it using a button on another form in the appliction.
    Wednesday, September 1, 2010 1:09 PM
  • How can I show a form as a modal form without using ShowDialog() function?

     

    I did not see the button part in his original question.

    Wednesday, September 1, 2010 1:41 PM
  • So, without a timer to close the modal dialog, I can't access any other form buttons to close it!
    Wednesday, September 1, 2010 3:17 PM
  • The originator does not want to close it, he/she just doesn't want the app to be locked up while the dialog is active.

    My solution above is similiar to that used in my work to solve am identical situation,

    1. Pass the owner to the dialog
    2. Add an Atcivated event to the owner so that when an attempt to make it active is fired, the dialog form is activated instead.

    This would solve his problem.


    DMAC
    Wednesday, September 1, 2010 3:21 PM
  • The originator does not want to close it, he/she just doesn't want the app to be locked up while the dialog is active.

    "If I use the timer event to call ShowDialog, and then close it from some other event in the application, lets say, from a button event just to test. Is it ok to program like this?"

    "Yes but this button would not be in the same modal form, it would be somewhere else in the application, maybe from another form button"

    My interpretation is that he wants to close the modal form using a button not on the modal form.  You interpretation is obviously different.  If the buttons not on the modal form are not "locked up" what is your definition of "modal".

    Wednesday, September 1, 2010 3:52 PM
  • what is your definition of "modal".

    From Microsoft "A modal form or dialog box must be closed or hidden before you can continue working with the rest of the application."

    This has a slight problem, say in a data entry form an error occurs when saving, the programmer will sometimes show a dialog alerting the user to the fact. If this dialog is Modal, then no other operation can be performed until the form is closed.

    If the user needs to access another part of the program to rsolve the issue he can't, but with a "partially modal" form (only modal to its owner), the user can keep the error on the screen and continue to use the rest of the program to try and resolve the issue


    DMAC
    Wednesday, September 1, 2010 5:35 PM
  • what is your definition of "modal".

    From Microsoft "A modal form or dialog box must be closed or hidden before you can continue working with the rest of the application."

    This has a slight problem, say in a data entry form an error occurs when saving, the programmer will sometimes show a dialog alerting the user to the fact. If this dialog is Modal, then no other operation can be performed until the form is closed.

    If the user needs to access another part of the program to rsolve the issue he can't, but with a "partially modal" form (only modal to its owner), the user can keep the error on the screen and continue to use the rest of the program to try and resolve the issue

    I'm from the old school.  There used to be "System.Modal" and "Appliction.Modal".  When did Modal become a double like Opacity?
    Wednesday, September 1, 2010 5:56 PM
  • We can just pass the parent object of the form for example.,if you want make frmA as modal form which is displaying under parent form FRM then 

    When you are showing that form using showdialog() method you can just pass this.parent.

    like : ShowDialog(this.Parent); in the show dialog function of child form.

    Monday, June 25, 2012 1:13 PM