2012-01-10 7 views
9

다음 두 가지의 차이점은 무엇입니까?MsgBox와 MessageBox.Show간에 차이가 있습니까?

msgbox() 
messagebox.show() 

일부 자습서 MSGBOX()를 사용하고, 일부는) (다른, messagebox.show을 사용 --- 내가 모두 편집 가능한 스타일을 가질 수 있음을 볼 수 있지만 궁금 해서요 : 왜 두 가지가있다?

이전 프로그래머 (이전 버전의 Visual Basic에서 배웠다)를 수용 할 수 있습니까?

그래서 Visual Basic 2010 (Visual Studio 2010)에서 사용해야합니까?

답변

10

MsgBox()Messagebox.Show()과 동일합니다.

익숙한 VB6 프로그래머를 위해 존재합니다.

사용할 규칙이 없지만 MsgBoxMessageBox에 위임하기 만하면 개인적으로 MessageBox으로 직접 이동합니다.

+0

+1. 기존 VB6 코드와의 하위 호환 *을 위해 주로 사용되지만 * C# 프로그래머는 자세한 정보를 제공합니다. :) – MarkJ

+0

비 GUI 라이브러리에서 MessageBox를 호출 할 수 없다는 것을 알았습니다. System.Windows.Forms를 라이브러리에서 사용하려면이 라이브러리를 참조하거나 가져와야하지만 라이브러리를 사용하는 이유는이 라이브러리를 사용하는 이유가됩니다. MsgBox는 부모 GUI 응용 프로그램에 정보를 전달하기 위해 잘 작동하므로 적어도 한 가지 차이점이 있습니다. –

4

다음은 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와 동일하다고 말하면서 사용할 수 있습니까? –

+1

물론 둘 중 하나를 사용할 수 있습니다. 그러나 전문성 개발을 위해서는 MsgBox를 사용하는 것이 바람직하지 않습니다. –

+1

또한 MsgBox는 던진 모든 것을 캐스팅하려고 시도합니다 (가져온 값이 Object이므로 캐스팅 할 때 오류가 발생하면 런타임에 예외가 발생합니다. msdn에서는 문자열을 입력해야 함) Messagebox.Show는 String 값만 받아들이는 "strict"입니다. MsgBox는 Messagebox.Show를 호출 할 것이므로 왜 "느린 경로"를 사용해야합니까? – SomeNickName

2

메시지 상자 MessageBox.Show() 의해 생성 된 메시지 박스 창에있는 제목이없는 반면, MsgBox()가 만든 형태의 타이틀을 보유하여 만들었다.

+2

제목이 있습니다 :'MessageBox.Show (message, title, button, icon)' – habakuk

2

this site에 따르면 지금까지 내 자신의 질문에 대한 답변 (remark 참고)과 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 
+0

Ok, 나는 새로운 질문을 http://stackoverflow.com/questions/23350074/how-do-you-display- a-help-file-using-the-msgbox-not-messagebox-function-if-vb – Jim

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

^이것은 물음표 아이콘 만 확인 버튼이있는 상자가 표시됩니다


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

enter image description here

^이것은 예, 아니오로 상자를 표시하고 버튼과 물음표 아이콘을 취소합니다


. MessageBox.Show를 사용하여

당신이 볼 수 있듯이


, 당신은 어떤 아이콘으로 원하는 버튼을 할 수 있습니다.

+0

방금 ​​스크린 샷을 추가하기 위해 편집했습니다. 이게 도움이 되길 바란다. – RHDxSPAWNx

+2

또한 MsgBox로 할 수 있습니다 :'MsgBox ("변경 사항을 저장 하시겠습니까?", MsgBoxStyle.Question 또는 MsgBoxStyle.YesNoCancel, "Save Changes")' – habakuk

1

그러나 MsgBox에 대한 정말 좋은 점은 SystemModal 일 수 있다는 것입니다. MsgBox ("새 메시지가 있습니다!"& Environment.NewLine & "지금 읽으시겠습니까?", MsgBoxStyle.Information + MsgBoxStyle.YesNo + MsgBoxStyle.SystemModal, "Quick Message") = MsgBoxResult.Yes 그리고 ...

나는 MessageBox.Show (...이 SystemModal 될 경우 을 만드는 간단한 방법을 찾을 수 없습니다.

내 메시지는 현재 화면에 전체 명성을 얻을. 지루해.