如果所有文本框都成功完成,我如何显示此消息框

时间:2018-12-13 01:20:40

标签: c#

我是C#的初学者,我为自己编写了一个程序。它检查所有文本框是否正确填充,然后在按下保存按钮时显示消息框,但如果所有文本框都不正确,则不显示它

My windows form and how i want it to be.

这是消息框代码:

    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 = "";
            }

            if (String.IsNullOrEmpty(textBox2.Text))
            {
                label6.ForeColor = Color.Red;
                label6.Text = "No surname!";
            }

            if (!dat.IsMatch(textBox3.Text))
            {
                label7.ForeColor = Color.Red;
                label7.Text = "Incorrect code!";
            }
            else
            {
                label7.Text = "";
            }

            if (String.IsNullOrEmpty(textBox3.Text))
            {
                label7.ForeColor = Color.Red;
                label7.Text = "Not entered!";
            }

            if (!epasts.IsMatch(textBox4.Text))
            {
                label8.ForeColor = Color.Red;
                label8.Text = "Incorrectly entered email!";
            }
            else
            {
                label8.Text = "";
            }

            if (String.IsNullOrEmpty(textBox4.Text))
            {
                label8.ForeColor = Color.Red;
                label8.Text = "Email not entered!";
            }

            if (MessageBox.Show("Data is being saved", "Data saving", MessageBoxButtons.OK) == DialogResult.OK)
            {
                textBox1.Text = "";
                textBox2.Text = "";
                textBox3.Text = "";
                textBox4.Text = "";
            }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

在方法开始时输入以下内容:

bool valid = true;

在每个发现有输入错误的if上输入以下内容:

valid = false;

然后在方法末尾添加以下内容:

if (valid && MessageBox.Show("Data is being saved", "Data saving", MessageBoxButtons.OK) == DialogResult.OK)
{
    textBox1.Text = "";
    textBox2.Text = "";
    textBox3.Text = "";
    textBox4.Text = "";
}

答案 1 :(得分:0)

尚不清楚您要做什么,但如果我理解正确的话,您想跟踪是否有错误,并且只有在没有错误的情况下才保存?您可以使用一个布尔值来做到这一点,该布尔值将指示是否有任何错误。另外-您的MessageBox仅具有“确定”按钮-因此,如果使用if语句则没有意义。您是说取消吗?

请在下面的修改后的代码中查看注释:

i = ignore upper/lowercase
s = search multiple lines