Form1.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace WindowsFormsErrorProvider
  11. {
  12. public partial class Form1 : Form
  13. {
  14. public Form1()
  15. {
  16. InitializeComponent();
  17. }
  18. private int a, b, c;
  19. private void textBox1_Validating(object sender, System.ComponentModel.CancelEventArgs e)
  20. {
  21. if (textBox1.Text == "")
  22. {
  23. errorProvider1.SetError(textBox1, "不能为空");
  24. }
  25. else
  26. {
  27. errorProvider1.SetError(textBox1, "");
  28. a = 1;
  29. }
  30. }
  31. private void textBox2_Validating(object sender, CancelEventArgs e)
  32. {
  33. if (textBox2.Text == "")
  34. {
  35. errorProvider2.SetError(textBox2, "不能为空");
  36. }
  37. else
  38. {
  39. try
  40. {
  41. int x = Int32.Parse(textBox2.Text);
  42. errorProvider2.SetError(textBox2, "");
  43. b = 1;
  44. }
  45. catch
  46. {
  47. errorProvider2.SetError(textBox2, "请输入一个数");
  48. }
  49. }
  50. }
  51. private void textBox3_Validating(object sender, CancelEventArgs e)
  52. {
  53. if (textBox3.Text == "")
  54. {
  55. errorProvider3.SetError(textBox3, "不能为空");
  56. }
  57. else
  58. {
  59. errorProvider3.SetError(textBox3, "");
  60. c = 1;
  61. }
  62. }
  63. private void button1_Click(object sender, EventArgs e)
  64. {
  65. if (a + b + c == 3)
  66. {
  67. MessageBox.Show("数据录入成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  68. }
  69. }
  70. private void button2_Click(object sender, EventArgs e)
  71. {
  72. textBox1.Text = "";
  73. textBox2.Text = "";
  74. textBox3.Text = "";
  75. }
  76. }
  77. }