MsgBox和MessageBox.Show之间有区别吗?

Leg*_*mbe 9 vb.net visual-studio-2010

以下两个之间有区别吗?

 msgbox()
 messagebox.show()
Run Code Online (Sandbox Code Playgroud)

一些教程使用msgbox(),有些使用另一个,messagebox.show()---我看到两者都可以有一个可编辑的样式,但我想知道:为什么有两个?

是否适应年龄较大的程序员(在旧版本的Visual Basic上学习过)?

那么在这种情况下,我应该在Visual Basic 2010(Visual Studio 2010)中使用哪一个?

Ode*_*ded 10

MsgBox()是一样的Messagebox.Show().

它适用于习惯它的VB6程序员.

没有关于使用哪一个的规则,但由于MsgBox只是最终委托MessageBox,我个人会直接使用MessageBox.

  • +1。尽管它的存在主要是为了与现有的 VB6 代码向后兼容。还有——谢啦!C# 程序员很啰嗦:) (2认同)

Jon*_*len 6

这是Msgbox的源代码。如您所见,在调用MessageBox.Show之前,它并没有做任何特别有趣的事情。

<MethodImpl(MethodImplOptions.NoInlining), HostProtection(SecurityAction.LinkDemand, Resources:=HostProtectionResource.UI)> _
Public Shared Function MsgBox(ByVal Prompt As Object, ByVal Optional Buttons As MsgBoxStyle = 0, ByVal Optional Title As Object = new Object()) As MsgBoxResult
    Dim owner As IWin32Window = Nothing
    Dim text As String = Nothing
    Dim titleFromAssembly As String
    Dim vBHost As IVbHost = HostServices.VBHost
    If (Not vBHost Is Nothing) Then
        owner = vBHost.GetParentWindow
    End If
    If ((((Buttons And 15) > MsgBoxStyle.RetryCancel) OrElse ((Buttons And 240) > MsgBoxStyle.Information)) OrElse ((Buttons And &HF00) > MsgBoxStyle.DefaultButton3)) Then
        Buttons = MsgBoxStyle.OkOnly
    End If
    Try 
        If (Not Prompt Is Nothing) Then
            [text] = CStr(Conversions.ChangeType(Prompt, GetType(String)))
        End If
    Catch exception As StackOverflowException
        Throw exception
    Catch exception2 As OutOfMemoryException
        Throw exception2
    Catch exception3 As ThreadAbortException
        Throw exception3
    Catch exception9 As Exception
        Throw New ArgumentException(Utils.GetResourceString("Argument_InvalidValueType2", New String() { "Prompt", "String" }))
    End Try
    Try 
        If (Title Is Nothing) Then
            If (vBHost Is Nothing) Then
                titleFromAssembly = Interaction.GetTitleFromAssembly(Assembly.GetCallingAssembly)
            Else
                titleFromAssembly = vBHost.GetWindowTitle
            End If
        Else
            titleFromAssembly = Conversions.ToString(Title)
        End If
    Catch exception4 As StackOverflowException
        Throw exception4
    Catch exception5 As OutOfMemoryException
        Throw exception5
    Catch exception6 As ThreadAbortException
        Throw exception6
    Catch exception13 As Exception
        Throw New ArgumentException(Utils.GetResourceString("Argument_InvalidValueType2", New String() { "Title", "String" }))
    End Try
    Return DirectCast(MessageBox.Show(owner, [text], titleFromAssembly, (DirectCast(Buttons, MessageBoxButtons) And DirectCast(15, MessageBoxButtons)), (DirectCast(Buttons, MessageBoxIcon) And DirectCast(240, MessageBoxIcon)), (DirectCast(Buttons, MessageBoxDefaultButton) And DirectCast(&HF00, MessageBoxDefaultButton)), (DirectCast(Buttons, MessageBoxOptions) And DirectCast(-4096, MessageBoxOptions))), MsgBoxResult)
End Function
Run Code Online (Sandbox Code Playgroud)

  • 另外,MsgBox会尝试对您抛出的所有内容进行强制转换(由于采用的值为Object,并且如果在强制转换时发生任何错误,则运行时将抛出异常,尽管msdn声明必须向其提供字符串。) Messagebox.Show更加“严格”,仅接受String值。由于MsgBox仍会调用Messagebox.Show,因此为什么要使用“慢路由”? (2认同)

RHD*_*WNx 6

当您尝试将图标与不同按钮混合使用时会有所不同。MsgBox 具有预定义的样式(可能有一种方法可以创建新样式)。

例如:

MsgBox("Do you wish to save changes?", MsgBoxStyle.YesNoCancel, "Save Changes")
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

^ 这将显示一个带有是、否和取消按钮的框,没有图标。



MsgBox("Do you wish to save changes?", MsgBoxStyle.Question, "Save Changes")
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

^ 这将显示一个带有问号图标的框,但只有一个确定按钮。



MessageBox.Show("Do you wish to save changes?", "Save Changes", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

^ 这将显示一个带有是、否和取消按钮以及一个问号图标的框。



如您所见,使用 MessageBox.Show 可以让您拥有带有任何图标的任何按钮。

  • 也可以用 MsgBox 来完成:`MsgBox("Do you want to save changes?", MsgBoxStyle.Question OR MsgBoxStyle.YesNoCancel, "Save Changes")` (3认同)