从Public Sub(模块)获取DialogResult

时间:2014-03-04 07:45:32

标签: .net vb.net public byval

我不知道要搜索的关键字。

这是我应用中的模块。

Public Sub msgYNC(ByVal result As DialogResult)
    result = MessageBox.Show("Are you sure you want to save and continue?", "RM Farms Confirmation", _
                 MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)
End Sub

现在我想在表单上使用它。所以当我点击确认

Dim result As DialogResult
    msgYNC(result)
    If result = Windows.Forms.DialogResult.Yes Then
        'yes
    ElseIf result = Windows.Forms.DialogResult.No Then
        'n
    ElseIf result = Windows.Forms.DialogResult.Cancel Then
        'ca
    End If

但它没有给我任何回报......我没有得到回复。我不知道如何编码,你能指出什么是错的吗?

1 个答案:

答案 0 :(得分:0)

解决方案应该非常简单。不要在您的Sub中使用byval,请使用byref

Public Sub msgYNC(ByRef result As DialogResult)
    result = MessageBox.Show("Are you sure you want to save and continue?", 
                             "RM Farms Confirmation", _
                             MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)
End Sub
你能试试吗?

尽管您可以使用以下结构(如果您只想在sub-method msgYNC中显示消息框)

    Select Case MessageBox.Show("", "", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)
        Case Windows.Forms.DialogResult.Yes
            'Do something
        Case Windows.Forms.DialogResult.No
            'Do something else
        Case Windows.Forms.DialogResult.Cancel
            'Cancel something
        Case Else
            'Do something unusefull
    End Select