Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net MsgBox和MessageBox.Show之间有区别吗?_Vb.net_Visual Studio 2010 - Fatal编程技术网

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

Vb.net MsgBox和MessageBox.Show之间有区别吗?,vb.net,visual-studio-2010,Vb.net,Visual Studio 2010,以下两者之间有区别吗 msgbox() messagebox.show() 有些教程使用msgbox(),有些使用另一个,messagebox.show()---我看到两者都可以有一个可编辑的样式,但我想知道:为什么有两个 它是否适合较老的程序员(他们已经学习过较旧版本的Visual Basic) 那么在这种情况下,我应该在Visual Basic 2010()中使用哪一个呢?MsgBox()与Messagebox.Show()相同 它适用于习惯于它的VB6程序员 没有关于使用哪一个的规则

以下两者之间有区别吗

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

它是否适合较老的程序员(他们已经学习过较旧版本的Visual Basic)

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

MsgBox()
Messagebox.Show()相同

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


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

以下是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
_
作为MsgBoxResult的公共共享函数MsgBox(ByVal提示为对象,ByVal可选按钮为MsgBoxStyle=0,ByVal可选标题为Object=new Object())
将所有者变暗为IWin32Window=无
将文本变暗为字符串=无
将暗淡的标题卷组装为字符串
Dim vBHost As IVbHost=HostServices.vBHost
如果(不是vBHost是Nothing),则
所有者=vBHost.GetParentWindow
如果结束
如果(((按钮和15)>MsgBoxStyle.RetryCancel)或lse((按钮和240)>MsgBoxStyle.Information))或lse((按钮和&HF00)>MsgBoxStyle.DefaultButton3)),则
按钮=MsgBoxStyle.ok仅限
如果结束
尝试
如果(不提示即为无),则
[text]=CStr(Conversions.ChangeType(提示符,GetType(字符串)))
如果结束
捕获异常为StackOverflowException
抛出异常
捕获异常2作为OutOfMemoryException
抛出例外2
捕获异常3作为ThreadAbortException
抛出例外3
捕获异常9作为异常
抛出新ArgumentException(Utils.GetResourceString(“参数_InvalidValueType2”,新字符串(){“提示符”,“字符串”}))
结束尝试
尝试
如果(标题什么都不是)那么
如果(vBHost为Nothing),则
titleFromAssembly=Interaction.GetTitleFromAssembly(Assembly.GetCallingAssembly)
其他的
titleFromAssembly=vBHost.GetWindowTitle
如果结束
其他的
titleFromAssembly=转换.ToString(标题)
如果结束
捕获异常4作为StackOverflowException
抛出例外4
捕获异常5作为OutOfMemoryException
抛出例外5
捕获异常6作为ThreadAbortException
抛出例外6
捕获异常13作为异常
抛出新ArgumentException(Utils.GetResourceString(“参数_InvalidValueType2”,新字符串(){“标题”,“字符串”}))
结束尝试
返回DirectCast(MessageBox.Show(所有者,[text],标题框组合,(DirectCast(按钮,MessageBoxButtons)和DirectCast(15,MessageBoxButtons)),(DirectCast(按钮,MessageBoxIcon)和DirectCast(按钮,MessageBoxDefaultButton)),(DirectCast(按钮,MessageBoxDefaultButton)和DirectCast(&HF00,MessageBoxDefaultButton)),(DirectCast(按钮,MessageBoxOptions)和DirectCast(-4096,MessageBoxOptions)),MsgBoxResult)
端函数

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

根据和我自己问题的答案(见备注),以及我无法使用msgbox函数显示特定的帮助文件,如果您想显示帮助,我必须说使用messagebox而不是msgbox。msgbox函数显示一个帮助按钮,但显然无法将帮助文件放入其中!我在下面展示了我使用的代码,在t上还有一个很好的代码示例他是第一个环节

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

尝试将图标与不同按钮混合使用时会出现差异。MsgBox具有预定义的样式(可能有创建新样式的方法)

例如:

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

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



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



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




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

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

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


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

那么你是想说MsgBox和messagebox一样,可以使用其中任何一个?当然,任何一个都可以使用。但是对于专业发展来说,使用MsgBox被认为是不好的形式。而且,MsgBox会尝试使用你扔给它的任何东西(因为获取的值是Object,如果在强制转换时发生任何错误,它将在运行时引发异常,尽管msdn声明必须为其提供字符串…)而Messagebox.Show更“严格”,只接受字符串值。既然MsgBox将调用Messagebox.Show,那么为什么要选择“慢路由”?“+1.虽然它的存在主要是为了与现有的VB6代码向后兼容。而且-sheesh!C#程序员很冗长:)我发现我无法从非GUI库调用MessageBox。似乎我需要引用/导入System.Windows.Forms才能在库中使用它,但这会破坏我使用库的原因。MsgBox工作正常(将信息传递到父GUI应用程序),所以至少有一个区别。好的,我确实问了一个新问题,我刚刚编辑到
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)