C#/WindowForm

C# 윈도우 폼 메시지 박스

hyun0229 2022. 3. 30. 01:57

MessageBox.Show를 이용해 메시지 박스 만들기

 public Form1()
        {
            InitializeComponent();
            MessageBox.Show("메시지의 내용을 담는 곳.", "메시지 박스의 타이틀", MessageBoxButtons.YesNo/*메시지박의 버튼수*/,
            MessageBoxIcon.Exclamation/*메시지박의 형식*/);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            DialogResult result1 = MessageBox.Show("두개의 버튼을 갖는 메시지박스입니다.","Question",
                MessageBoxButtons.YesNo); //result1에 댛화상자의 반환값을 저장

            DialogResult result2 = MessageBox.Show( "세개의 버튼과 물음표 아이콘을 보여주는 메시지박스입니다.","Question",
                MessageBoxButtons.YesNoCancel,MessageBoxIcon.Question); //result2에 대화상자의 반환값을 저장
            DialogResult result3 = MessageBox.Show("디폴트 버튼을 두 번째 버튼으로 \n지정한 메시지박스입니다.", "Question",
                MessageBoxButtons.YesNoCancel,MessageBoxIcon.Question,MessageBoxDefaultButton.Button2);
            //MessageBoxDefaultButton.Button2는 메시지 버튼의 기본값을 2번째 버튼으로 함
            string msg = string.Format("당신의 선택 : {0} {1} {2}",result1.ToString(),result2.ToString(),result3.ToString());
            //result1~3의 반환값을 문자열로 변경하여 저장
            MessageBox.Show(msg, "Your Selections"); 


        }

MessageBox.Show를 이용해 메시지 박스는 (내용,제목,버튼,형식) 순으로 담겨있고 제목, 버튼, 형식을 설정하지 않아도 디폴트값이 있기 때문에 실행된다. 

아래는 차례로 실행 결과이다.

실행화면
실행화면
MessageBox_1
MessageBox_2
MessageBox_3
결과