【问题标题】:Is there a difference between MsgBox and MessageBox.Show?MsgBox 和 MessageBox.Show 有区别吗?
【发布时间】:2012-01-10 07:38:52
【问题描述】:

以下两者有区别吗?

 msgbox()
 messagebox.show()

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

它是为了适应年长的程序员(他们在旧版本的 Visual Basic 上学习过的)吗?

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

【问题讨论】:

    标签: vb.net visual-studio-2010


    【解决方案1】:

    MsgBox()Messagebox.Show() 相同。

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

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

    【讨论】:

    • +1。虽然它的存在主要是为了向后兼容与现有的工作 VB6 代码。还有——嘘! C# 程序员很冗长 :)
    • 我发现我无法从非 GUI 库中调用 MessageBox。看来我需要引用/导入 System.Windows.Forms 才能在库中使用它,但这会破坏我使用库的原因。 MsgBox 工作正常(将信息传递给父 GUI 应用程序),所以至少有一个区别。
    【解决方案2】:

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

    【讨论】:

    • 所以你是想说MsgBox和messagebox一样,都可以用?
    • 当然可以。但是对于专业发展来说,使用 MsgBox 被认为是一种糟糕的形式。
    • 另外,MsgBox 会尝试转换你扔给它的所有东西(因为获取的值是 Object,如果在转换时发生任何错误,它会在运行时抛出异常,尽管 msdn 声明你必须提供一个字符串..) 而 Messagebox.Show 更“严格”,只接受字符串值。既然 MsgBox 无论如何都会调用 Messagebox.Show,为什么要走“慢路线”?
    【解决方案3】:

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

    例如:

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

    ^ 这将显示一个框,其中包含是、否和取消按钮,没有图标。



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

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



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

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



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

    【讨论】:

    • 我刚刚编辑添加截图。我希望这会有所帮助。
    • 也可以用MsgBox做:MsgBox("Do you wish to save changes?", MsgBoxStyle.Question OR MsgBoxStyle.YesNoCancel, "Save Changes")
    【解决方案4】:

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

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

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

    【讨论】:

      【解决方案5】:

      根据this site 和到目前为止我自己的问题的答案(见备注),以及我无法使用 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\mui\0409\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
      

      【讨论】:

      【解决方案6】:

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

      【讨论】:

      • 它有一个标题:MessageBox.Show(message, title, button, icon)
      猜你喜欢
      • 2010-09-13
      • 1970-01-01
      • 2022-02-04
      • 2012-02-16
      • 2010-09-26
      • 2011-01-10
      • 2017-06-07
      相关资源
      最近更新 更多