Msgbox 是没有错误

Msgbox YesNo error

我收到此错误消息:表达式是一个值,因此不能作为赋值的目标

对于此代码

MsgBox("Do you really wish to delete " & txtLayerDelete.Text & "?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes

txtLayerDelete 在 UserControl 上。

您已尝试分配 MsgBox 但未使用该方法的 return 值。

MsgBox("Do you really wish to delete " & txtLayerDelete.Text & "?", MsgBoxStyle.YesNo)

将向您的用户显示消息框,此方法 return 是您应该使用的 MsgBoxResult 对象,如下所示。

Dim delete =  MsgBox("Do you really wish to delete " & txtLayerDelete.Text & "?", MsgBoxStyle.YesNo)

If delete = MsgBoxResult.Yes Then
  'Your logic
End If 

或者您可以只添加一个 If 语句并执行:

If MsgBox("Do you really wish to delete?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then

End If

作为补充说明 MsgBox function appears to be outdated, you should try and use MessageBox.Show

Dim delete = MessageBox.Show("Should this item be deleted", "Form Title", MessageBoxButtons.YesNo)

If delete = DialogResult.Yes Then

End If

这样试试。请注意 MsgBox 和 MessageBox return 结果是一个枚举值。 MsgBox 已替换为 MessageBox

    Dim result As DialogResult = MessageBox.Show("Do you really wish to delete " & txtLayerDelete.Text & "?", , MessageBoxButtons.YesNo)
    If result = Windows.Forms.DialogResult.Yes Then

    End If

你没有使用方法 return 值,我的朋友。

应该是:

`Dim dialogDel = MsgBox("Do you really wish to delete " & txtLayerDelete.Text & "?", MsgBoxStyle.YesNo)`

If dialogDel = DialogResult.Yes Then
 'Yes code.
Else
 'No code
End If

不客气:)

好的,既然每个人都已经向您展示了如何使用 IF Statement 完成任务,我将向您展示我将如何做。
我个人更喜欢使用 CASE statement 因为我觉得它看起来更干净了。

Select Case MsgBox("Do you really want to delete " & txtLayerDelete.Text & "?", MsgBoxStyle.YesNo)
        Case MsgBoxResult.Yes
            ''DELETES
        Case MsgBoxResult.No
            ''NOTHING
    End Select

如果您对我提供给您的代码有任何疑问,请告诉我:)