Xin chào những bạn, trong bài xích học thiết kế c# hôm nay, tôi đang hướng dẫn chúng ta validation bằng phương pháp sử dụng Error Provider trong Dev
Express.

Bạn đang xem: Cách dùng errorprovider trong c#

Để tiến hành yêu ước này, các chúng ta có thể tham khảo giải pháp làm của mình dưới đây:

Các bạn hãy thiết kế form theo mẫu sau:

*

Gọi sự kiện “Validating”:

*

Sau kia viết code xử lý:

*

Nếu ko nhập thông tin vào ô thông tin tài khoản thì sẽ hiển thị thông báo lỗi

*

Khi singin thành công

*

Vậy là chúng ta đã xong bài học hôm nay. Chúc các bạn thành công.

Hẹn gặp gỡ lại chúng ta vào bài bác học lập trình c# lần sau.

Xin cảm ơn.

STANFORD – SỐ 1 DẠY gớm NGHIỆM LẬP TRÌNH

Giảng viên, chuyên viên là hầu hết người có không ít kinh nghiệm thực tiễn từ những dự án phần mềm lớn.Nội dung học unique & đi sát với thực tiễn. Tiền học phí nhiều chiết khấu hấp dẫn
Hỗ trợ trong quá trình học và reviews việc làm sau khoản thời gian học xong.

Xem thêm: Review Cách Dùng Bha Obagi Lần Đầu, Cách Sử Dụng Bha Obagi Cho Người Mới Bắt Đầu

HOTLINE: (024) 6275 2212 | (024) 6662 3355

Tầng 2 số 20 ngõ 678 Đường bóng (Hoặc cuối ngõ 100 Nguyễn Chí Thanh), Đống Đa, Hà Nội.
5 / 5 ( 10 votes )

You might also like...


Previous Post: học lập trình c# – Đọc file Excel
Next Post: dạy dỗ lập trình C – ngôn ngữ phổ cập hiện nay

Nhận xét Cancel reply


Name (required)
Email (required)
Website (optional)

Save my name, email, and website in this browser for the next time I comment.

Submit Comment

Δ


Bài viết mới

Nhận xét mới

Chủ đề

.Net
Java
Lập trình di động
PHPTổng hợp

Chương trình ưu đãi tại Stanford

*



Địa chỉ


Stanford - Dạy kinh nghiệm lập trình
Số 20 ngõ 678 Đường Láng, Phường trơn Thượng, thủ đô
*

Liên hệ


stanford.com.vn

Bài viết mới


Theo dõi tôi


Xin chào các bạn, nội dung bài viết hôm nay mình sẻ hướng dẫn các bạn cách thực hiện thư viện Fluent
Validation
 để kiểm tra dữ liệu Form nhập liệu trên Winform C#.

Using Fluent Validation Form

Bất kỳ, khi các bạn thiết kế khung nào để nhập liệu, các bạn đều đề nghị kiểm tra xem form đó người dùng nhập liệu vào gồm hợp liệu xuất xắc không.


*
*

Đầu tiện, chúng ta tải Fluent Validation từ Nuget:


using Fluent
Validation;using Fluent
Validation.Results;using System;using System.Collections.Generic;using System.Component
Model;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace Fluent
Validate
Demo public partial class Form1 : khung public Form1() Initialize
Component(); private void btn
Save_Click(object sender, Event
Args e) var student = new Student(); student.ID = txt
ID.Text; student.Name = txt
Name.Text; student.Email = txt
Email.Text; student.Address = txt
Address.Text; student.Phone = txt
Phone.Text; var validator = new Student
Validator(); Validation
Result results = validator.Validate(student); error
Provider.Clear(); if (!results.Is
Valid) foreach (var failure in results.Errors) switch (failure.Property
Name) case "ID": error
Provider.Set
Error(txt
ID, failure.Error
Message); txt
ID.Border
Color = Color.Red; break; case "Name": error
Provider.Set
Error(txt
Name, failure.Error
Message); txt
Name.Border
Color = Color.Red; break; case "Email": error
Provider.Set
Error(txt
Email, failure.Error
Message); txt
Email.Border
Color = Color.Red; break; case "Address": error
Provider.Set
Error(txt
Address, failure.Error
Message); txt
Address.Border
Color = Color.Red; break; case "Phone": error
Provider.Set
Error(txt
Phone, failure.Error
Message); txt
Phone.Border
Color = Color.Red; break; if(!results.Errors.To
List().Exists(x => x.Property
Name == "ID")) success
Provider.Set
Error(txt
ID, "Pass"); txt
ID.Border
Color = Color.Green; if (!results.Errors.To
List().Exists(x => x.Property
Name == "Name")) success
Provider.Set
Error(txt
Name, "Pass"); txt
Name.Border
Color = Color.Green; if (!results.Errors.To
List().Exists(x => x.Property
Name == "Email")) success
Provider.Set
Error(txt
Email, "Pass"); txt
Email.Border
Color = Color.Green; if (!results.Errors.To
List().Exists(x => x.Property
Name == "Address")) success
Provider.Set
Error(txt
Address, "Pass"); txt
Address.Border
Color = Color.Green; if (!results.Errors.To
List().Exists(x => x.Property
Name == "Phone")) success
Provider.Set
Error(txt
Phone, "Pass"); txt
Phone.Border
Color = Color.Green; else Message
Box.Show("Pass validate Form."); public class Student public string ID set; get; public string Name set; get; public string email set; get; public string Address set; get; public string Phone set; get; public class Student
Validator : Abstract
Validator
Student> public Student
Validator() Rule
For(x => x.ID) .Not
Empty().With
Message("Enter your ID."); Rule
For(x => x.Name) .Not
Empty().With
Message("Enter your Name."); Rule
For(x => x.Email) .Not
Empty().With
Message("Email address is required") .Email
Address().With
Message("Email is not valid"); Rule
For(x => x.Address).Length(10, 100).With
Message("Please specify a valid Address"); Rule
For(x => x.Phone) .Not
Empty().With
Message("Phone is required") .Length(10, 11).With
Message("Please specify a valid Phone");
Thanks for watching!