How to validate all textbox controls of window form application

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. namespace ManagementApplication  
  10. {  
  11. public partial class UserRegistration : Form  
  12. {  
  13. // public String characters = "abcdeCDEfghijkzMABFHIJKLNOlmnopqrPQRSTstuvwxyUVWXYZ";  
  14. public UserRegistration()  
  15. {  
  16. InitializeComponent();  
  17. }  
  18. private void UserRegistration_Load(object sender, EventArgs e)  
  19. {  
  20. WindowState = FormWindowState.Maximized;  
  21. dateTimePicker1.Format = DateTimePickerFormat.Short;  
  22. }  
  23. private void button1_Click(object sender, EventArgs e)  
  24. {  
  25. TextBox[] text = new TextBox[5];  
  26. text[0] = textBox1;  
  27. text[1] = textBox2;  
  28. text[2] = textBox3;  
  29. text[3] = textBox4;  
  30. text[4] = textBox5;  
  31. foreach ( TextBox txt in text )  
  32. {  
  33. if (txt.Text == "")  
  34. {  
  35. MessageBox.Show("Field Can't be Blank");  
  36. break;  
  37. }  
  38. }  
  39. if (textBox1.Text != "" && textBox2.Text != "" && textBox3.Text != "" && textBox4.Text != "" && textBox5.Text != "")  
  40. {  
  41. for (int i = 0; i <= 4; i = i + 1)  
  42. {  
  43. text[i].Text = "";  
  44. }  
  45. MessageBox.Show("Your data has been added your data successfully");  
  46. }  
  47. }  
  48. }  
  49. }