Friday, February 6, 2009

c# textbox validation

Question:

I have a form that contain several textboxes that are bound to a database.row that has int32 as datatype and allows dbNull value. When a user starts up the form, the code inserts three values (this is because the user needs to know these values before filling in the rest), and fills the tableadapter with data so many textboxes is bound to dbNull value.

The form has many textboxes bound to a SQL-database and the form functions very well if you put correct data into the form. I also have checks on the textboxes that must have int as datatyp that the text is possible to parse as an int (so all checks is ok).

My problem is as follows:
1. If a user "tabs" his way throug the form and accidentally puts a character into a textbox that must have an int they get a warning saying the textbox can only contain integers.
2. If they delete the character the textbox.validate() locks the control and I cannot get the form to do anything until the user puts in an int into the box. If the user puts in an int the validatoin works and form is unlocked again. I want it also to validate=true when textbox is empty, but I cannot seem to get this to work.
3. I want to set the textbox back to its previous state whenever the textbox.text.length == 0, because then it is the same as when you open the form and this validates fine (because it contains no data).
4. Attached code that I have to check the textboxes, but the only thing that happens is textboxSjekk.clear() and then the form locks itself because an empty textbox does not validate to int (but an empty textbox on startup does).

Hope this was understandable.

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
private void feilMldTall(TextBox textBoxSjekk, string feilKontroll)
{
if (textBoxSjekk.Text.Length > 0)
{
try
{ Int32.Parse(textBoxSjekk.Text); }
catch (Exception)
{
MessageBox.Show("Can only contain int");

}
}
else
{
textBoxSjekk
.Clear();
}

}
ANSWER:
implement the validating event of textboxes:

private void textBoxSjekk_Validating(ob
ject sender, CancelEventArgs e)
{
if (textBoxSjekk.Text.Length > 0)
{
try
{ Int32.Parse(textBoxSjekk.Text); }
catch (Exception)
{
MessageBox.Show("Can only contain int");
textBoxSjekk.Clear();
textBoxSjekk.Focus();
}
}
}

If you want to restore to the previous valid state of the textbox:

public partial class Form1 : Form
{
string _previousValidTextBoxSjekk;
public Form1()
{
InitializeComponent();
textBoxSjekk.Text = "2";
_previousValidTextBoxSjekk = textBoxSjekk.Text; //initialize
}

private void textBoxSjekk_Validating(object sender, CancelEventArgs e)
{
if (textBoxSjekk.Text.Length > 0)
{
int i;
if(!Int32.TryParse(textBoxSjekk.Text,out i))
{

MessageBox.Show("Can only contain int");
textBoxSjekk.Text = _previousValidTextBoxSjekk;
textBoxSjekk.Focus();
return;
}
_previousValidTextBoxSjekk = textBoxSjekk.Text; // here if you only want to restore to a populated state of the textbox once it was populated at some point in time
}
// _previousValidTextBoxSjekk = textBoxSjekk.Text ; //here if you want to add also empty as a previous valid state
}
}

No comments:

Post a Comment