다음을 통해 공유


방법: 메시지 상자 표시

MessageBox는 사용자에게 응용 프로그램 관련 정보를 표시하는 미리 정의된 대화 상자입니다. 또한 사용자에게 정보를 요청할 경우에도 메시지 상자가 사용됩니다.

메시지 상자에 정보를 표시하려면

  1. 메시지 상자를 표시하는 코드를 추가할 위치로 이동합니다.

  2. Show 메서드를 사용하여 코드를 추가합니다.

    다음 코드는 MessageBox 클래스의 Show 메서드를 호출하여 사용자에게 정보를 표시하는 방법을 보여 줍니다. Show 메서드를 호출하는 경우 선택적 매개 변수 style을 사용하여 표시되는 메시지 상자의 유형에 가장 적합한 아이콘 형식을 지정합니다.

    Public Sub PerformCalculations()
       ' Code is entered here that performs a calculation.
       ' Display a message box informing the user that the calculations 
       ' are complete.
          MessageBox.Show("The calculations are complete", "My Application", _
               MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk)
    End Sub
    
    public void PerformCalculations() 
    {
       // Code is entered here that performs a calculation
       // Display a message box informing the user that the calculations 
       // are complete
       MessageBox.Show ("The calculations are complete", "My Application", 
    MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
    }
    
    public:
       void PerformCalculations()
       {
          // Code is entered here that performs a calculation
          // Display a message box informing the user that the calculations 
          // are complete
          MessageBox::Show("The calculations are complete",
             "My Application", MessageBoxButtons::OKCancel,
             MessageBoxIcon::Asterisk);
       }
    

    메시지 상자에 내용을 입력할 수도 있습니다. MessageBox 클래스의 Show 메서드는 사용자의 선택을 확인하는 데 사용할 수 있는 값을 반환합니다. 이 값을 정수로 저장하거나 if 문을 사용하여 메시지 상자를 표시할 때 반환되는 값을 비교할 수 있습니다. 사용자에게 정보를 요청하는 단추를 표시하도록 Show 메서드의 style 매개 변수를 설정할 수 있습니다.

정보를 요청하는 메시지 상자를 표시하려면

  1. 클래스의 코드 편집기를 열고 메시지 상자를 표시하는 코드를 추가할 위치로 이동합니다.

  2. MessageBox 클래스의 Show 메서드를 사용하는 코드를 추가하여 메시지 상자를 표시합니다.

    다음 코드는 MessageBox 메서드를 호출하여 사용자가 입력한 정보를 검색한 다음 선택된 값을 확인하는 방법을 보여 줍니다.

    Public Sub ExitApplication()
       ' Display a message box asking users if they 
       ' want to exit the application.
       If MessageBox.Show ("Do you want to exit?", "My Application", _
             MessageBoxButtons.YesNo, MessageBoxIcon.Question) _
             = DialogResult.Yes Then
          Application.Exit
       End If
    End Sub
    
    public void ExitApplication()
    {
       // Display a message box asking users if they
       // want to exit the application.
       if (MessageBox.Show ("Do you want to exit?", "My Application",
             MessageBoxButtons.YesNo, MessageBoxIcon.Question)
             == DialogResult.Yes) 
       {
          Application.Exit();
       }
    }
    
    public:
       void ExitApplication()
       {
          // Display a message box asking users if they
          // want to exit the application.
          if (MessageBox::Show("Do you want to exit?",
             "My Application", MessageBoxButtons::YesNo,
             MessageBoxIcon::Question) == DialogResult::Yes)
          {
             Application::Exit();
          }
       }
    
    Visual Basic 정보Visual Basic 정보

    Visual Basic에서는 위의 예제와 같이 새로운 구문인 MessageBox.Show()를 사용하는 것이 좋지만 MsgBox()를 사용하여 표시할 메시지 상자를 만드는 방법도 지원됩니다. 따라서 위의 코드 예제의 경우 Visual Basic에서는 다음과 같이 작성할 수도 있습니다.

    Public Sub ExitApplication()
       If MsgBox("Do you want to exit?", MsgBoxStyle.Exclamation, _
    "My Application") = MsgBoxResult.Yes Then
         Application.Exit()
       End If
    End Sub
    

    MsgBox()에 대한 자세한 내용은 MsgBox 함수를 참조하십시오.

참고 항목

작업

방법: 디자인 타임에 대화 상자 만들기

참조

MessageBox

MsgBox Result Constants for Visual Basic 6.0 Users

MsgBox Style Constants for Visual Basic 6.0 Users

Form.DialogResult

기타 리소스

Windows Forms 대화 상자

새 Windows Form 만들기