MessageBoxResult
时间: 2023-08-25 18:09:05 浏览: 38
MessageBoxResult是一个枚举类型,用于表示用户在消息框中的选择结果。它包含以下几个可能的值:OK、Cancel、Yes、No、None等。\[1\]\[2\]\[3\]
#### 引用[.reference_title]
- *1* *2* *3* [C#日记:MessageBox, 简单粗暴地使用](https://blog.csdn.net/weixin_30764883/article/details/98357017)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
相关问题
MessageBoxResult result = MessageBox.Show("确认退出程序?", "提示", MessageBoxButton.YesNo, MessageBoxImage.Question); if (result == MessageBoxResult.Yes) { this.Close(); System.Environment.Exit(0); }请优化这段代码,让询问框右上角的关闭按钮隐藏
要隐藏询问框右上角的关闭按钮,可以在显示前设置对话框的 `WindowStyle` 属性为 `WindowStyle.ToolWindow`,并将 `ShowInTaskbar` 属性设置为 `false`,这样就能够达到隐藏关闭按钮的效果。
以下是优化后的代码:
```
MessageBoxResult result;
var messageBox = new MessageBoxWindow("确认退出程序?", "提示", MessageBoxButton.YesNo, MessageBoxImage.Question);
messageBox.WindowStyle = WindowStyle.ToolWindow;
messageBox.ShowInTaskbar = false;
result = messageBox.ShowDialog();
if (result == MessageBoxResult.Yes)
{
this.Close();
System.Environment.Exit(0);
}
```
这里使用了自定义的 `MessageBoxWindow` 类,用于创建一个自定义的消息框窗口。在这个类中,需要设置窗口的样式和属性。这样就能够在显示消息框时隐藏关闭按钮了。
using (FileStream fswrite = File.OpenWrite(path)) { wb.Write(fswrite); MessageBox.Show("导出完成!", "提示", MessageBoxButton.OK, MessageBoxImage.Asterisk); }wpf messagebox怎么2秒后自动关闭
你可以在`MessageBox.Show`之前添加`Task.Delay`来延迟2秒钟后自动关闭`MessageBox`。以下是示例代码:
```
using (FileStream fswrite = File.OpenWrite(path))
{
wb.Write(fswrite);
var messageBoxResult = MessageBox.Show("导出完成!", "提示", MessageBoxButton.OK, MessageBoxImage.Asterisk);
if (messageBoxResult == MessageBoxResult.OK)
{
await Task.Delay(2000);
var activeWindow = Application.Current.Windows.OfType<Window>().SingleOrDefault(x => x.IsActive);
activeWindow?.Close();
}
}
```
这段代码会在`MessageBox.Show`之后等待2秒钟,然后关闭处于活动状态的窗口。请注意,这里使用`Application.Current.Windows.OfType<Window>().SingleOrDefault(x => x.IsActive)`来获取当前活动的窗口,因为MessageBox可能会弹出一个新的窗口并将其设置为活动窗口。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![exe](https://img-home.csdnimg.cn/images/20210720083343.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)