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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8799976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:52:08  来源:igfitidea点击:

Is there a difference between MsgBox and MessageBox.Show?

vb.netvisual-studio-2010

提问by Legendary Lambe

Is there a difference between the following two?

以下两者有区别吗?

 msgbox()
 messagebox.show()

Some tutorials use msgbox(), and some use the other, messagebox.show()---I see that both can have an editable style, but I was wondering: Why are there two?

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

Is it to accommodate older programmers (who have learnt on an older version of Visual Basic)?

是为了适应较老的程序员(他们已经学习了较旧版本的 Visual Basic)吗?

So in that case, which one should I use in Visual Basic 2010 (Visual Studio 2010)?

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

采纳答案by Oded

MsgBox()is the same as Messagebox.Show().

MsgBox()与 相同Messagebox.Show()

It exists for VB6 programmers who are used to it.

它是为习惯它的 VB6 程序员而存在的。

There are no rules on which one to use, but since MsgBoxsimply ends up delegating to MessageBox, I personally would go directly with MessageBox.

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

回答by Jonathan Allen

Here is the source code for Msgbox. As you can see it doesn't do anything particularly interesting before calling MessageBox.Show.

这是 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

回答by RHDxSPAWNx

There is a difference when you are attempting to mix icons with different buttons. MsgBox has predefined styles (there may be a way to create new styles).

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

For example:

例如:

MsgBox("Do you wish to save changes?", MsgBoxStyle.YesNoCancel, "Save Changes")

enter image description here

在此处输入图片说明

^ This will display a box with Yes, No and Cancel buttons without an icon.

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





MsgBox("Do you wish to save changes?", MsgBoxStyle.Question, "Save Changes")

enter image description here

在此处输入图片说明

^ This will display a box with a Question mark icon but with ONLY an OK button.

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





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

enter image description here

在此处输入图片说明

^ This will display a box with Yes, No and Cancel buttons AND a Question mark icon.

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





As you can see, using MessageBox.Show enables you to have any buttons you want with any icon.

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

回答by Brian Jones

But the really nice thing about MsgBox is that it can be SystemModal e.g. If MsgBox("There is a new Quick Message!" & Environment.NewLine & "Do you want to read it now?", MsgBoxStyle.Information + MsgBoxStyle.YesNo + MsgBoxStyle.SystemModal, "Quick Message") = MsgBoxResult.Yes Then...

但是 MsgBox 真正的好处是它可以是 SystemModal 例如If MsgBox("有一个新的快速消息!" & Environment.NewLine & "你想现在阅读吗?", MsgBoxStyle.Information + MsgBoxStyle.YesNo + MsgBoxStyle.SystemModal, "Quick Message") = MsgBoxResult.Yes 那么...

I couldn't find a simple way of making If MessageBox.Show(...to be SystemModal.

我找不到使If MessageBox.Show(...成为 SystemModal的简单方法。

My messages now get full prominence on screen. Yippee.

我的消息现在在屏幕上得到充分的突出显示。伊皮。

回答by user1790926

The message box created using MsgBox()has a title of the form which created it, whereas the message box window created by MessageBox.Show()does not have any title.

使用创建的消息框MsgBox()具有创建它的表单的标题,而创建的消息框窗口MessageBox.Show()没有任何标题。

回答by Jim

According to this siteand the answers so far to my own question (see remark), as well my inability to display a specific help file using the msgbox function, I'd have to say use messagebox rather than msgbox if you want to show help. The msgbox function displays a help button, but apparently there is no way to put a helpfile in it! I'm showing the code I played around with below, and there is also a good code sample on the first link.

根据这个站点和到目前为止对我自己的问题的答案(见备注),以及我无法使用 msgbox 函数显示特定的帮助文件,如果你想显示帮助,我不得不说使用 messagebox 而不是 msgbox . msgbox 函数显示了一个帮助按钮,但显然没有办法在其中放置帮助文件!我在下面展示了我玩过的代码,第一个链接上还有一个很好的代码示例。

Imports Microsoft.visualbasic 'have to have this namespace to use msgbox
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim Helpfilepath As String = "C:\Windows\Help\mui09\aclui.chm"
    Dim msgresult As Byte
    'BTW, Must use 0 for BLANK PARAMETER. Using messageboxoptions.defaultdesktoponly errors out with help btn.
    msgresult = MessageBox.Show("Text", "Messagebox", 0, _
            0, 0, 0, Helpfilepath)

    'displays help button, but how do you display the help file?
    msgresult = MsgBox("Text", MsgBoxStyle.MsgBoxHelp, "msgbox")
    'BTW, must use dialogresult rather than messageboxresult with windows forms
    If msgresult = DialogResult.Yes Then
        'etc
    End If
End Sub
End Class