Program Club

자식 양식이 활성 상태 일 때 부모 양식을 비활성화하는 방법은 무엇입니까?

proclub 2020. 10. 30. 21:16
반응형

자식 양식이 활성 상태 일 때 부모 양식을 비활성화하는 방법은 무엇입니까?


C #을 사용하여 자식 양식이 활성 상태 일 때 부모 양식을 비활성화하는 방법은 무엇입니까?


사용 해봤 Form.ShowDialog () 대신 Form.Show ()을 ?

ShowDialog는 창을 모달로 표시하므로 닫힐 때까지 상위 양식과 상호 작용할 수 없습니다.


부모 양식에서 전화 ShowDialog()하거나 Show()자녀 양식에 있습니까?

ShowDialog에 매개 변수로 전달 된 양식과 사용자가 상호 작용하는 것을 "차단"합니다 ShowDialog.

부모 내에서 다음과 같이 부를 수 있습니다.

MyChildForm childForm = new MyChildForm();

childForm.ShowDialog(this);

this부모 양식은 어디에 있습니까 ?


간단하고 사용

  Form.ShowDialog();

대신

  Form.Show();

사용 중에는 Form.ShowDialog()닫을 때까지 상위 양식과 상호 작용할 수 없습니다.


수있는 일은 하위 양식을 표시 할 때 소유자로 상위 양식을 전달하는 것입니다.

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

그런 다음 자식 양식에서 ActivatedDeactivate이벤트에 대한 이벤트 처리기를 설정 합니다.

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;
    }
}

그러나 이것은 정말로 이상한 행동을 초래할 것입니다. 돌아가서 부모 양식과 즉시 상호 작용할 수는 없지만 다른 응용 프로그램을 활성화하면 활성화되고 사용자가 상호 작용할 수 있습니다.

당신이 자식 폼의 확인하려면 모달을 사용 ShowDialog하는 대신 :

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

앞서 언급 한 childForm.ShowDialog (this)를 사용하면 기본 양식이 비활성화되지만 여전히 매우 비활성화 된 것처럼 보입니다. 그러나 ShowDialog () 전에 Enabled = false를 호출하고 ShowDialog ()를 호출 한 후 Enable = true를 호출하면 기본 폼이 비활성화 된 것처럼 보일 수도 있습니다.

var childForm = new Form();
Enabled = false;
childForm .ShowDialog(this);
Enabled = true;

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


Form1 frmnew = new Form1();
frmnew.ShowDialog();

@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


You can do that with the following:

Form3 formshow = new Form3();

formshow.ShowDialog();

You can also use MDIParent-child form. Set the child form's parent as MDI Parent

Eg

child.MdiParent = parentForm;
child.Show();

In this case just 1 form will be shown and the child forms will come inside the parent. Hope this helps


For me this work for example here what happen is the main menu will be disabled when you open the registration form.

 frmUserRegistration frmMainMenu = new frmUserRegistration();
    frmMainMenu.ShowDialog(this);

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;
}

Why not just have the parent wait for the child to close. This is more than you need.

// Execute child process
System.Diagnostics.Process proc = 
    System.Diagnostics.Process.Start("notepad.exe");
proc.WaitForExit();

참고URL : https://stackoverflow.com/questions/1130208/how-to-disable-the-parent-form-when-a-child-form-is-active

반응형