当我的 WinForms 中留下错误时,如何显示消息框“数据无效”。尝试过类似的东西,但它不起作用。
if (errorprovider1 == !null)
{
MessageBox.Show("Data is invalid");
}
也许我必须使用 bool 来解决这个问题。
我的完整代码:
private void Form1_Load(object sender, EventArgs e)
{
this.Text = "Formas elementu validācija";
}
private void textBox1_Validating(object sender, CancelEventArgs e)
{
Regex regex1 = new Regex(@"^[a-zA-Z]+$");
if (!regex1.IsMatch(textBox1.Text))
{
errorProvider1.SetError(textBox1, "Nosaukums nedrīskt saturēt ciparus!");
}
else
{
errorProvider1.Clear();
}
}
private void textBox2_Validating(object sender, CancelEventArgs e)
{
Regex regex1 = new Regex(@"^[0-9]+$");
if (!regex1.IsMatch(textBox2.Text))
{
errorProvider2.SetError(textBox2, "Reģ.nur drīkst saturēt TIKAI ciparus!");
}
else
{
errorProvider2.Clear();
}
}
private void button1_Click(object sender, EventArgs e)
{
// if errorProvider1 is empty (no errors) , show messagebox with text: All data correct.
// else Data is incorrect.
}