重载解析失败,因为在没有缩小转换的情况下无法调用可访问的 'Show'

Overload resolution failed because no accessible 'Show' can be called without a narrowing conversion

我一直遇到有关此缩小转换错误的问题

Overload resolution failed because no accessible 'Show' can be called without a narrowing conversion:

'Public Shared Function Show(owner As System.Windows.Forms.IWin32Window, text As String, caption As String, buttons As System.Windows.Forms.MessageBoxButtons) As System.Windows.Forms.DialogResult': Argument matching parameter 'owner' narrows from 'String' to 'System.Windows.Forms.IWin32Window'.

'Public Shared Function Show(owner As System.Windows.Forms.IWin32Window, text As String, caption As String, buttons As System.Windows.Forms.MessageBoxButtons) As System.Windows.Forms.DialogResult': Argument matching parameter 'caption' narrows from 'Microsoft.VisualBasic.MsgBoxStyle' to 'String'.

'Public Shared Function Show(owner As System.Windows.Forms.IWin32Window, text As String, caption As String, buttons As System.Windows.Forms.MessageBoxButtons) As System.Windows.Forms.DialogResult': Argument matching parameter 'buttons' narrows from 'System.Windows.Forms.MessageBoxIcon' to 'System.Windows.Forms.MessageBoxButtons'.

'Public Shared Function Show(text As String, caption As String, buttons As System.Windows.Forms.MessageBoxButtons, icon As System.Windows.Forms.MessageBoxIcon) As System.Windows.Forms.DialogResult': Argument matching parameter 'buttons' narrows from 'Microsoft.VisualBasic.MsgBoxStyle' to 'System.Windows.Forms.MessageBoxButtons'.

我做了一些研究,"Overload resolution failed because no accessible '' can be called without a narrowing conversion: " 错误的通用解决方案是根据 Microsoft 指定 Option Strict Off。我尝试在项目属性中手动更改它,但它似乎没有用。

这是发生错误的代码:

If MessageBox.Show("Please Enter a value for ESD (rad)", "ESD (rad) Value", MsgBoxStyle.OkCancel, MessageBoxIcon.Information) = DialogResult.OK Then
            txtCal_USE_Radio.Focus()

我还查看了其他几个论坛,他们在其中讨论了这个错误,但具体与 'New' 函数相关,但他们似乎没有帮助。

任何帮助都将非常有用!

您正在调用 Show({string}, {MsgBoxStyle}, {MessageBoxIcon}),因此错误消息中的最后一个重载最接近:

'Public Shared Function Show(text As String, caption As String, buttons As System.Windows.Forms.MessageBoxButtons, icon As System.Windows.Forms.MessageBoxIcon) As System.Windows.Forms.DialogResult': Argument matching parameter 'buttons' narrows from 'Microsoft.VisualBasic.MsgBoxStyle' to 'System.Windows.Forms.MessageBoxButtons'.

那是 Show({String}, {String}, {MessageBoxButtons}, {MessageBoxIcon}) - 您缺少 caption 参数,而不是 MsgBoxStyle 您应该使用 MessageBoxButtons 枚举。

听起来你有 Option Strict On - 那是 很好 优秀 - 但你似乎也有 Imports Microsoft.VisualBasic,这实际上污染了你的 IntelliSense 与 VB6 向后兼容的东西,MsgBoxStyle 是其中的一部分;该枚举意味着使用遗留 MsgBox 函数,MessageBox 是更符合 .NET 习惯的替代品。

关闭 Option Strict 将是最糟糕的事情 - 你传递了一个错误的参数并且编译器告诉你 "I can't convert the supplied type to the expected one";最后要做的是让它说 "hey don't worry, just implicitly convert all the things and blow up at run-time instead".

IntelliSense/autocomplete 应该告诉你要做什么当你在函数调用中输入参数时;重新键入左括号 ( 并观察 IntelliSense 突出显示参数及其各自的类型,因为您使用箭头键将插入符号移动到您提供的参数上。

您正在将 MessageBox 与 MsgBox 混合使用 将 MsgBoxStyle.OkCancel 更改为 MessageBox 语法。

If MessageBox.Show("Please Enter a value for ESD (rad)", "ESD (rad) Value", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) = DialogResult.OK Then