아래는 기본화면입니다.
이메일 형식으로 올바르게 입력했을때 전송버튼을 누르면
아래와 같은 메세지가 뜬다
실패할경우
아래와 같이 아이콘이 보이면서 자동으로 전송버튼 이벤트도 발생하지 않는다
에러검사
에러메세지 발생시키는 방법
this.errorProvider1.SetError(textBox1, errorMsg);
errorProvider를 끌어와서 추가한다
텍스트 박스에 아래와 같이 이벤트를 추가한다
아래의 코드를 참조해서 응용해서 코드를 처리
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace TestWindowsFormsApplication
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_Validating(object sender,
System.ComponentModel.CancelEventArgs e)
{
string errorMsg;
if (!ValidEmailAddress(textBox1.Text, out errorMsg))
{
// Cancel the event and select the text to be corrected by the user.
e.Cancel = true;
textBox1.Select(0, textBox1.Text.Length);
// Set the ErrorProvider error with the text to display.
this.errorProvider1.SetError(textBox1, errorMsg);
}
}
private void textBox1_Validated(object sender, System.EventArgs e)
{
// If all conditions have been met, clear the ErrorProvider of errors.
errorProvider1.SetError(textBox1, "");
}
public bool ValidEmailAddress(string emailAddress, out string errorMessage)
{
// Confirm that the e-mail address string is not empty.
if (emailAddress.Length == 0)
{
errorMessage = "e-mail address is required.";
return false;
}
// Confirm that there is an "@" and a "." in the e-mail address, and in the correct order.
if (emailAddress.IndexOf("@") > -1)
{
if (emailAddress.IndexOf(".", emailAddress.IndexOf("@")) > emailAddress.IndexOf("@"))
{
errorMessage = "";
return true;
}
}
errorMessage = "e-mail address must be valid e-mail address format.\n" +
"For example 'someone@example.com' ";
return false;
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("전송완료!");
}
}
}
에러발생을 하고 싶지 않다면?
좀 친절하게 메세지를 표시!
위의 간단한 설정으로 마우스가 올라가면 메세지가 표시된다
'C#.NET' 카테고리의 다른 글
| [MSDN] EventHandler (0) | 2010.03.31 |
|---|---|
| [C#]사용자 아이피 알아오기 (0) | 2010.03.17 |
| [MSDN][BindingList] 데이터 바인드 개선 (0) | 2010.03.12 |
| [Binding] 컨트롤 바인딩 할떄 포멧변경 (0) | 2010.03.11 |
| Partial 클래스 (0) | 2010.03.10 |
| openFileDialog로 텍스트파일 선택하기 (0) | 2010.03.06 |
| [MSDN]DataGridView 셀에서 컨트롤 호스팅 (0) | 2010.03.03 |
| [Interface]인터페이스가 구현되어져 있는지 확인해보기 (0) | 2010.03.03 |
| [ErrorProvider]폼 유효성에 대한 오류 아이콘 표시 (0) | 2010.03.03 |
| 초간단 숫자 앞에 0 채우기 (0) | 2010.02.25 |