본문 바로가기
카테고리 없음

C# Winform(윈폼) InputBox / MessageBox

by AJAX하고싶다 2023. 3. 22.
반응형

C# Winform(윈폼) 환경에서 인풋박스[InputBox], 메시지박스[MessageBox]의 세팅 방법을 알아봅니다. InputBox의 경우 VisualBasic을 참조하여 사용하는 경우, 클래스를 생성하여 Form형태를 로딩하는 경우를 알아봅니다. MessageBox의 경우 기본타입, 타이틀, 선택버튼, 메시지박스 아이콘 등의 설정을 알아봅니다.

 

# InputBox

1. VB(VisualBasic) InputBox를 참조하여 로딩하는 방법으로 [프로젝트] > [참조추가] > [어셈플리] > "VisualBasic" 검색 및 참조추가를 진행한다.

 

VS 참조추가
C# VS참조추가
C# VS 참조추가
C# Microsoft VisualBasic 추가

 

 

    string t메시지 = "메시지"; string t타이틀 = "타이틀";
    string t입력값 = Microsoft.VisualBasic.Interaction.InputBox(t메시지, t타이틀, "TEXT를 입력하세요.");

    if (t입력값 != "")
    {
        Console.WriteLine(t입력값);
    }

VisualBasic InputBox
C# VisualBasic InputBox

 

 

 

 

2. InputBox 클래스를 생성하여 입력받은 값을 확인한다. [ C_TEST.cs ] 생성.

using System;
using System.Drawing;
using System.Windows.Forms;
public class tClass
{
    public DialogResult inputBox(string t타이틀, string t메시지, ref string t입력값)
    {
        Form form = new Form();
        Label label = new Label();
        TextBox textBox = new TextBox();
        Button buttonOk = new Button();
        Button buttonCancel = new Button();

        form.Text = t타이틀;
        label.Text = t메시지;
        textBox.Text = t입력값;

        buttonOk.Text = "OK";
        buttonCancel.Text = "Cancel";
        buttonOk.DialogResult = DialogResult.OK;
        buttonCancel.DialogResult = DialogResult.Cancel;

        label.SetBounds(9, 20, 372, 13);
        textBox.SetBounds(12, 36, 372, 20);
        buttonOk.SetBounds(228, 72, 75, 23);
        buttonCancel.SetBounds(309, 72, 75, 23);

        label.AutoSize = true;
        textBox.Anchor = textBox.Anchor | AnchorStyles.Right;
        buttonOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
        buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;

        form.ClientSize = new Size(396, 107);
        form.Controls.AddRange(new Control[] { label, textBox, buttonOk, buttonCancel });
        form.ClientSize = new Size(Math.Max(300, label.Right + 10), form.ClientSize.Height);
        form.FormBorderStyle = FormBorderStyle.FixedDialog;
        form.StartPosition = FormStartPosition.CenterScreen;
        form.MinimizeBox = false;
        form.MaximizeBox = false;
        form.AcceptButton = buttonOk;
        form.CancelButton = buttonCancel;

        DialogResult dialogResult = form.ShowDialog();
        t입력값 = textBox.Text;
        return dialogResult;
    }

}

 

생성된  [ C_TEST.cs ] 클래스를 선언하여 InputBox 호출한다.

    tClass _tClass = new tClass();

    string t메시지 = "메시지"; string t타이틀 = "타이틀";
    string t입력값 = "TEXT를 입력하세요.";
    if (_tClass.inputBox(t메시지, t타이틀, ref t입력값) == DialogResult.OK)
    {
        Console.WriteLine("OK");
        Console.WriteLine(t입력값);        //OK 선택시 입력값 받음.
    }
    else
    {
        Console.WriteLine("Cancle");
    }

 

 

 

 

 

# MessageBox

1. 일반 메시지 박스 사용. 

    string t메시지 = "메시지";
    MessageBox.Show(t메시지);

C# 메시지박스
C# 메시지박스

 

 

2. 메시지박스 타이틀 사용.

    string t메시지 = "메시지";
    string t타이틀 = "타이틀";
    MessageBox.Show(t메시지, t타이틀);

C# 메시지박스 타이틀
C# 메시지박스 타이틀

 

 

3. 메시지박스의 선택 버튼 구성. 선택 버튼의 종류는 다음과 같다.

-OK
-OKCancel
-YesNo
-YesNoCancel
-AbortRetryIgnore
-RetryCancel

    string t메시지 = "메시지";
    string t타이틀 = "타이틀";
    MessageBoxButtons t버튼 = MessageBoxButtons.RetryCancel;
    DialogResult t응답 = MessageBox.Show(t메시지, t타이틀, t버튼);
    if (t응답 == DialogResult.OK)
    {
        Console.WriteLine("OK누름");
    }
    else
    {
        Console.WriteLine("Cancel누름");
    }

C# 버튼 OK
C# 버튼 OK
C# 버튼 OKCancel
C# 버튼 OKCancel
C# 버튼 YesNo
C# 버튼 YesNo
C# 버튼 YesNoCancel
C# 버튼 YesNoCancel
C# 버튼 AbortRetryIgnore
C# 버튼 AbortRetryIgnore
C# 버튼 RetryCancel
C# 버튼 RetryCancel

 

 

 

 

4.메시지박스 아이콘 이미지 사용. 선택 이미지의 종류는 다음과 같다.

- Error / Hand / Stop      이미지 동일
- Information / Asterisk   이미지 동일
- Warning / Exclamation 이미지 동일
- Question
- None

    string t메시지 = "메시지입니다.";
    string t타이틀 = "타이틀";
    DialogResult t응답 = MessageBox.Show(t메시지, t타이틀, MessageBoxButtons.OK, MessageBoxIcon.None);

C# 이미지 Error
C# 이미지 Error
C# 이미지 Information
C# 이미지 Information
C# 이미지 Warning
C# 이미지 Warning
C# 이미지 Question
C# 이미지 Question
C# 이미지 None
C# 이미지 None

 

 

5. 메시지박스 버튼 기본선택 위치 설정.

- MessageBoxDefaultButton.Button1 > 기본선택 왼쪽
- MessageBoxDefaultButton.Button2 > 기본선택 가운데
- MessageBoxDefaultButton.Button3 > 기본선택 오른쪽

    string t메시지 = "메시지입니다.";
    string t타이틀 = "타이틀";
    MessageBoxButtons t버튼 = MessageBoxButtons.AbortRetryIgnore;
    MessageBoxIcon t아이콘 = MessageBoxIcon.Warning;
    DialogResult t응답 = MessageBox.Show(t메시지, t타이틀, t버튼, t아이콘, MessageBoxDefaultButton.Button3);
    if (t응답 == DialogResult.Abort)
    {
        Console.WriteLine("중지");
    }
    else if (t응답 == DialogResult.Retry)
    {
        Console.WriteLine("재시도");
    }
    else
    {
        Console.WriteLine("무시");
    }

C# 버튼 기본위치 왼쪽
C# 버튼 기본위치 왼쪽
C# 버튼 기본위치 가운데
C# 버튼 기본위치 가운데
C# 버튼 기본위치 오른쪽
C# 버튼 기본위치 오른쪽

 

 

6. 메시지박스 도움말 버튼 생성. 도움말 버튼을 클릭하면 NAVER 사이트가 로딩되도록 설정.

    string t메시지 = "메시지입니다.";
    string t타이틀 = "타이틀";
    MessageBoxButtons t버튼 = MessageBoxButtons.YesNo;
    MessageBoxIcon t아이콘 = MessageBoxIcon.Question;
    MessageBoxDefaultButton t버튼시작 = MessageBoxDefaultButton.Button3;

    string t도움말 = "https://www.naver.com/";

    DialogResult t응답 = MessageBox.Show(t메시지, t타이틀, t버튼, t아이콘, t버튼시작, 0, t도움말);

    if (t응답 == DialogResult.Abort)
    {
        Console.WriteLine("중지");
    }
    else if (t응답 == DialogResult.Retry)
    {
        Console.WriteLine("재시도");
    }
    else
    {
        Console.WriteLine("무시");
    }

C# 도움말 버튼
C# 도움말 버튼

 

 

 

이상입니다. C# Winfrom 환경에서 InputBox, MessageBox 세팅을 알아보았습니다.

반응형

댓글