How to disable the parent form when a child form is active?

前端 未结 13 2132
闹比i
闹比i 2020-12-12 23:26

How to disable a parent form when child form is active using c#?

相关标签:
13条回答
  • 2020-12-12 23:53

    @Melodia

    Sorry for this is not C# code but this is what you would want, besides translating this should be easy.

    FORM1

    Private Sub Form1_MouseEnter(sender As Object, e As EventArgs) Handles MyBase.MouseEnter
        Me.Focus()
        Me.Enabled = True
        Form2.Enabled = False
    End Sub
    
    Private Sub Form1_MouseLeave(sender As Object, e As EventArgs) Handles MyBase.MouseLeave
        Form2.Enabled = True
        Form2.Focus()
    End Sub
    

    FORM2

    Private Sub Form2_MouseEnter(sender As Object, e As EventArgs) Handles MyBase.MouseEnter
        Me.Focus()
        Me.Enabled = True
        Form1.Enabled = False
    End Sub
    
    Private Sub Form2_MouseLeave(sender As Object, e As EventArgs) Handles MyBase.MouseLeave
        Form1.Enabled = True
        Form1.Focus()
    End Sub
    

    Hope this helps

    0 讨论(0)
  • 2020-12-12 23:55
    ChildForm child = new ChildForm();
    child.Owner = this;
    child.Show();
    

    // In ChildForm_Load:

    private void ChildForm_Load(object sender, EventArgs e) 
    {
      this.Owner.Enabled = false;
    }
    
    private void ChildForm_Closed(object sender, EventArgs e) 
    {
      this.Owner.Enabled = true;
    } 
    

    source : http://social.msdn.microsoft.com/Forums/vstudio/en-US/ae8ef4ef-3ac9-43d2-b883-20abd34f0e55/how-can-i-open-a-child-window-and-block-the-parent-window-only

    0 讨论(0)
  • 2020-12-12 23:59

    While using the previously mentioned childForm.ShowDialog(this) will disable your main form, it still doesent look very disabled. However if you call Enabled = false before ShowDialog() and Enable = true after you call ShowDialog() the main form will even look like it is disabled.

    var childForm = new Form();
    Enabled = false;
    childForm .ShowDialog(this);
    Enabled = true;
    
    0 讨论(0)
  • 2020-12-13 00:01

    If you are just trying to simulate a Form.ShowDialog call but WITHOUT blocking anything (kinda like a Simulated Dialog Form) you can try using Form.Show() and as soon as you show the simulated dialog form then immediately disable all other windows using something like...

    private void DisableAllWindows()
    {
    foreach (Form f in Application.OpenForms)
    if (f.Name != this.Name)f.Enabled = false;
    else f.Focus();
    }
    

    Then when you close your "pseudo-dialog form" be sure to call....

    private void EnableAllWindows()
    {
    foreach (Form f in Application.OpenForms) f.Enabled = true;
    }
    
    0 讨论(0)
  • 2020-12-13 00:02

    Are you calling ShowDialog() or just Show() on your child form from the parent form?

    ShowDialog will "block" the user from interacting with the form which is passed as a parameter to ShowDialog.

    Within the parent you might call something like:

    MyChildForm childForm = new MyChildForm();
    
    childForm.ShowDialog(this);
    

    where this is the parent form.

    0 讨论(0)
  • 2020-12-13 00:02

    What you could do, is to make sure to pass the parent form as the owner when showing the child form:

      Form newForm = new ChildForm();
      newForm.Show(this);
    

    Then, in the child form, set up event handlers for the Activated and Deactivate events:

    private void Form_Activated(object sender, System.EventArgs e)
    {
        if (this.Owner != null)
        {
            this.Owner.Enabled = false; 
        }
    }
    
    private void Form_Deactivate(object sender, System.EventArgs e)
    {
        if (this.Owner != null)
        {
            this.Owner.Enabled = true;
        }
    }
    

    However, this will result in a truly wierd behaviour; while you will not be able to go back and interact with the parent form immediately, activating any other application will enable it, and then the user can interact with it.

    If you want to make the child form modal, use ShowDialog instead:

      Form newForm = new ChildForm();
      newForm.ShowDialog(this);
    
    0 讨论(0)
提交回复
热议问题