You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WPF(VB.NET)项目MessageBoxButton枚举参数异常求助

解决WPF中MessageBox.Show触发InvalidEnumArgumentException的问题

你的代码触发错误的核心原因是MessageBox.Show的参数顺序错误,不符合WPF System.Windows.MessageBox.Show的重载定义。

WPF的MessageBox.Show没有(string 消息, MessageBoxButton 按钮, MessageBoxImage 图标)这样的重载。你当前的调用会被编译器错误匹配到(string 消息, string 标题, MessageBoxButton 按钮)这个重载:

  • 第二个参数本应是字符串类型的对话框标题,但你传入了MessageBoxButton.OK枚举,VB.NET会隐式将其转换为字符串;
  • 第三个参数MessageBoxImage.Information会被当作MessageBoxButton类型传入,它的枚举值为64,而MessageBoxButton的有效值范围不包含64,因此触发InvalidEnumArgumentException。

正确的调用方式

根据你的需求,有几种正确的写法:

  1. 仅显示消息和指定按钮(无标题、无图标)
    直接使用(string 消息, MessageBoxButton 按钮)的重载:
If OwordDoc IsNot Nothing Then
    System.Windows.MessageBox.Show("A document is already open.", MessageBoxButton.OK)
    Return
End If
  1. 显示消息、按钮和图标(带空标题)
    如果需要显示图标,必须传入标题参数(可以是空字符串),使用(string 消息, string 标题, MessageBoxButton 按钮, MessageBoxImage 图标)的重载:
If OwordDoc IsNot Nothing Then
    System.Windows.MessageBox.Show("A document is already open.", "", MessageBoxButton.OK, MessageBoxImage.Information)
    Return
End If
  1. 显示完整的消息、标题、按钮和图标
    添加自定义标题,让对话框更规范:
If OwordDoc IsNot Nothing Then
    System.Windows.MessageBox.Show("A document is already open.", "提示", MessageBoxButton.OK, MessageBoxImage.Information)
    Return
End If

内容的提问来源于stack exchange,提问作者barryleajo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 09:07:26