我是 C# 的初学者,我自己编写了一个程序。它检查是否所有文本框都正确填充,然后在按下保存按钮时应该显示消息框,但如果所有文本框都不正确,则不应显示它
这是消息框代码:
if (MessageBox.Show("Data is being saved", "Data saving", MessageBoxButtons.OK) == DialogResult.OK)
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
}
这是完整的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _5_prakt
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Regex regex1 = new Regex("^[a-zA-Z ]+$");
Regex dat = new Regex("^(0[1-9]|[12][0-9]|3[01])(0[1-9]|1[012])([0-9]{2})[-]([0-9]{5})$");
Regex epasts = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
if (!regex1.IsMatch(textBox1.Text))
{
label5.ForeColor = Color.Red;
label5.Text = "Incorrectly entered name!";
}
else
{
label5.Text = "";
}
if (String.IsNullOrEmpty(textBox1.Text))
{
label5.ForeColor = Color.Red;
label5.Text = "Name wasn't entered!";
}
if (!regex1.IsMatch(textBox2.Text))
{
label6.ForeColor = Color.Red;
label6.Text = "Surname entered incorrectly!";
}
else
{
label6.Text = "";
}
当年话下
哈士奇WWW
相关分类