web-dev-qa-db-ja.com

MsgBoxとMessageBox.Showに違いはありますか?

次の2つに違いはありますか?

 msgbox()
 messagebox.show()

いくつかのチュートリアルはmsgbox()を使用し、いくつかは他のmessagebox.show()を使用します---両方とも編集可能なスタイルを持つことができるようですが、私は疑問に思っていました:なぜ2つあるのですか?

古いプログラマー(古いバージョンのVisual Basicで学習した)に対応するためですか?

その場合、Visual Basic 2010でどちらを使用する必要がありますか( Visual Studio 201 )?

11
Legendary Lambe

MsgBox()Messagebox.Show()と同じです。

これは、それに慣れているVB6プログラマーのために存在します。

どちらを使用するかについてのルールはありませんが、MsgBoxは単にMessageBoxに委任することになりますので、私は個人的にMessageBoxを直接使用します。

10
Oded

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
6
Jonathan Allen

アイコンと異なるボタンを混在させようとすると、違いがあります。 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を使用すると、任意のアイコンで任意のボタンを使用できます。

5
RHDxSPAWNx

このサイト と私自身の質問に対するこれまでの回答(備考を参照)、およびmsgbox関数を使用して特定のヘルプファイルを表示できないことによると、私はメッセージボックスではなくメッセージボックスを使用すると言わなければなりませんヘルプを表示したい場合は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
2
Jim

しかし、MsgBoxの本当に素晴らしい点は、SystemModalにすることができることです。 If MsgBox( "新しいクイックメッセージがあります!"&Environment.NewLine& "今すぐ読みますか?"、MsgBoxStyle.Information + MsgBoxStyle.YesNo + MsgBoxStyle.SystemModal、 "クイックメッセージ" MsgBoxResult.Yes Then ...)==

MessageBox.Show(...をSystemModalにする簡単な方法が見つかりませんでした。

私のメッセージが画面上で完全に目立つようになりました。イッピー。

2
Brian Jones

MsgBox()を使用して作成されたメッセージボックスには、それを作成したフォームのタイトルがありますが、MessageBox.Show()によって作成されたメッセージボックスウィンドウにはタイトルがありません。

2
user1790926