gpt4 book ai didi

c# - 当 ChildWindow 打开 MessageBox 并关闭时,MainWindow 最小化

转载 作者:太空宇宙 更新时间:2023-11-03 15:05:26 24 4
gpt4 key购买 nike

该程序有一个主窗口,带有用于打开子窗口的按钮。当其中一个 child 显示消息框时,当我关闭 child 时,主窗口最小化。

protected void EventBtn_Click(object sender, RoutedEventArgs e)
{
Child child = new child();
child.Show();
child.Owner = this;
}

解决方案:

感谢@Oscar Martinez

child :

public partial class Child: Window
{
public Child()
{
InitializeComponent();
}

private void button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Hello world!");
}

// this is what you need to add
protected override void OnClosing(CancelEventArgs e)
{
this.Owner = null;
}
}

家长:

public partial class Parent: Window
{
public Parent()
{
InitializeComponent();
}

private void button_Click(object sender, RoutedEventArgs e)
{
Child child = new Child();
child.Show();
child.Owner = this;
}
}

最佳答案

我有同样的行为。通过处理 Child 表单的 Closing 事件很容易解决这个问题。

child :

public partial class Child: Window
{
public Child()
{
InitializeComponent();
}
private void button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Hello world!");
}

// this is what you need to add
private void Child_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
this.Owner = null;
}
}

家长:

 public partial class Parent: Window
{
public Parent()
{
InitializeComponent();
}

private void button_Click(object sender, RoutedEventArgs e)
{
Child child = new Child();
child.Show();
child.Owner = this;
}
}

如果您最小化 Parent,则 Child 也会最小化。

如果关闭子项,父项不会最小化。

关于c# - 当 ChildWindow 打开 MessageBox 并关闭时,MainWindow 最小化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43778678/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com