WinForms programming - Modal and Non-Modal forms problem

后端 未结 5 1013
逝去的感伤
逝去的感伤 2020-12-17 03:25

I have a problem with modality of the forms under C#.NET. Let\'s say I have main form #0 (see the image below). This form represents main application form, where user can pe

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-17 03:51

    It looks to me like you could use an MDI application setting the Form #0 IsMdiContainer property to true.

    Then, you could do something alike:

    public partial class Form0 {
        public Form0 {
            InitializeComponent();
            this.IsMdiContainer = true; // This will allow the Form #0 to be responsive while other forms are opened.
        }
    
        private void button1_Click(object sender, EventArgs e) {
            Form1 newForm1 = new Form1();
            newForm1.Parent = this;
            newForm1.Show();
        }
    }
    

    Using the ShowDialog() as you stated in your question will make all of the forms Modal = true.

    By definition, a modal form is:

    When a form is displayed modally, no input (keyboard or mouse click) can occur except to objects on the modal form. The program must hide or close a modal form (usually in response to some user action) before input to another form can occur. Forms that are displayed modally are typically used as dialog boxes in an application.

    You can use this property [(Modal)] to determine whether a form that you have obtained from a method or property has been displayed modally.

    So, a modal form shall be used only when you require immediate assistance/interaction from the user. Using modal forms otherwise makes believe that you're perhaps running into a wrong direction.

    If you do not want your main form to be an MDI container, then perhaps using multithreading is one solution through a simple BackgroundWorker class is the key to what you want to achieve. Thus, it looks to me like a design smell...

    • What is it you want to do, apart of making your main form responsive, etc.
    • What is it you have to do?

    Explaining what you have to do, we might be able to guide you altogether into the right, or at least perhaps better, direction.

提交回复
热议问题