How do i make validation.

To check if the eg textbox is type with certain character like we want this text box to have at least 7 character.
and then when we click button OK[save button as usual] it will not save and it validates at the side with * at least 7 characters before they can run the program in save button? without using message box. is this possible? using labels?

please let me know if you do not understand my question. i needa think of a way to phrase.

Use an error provider!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace daniweb.validation
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
      errorProvider1.SetError((sender as Control), string.Empty);
    }

    private void button1_Click(object sender, EventArgs e)
    {
      //Clear and re-check all controls

      errorProvider1.Clear();
      bool hasErrors = false;

      if (string.IsNullOrEmpty(textBox1.Text) || (textBox1.Text.Length < 7))
      {
        errorProvider1.SetError(textBox1, "You must enter at least 7 characters");
        hasErrors = true;
      }

      if (!hasErrors)
      {
        //Save your data
      }
    }
  }
}

Edited 7 Years Ago by sknake: n/a

Use an error provider!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace daniweb.validation
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
      errorProvider1.SetError((sender as Control), string.Empty);
    }

    private void button1_Click(object sender, EventArgs e)
    {
      //Clear and re-check all controls

      errorProvider1.Clear();
      bool hasErrors = false;

      if (string.IsNullOrEmpty(textBox1.Text) || (textBox1.Text.Length < 7))
      {
        errorProvider1.SetError(textBox1, "You must enter at least 7 characters");
        hasErrors = true;
      }

      if (!hasErrors)
      {
        //Save your data
      }
    }
  }
}

How about if there is multiple validation for like textboxes.

Then use the same code; just add in more textboxes.

if (string.IsNullOrEmpty(textBox2.Text) || (textBox2.Text.Length < 7))
{
errorProvider2.SetError(textBox2, "You must enter at least 7 characters");
hasErrors = true;
}

etc...

Then use the same code; just add in more textboxes.

if (string.IsNullOrEmpty(textBox2.Text) || (textBox2.Text.Length < 7))
{
errorProvider2.SetError(textBox2, "You must enter at least 7 characters");
hasErrors = true;
}

etc...

thanks actually i want to use just one error provider. But anyway I have solve that problem=)

This question has already been answered. Start a new discussion instead.