2012-01-10 335 views
9

以下两者有区别吗?MsgBox和MessageBox.Show有区别吗?

msgbox() 
messagebox.show() 

一些教程采用的msgbox(),以及一些使用其他messagebox.show()---我看到两个能有一个可编辑的风格,但我想知道:为什么有两个?

它是否适应老程序员(谁已经学习了一个老版本的Visual Basic)?

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

回答

10

MsgBox()Messagebox.Show()相同。

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

有没有规则使用哪一个,但由于MsgBox只是结束委托给MessageBox,我个人会直接与MessageBox

+0

+1。尽管它主要存在*用于向后兼容*与现有的VB6工作代码。另外 - sheesh!C#程序员是冗长的:) – MarkJ 2012-11-01 12:15:40

+0

我发现我无法从非GUI库调用MessageBox。似乎我需要引用/导入System.Windows.Forms以在库中使用它,但这会破坏我使用库的原因。 MsgBox工作正常(将信息传递给父GUI应用程序),所以至少有一个区别。 – 2015-08-14 17:30:50

4

以下是Msgbox的源代码。正如你所看到的,在调用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 
+0

所以你试图说MsgBox和messagebox是一样的,可以使用吗? – 2012-01-10 08:10:26

+1

当然也可以使用。但对于职业发展来说,使用MsgBox被认为是不好的形式。 – 2012-01-10 08:11:32

+1

此外,MsgBox会尝试投射你所投的所有东西(因为所采用的值是Object,并且如果在投射时发生任何错误,它将在运行时抛出一个异常,尽管msdn指出你必须为它提供一个字符串..)而Messagebox.Show更“严格”,只接受String值。由于MsgBox会调用Messagebox.Show,为什么要采取“慢路径?” – SomeNickName 2014-04-29 23:27:24

2

的消息框。

+2

它有一个标题:'MessageBox.Show(message,title,button,icon)' – habakuk 2014-10-06 12:28:28

2

根据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 
+0

好吧,我确实在http://stackoverflow.com/questions/23350074/how-do-you-display- a-help-file-using-the-msgbox-not-messagebox-function-if-vb – Jim 2014-04-28 20:31:56

3

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

例如:

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

enter image description here

^这将显示用是,否一个盒子和取消按钮没有图标。



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

enter image description here

^这将显示一个问号图标,但只有一个OK按钮,一个盒子。



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

enter image description here

^这将显示一个方块,是,否和取消按钮和问号图标。



正如你可以看到,使用MessageBox.Show使你有你想要的任何图标任何按钮。

+0

我刚编辑添加屏幕截图。我希望这有帮助。 – RHDxSPAWNx 2014-06-13 18:47:22

+2

也可以用MsgBox完成:'MsgBox(“你想保存更改吗?”,MsgBoxStyle.Question或MsgBoxStyle.YesNoCancel,“保存更改”)' – habakuk 2014-10-06 12:26:47

1

但MsgBox的真正好处在于它可以是SystemModal,例如如果MsgBox(“有一个新的快速消息!”& Environment.NewLine &“你想现在阅读吗?”,MsgBoxStyle.Information + MsgBoxStyle.YesNo + MsgBoxStyle.SystemModal,“快速消息”)= MsgBoxResult.Yes然后...

我找不到让如果MessageBox.Show(...是系统模式的简单方式。

我的邮件现在在屏幕上得到充分的重视。yippee的。