아래는 기본화면입니다.

image

이메일 형식으로 올바르게 입력했을때 전송버튼을 누르면

image

아래와 같은 메세지가 뜬다

실패할경우

image

아래와 같이 아이콘이 보이면서 자동으로 전송버튼 이벤트도 발생하지 않는다

에러검사

에러메세지 발생시키는 방법

this.errorProvider1.SetError(textBox1, errorMsg);

image

errorProvider를 끌어와서 추가한다

 

텍스트 박스에  아래와 같이 이벤트를 추가한다

image

아래의 코드를 참조해서 응용해서 코드를 처리

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("전송완료!");
        }

    }
}

에러발생을 하고 싶지 않다면?

image

 

좀 친절하게 메세지를 표시!

image

위의 간단한 설정으로 마우스가 올라가면 메세지가 표시된다

image

+ Recent posts