Error provider in C#(Win forms)

91 views
Skip to first unread message

mathew

unread,
Jul 18, 2008, 1:38:12 PM7/18/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Hi all
I use an error provider in my win form in order to validate some text
boxes value.
My problem is this:if the user input something wrong then the error
provider shows him a
notification,It is logical,but when user correct the value the error
notification is still visible to him
and the user think that he made a mistake again.The same problem is
happened in DatagridView.
what should I do to prevent this?(the error provider disappeared when
user correct his value when he entered)
the following code is a piece of my code which I use:

private void txtName_Validating(object sender,
System.ComponentModel.CancelEventArgs e)
{
if (!IsNameValid())
{
e.Cancel = true;
}
}
private bool IsNameValid()
{
txtName.Text = txtName.Text.Trim();
if (txtName.Text == string.Empty)
{
erp1.SetError(txtName, "Name must not be blank.");
return false;
}
else
{
erp1.SetError(txtName, "");
return true;
}
}
---------------------DataGridView-----------------
void DataGridView1CellValidating(object sender,
DataGridViewCellValidatingEventArgs e)
{
if (dataGridView1.Columns[e.ColumnIndex].HeaderText=="Input")
{
char[] chArray=e.FormattedValue.ToString().ToCharArray();
bool flag=false;
foreach(char ch in chArray){
if(!Char.IsDigit(ch)){

flag=true;
e.Cancel=true;
break;
}
if(flag==true){
dataGridView1.Rows[e.RowIndex].ErrorText = "Invalid Product
Image";
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText =
"The file name must end with '.gif'.";
}
else
{
dataGridView1.Rows[e.RowIndex].ErrorText = "";

dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText
="";

}
}

}
}

Glenn

unread,
Jul 18, 2008, 3:25:19 PM7/18/08
to DotNetDe...@googlegroups.com
Are you sure it is getting into the Validating() method?  The code you wrote is correct, but the program just needs to get into your validation code to get it reset.  Usually this means that the user must attempt to set focus away from the control by attempting to go to another field or close the dialog.
 
...Glenn

 

Glenn

unread,
Jul 19, 2008, 8:27:08 AM7/19/08
to DotNetDe...@googlegroups.com
Please keep all replies on-list so that others can benefit.
 
The way the ErrorProvider mechanism works is to validate the controls when the user sets focus away from them. 
 
For instance, let's say you have a form in which you have the user enter a name and address.  You would enable validation on the fields that you would want to perform data validation on to insure that the user entered correct data (valid address, valid state, etc).  As the user entered the data and hit tab to go to the next field, that field will be validated.
 
So, the validation is not going to get called until the user goes to the next field.  Any error condition from a previous check will remain until the validation call is done again.
 
...Glenn

 
On 7/18/08, mathew <pay...@gmail.com> wrote:

Dear Glenn
Yeah it doesn't work.Another problem is that you said user must
attempt to set focus away from the contol.Do you know what should I do
to overcome these problems?
Reply all
Reply to author
Forward
0 new messages