C# Winform(윈폼) 환경에서 인풋박스[InputBox], 메시지박스[MessageBox]의 세팅 방법을 알아봅니다. InputBox의 경우 VisualBasic을 참조하여 사용하는 경우, 클래스를 생성하여 Form형태를 로딩하는 경우를 알아봅니다. MessageBox의 경우 기본타입, 타이틀, 선택버튼, 메시지박스 아이콘 등의 설정을 알아봅니다.
# InputBox
1. VB(VisualBasic) InputBox를 참조하여 로딩하는 방법으로 [프로젝트] > [참조추가] > [어셈플리] > "VisualBasic" 검색 및 참조추가를 진행한다.
string t메시지 = "메시지"; string t타이틀 = "타이틀";
string t입력값 = Microsoft.VisualBasic.Interaction.InputBox(t메시지, t타이틀, "TEXT를 입력하세요.");
if (t입력값 != "")
{
Console.WriteLine(t입력값);
}
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메시지);
2. 메시지박스 타이틀 사용.
string t메시지 = "메시지";
string t타이틀 = "타이틀";
MessageBox.Show(t메시지, t타이틀);
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누름");
}
4.메시지박스 아이콘 이미지 사용. 선택 이미지의 종류는 다음과 같다.
- Error / Hand / Stop 이미지 동일
- Information / Asterisk 이미지 동일
- Warning / Exclamation 이미지 동일
- Question
- None
string t메시지 = "메시지입니다.";
string t타이틀 = "타이틀";
DialogResult t응답 = MessageBox.Show(t메시지, t타이틀, MessageBoxButtons.OK, MessageBoxIcon.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("무시");
}
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# Winfrom 환경에서 InputBox, MessageBox 세팅을 알아보았습니다.
댓글