Link to home
Start Free TrialLog in
Avatar of orjanmj
orjanmj

asked on

c# textbox validation

Hi

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.

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();
            }
         
        }

Open in new window

Avatar of ripahoratiu
ripahoratiu
Flag of Romania image

implement the validating event of textboxes:

        private void textBoxSjekk_Validating(object 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
        }
    }
Avatar of orjanmj
orjanmj

ASKER

This does not work...

First of all I do not want to clear() the textbox if the user have put in text by accident. Then I just show the error-message and user changes data.

The validating process is ok and I see what you mean on the code, but I have tried this and it still does not work. I do not think previousState will work either.

I only use tablemanager.updateAll(); when I want to update database with form data. I think the problem will be solved if I manually try to put in data to database and can put in dbNull when the box is empty.

I think there is something wrong with the validation of a textbox that must have int, since you cannot pares "Empty" to int it will not validate. Even though it's bound to a row that can contain DBNull.

The validation happens before the update, so I just want to be able to have dbNull in the textbox.text, but that is not possible. I can have it to null, but not dbNull. And of course null value fails validation also.
ASKER CERTIFIED SOLUTION
Avatar of orjanmj
orjanmj

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial