TextBox validation for currency (adding" .00 " at the end )

Adding ".00" automatically at last 
        This code will validates currency value, means it will add " .00 " value automatically at the end of the value entred by the user. Validation for textbox will be validate in two ways, one is validate on user interface. This validation will be done at the user interface only,there is no need of database connection. And another validation type is validate with the database connection. Checking duplication is one of the such validation type. For the when user clicks the save  button, the data will be from database and shows a error message to the use.This code will validate on user interface only.


For this you must restrict the lettres and have to allow only numbers because currency value will be in numbers.

Event :- This code should write in textBox_Leave event.

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 WindowsFormsApplication20
{
    public partial class Form1 : Form
    {
     public Form1()
    {
     InitializeComponent();
     }

       private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsLetter(e.KeyChar))
            {
                e.Handled = true;
            }
        }

         private void textBox1_Leave(object sender, EventArgs e)
         {
            double x;
            double.TryParse(textBox1.Text, out x);
            textBox1.Text = x.ToString(".00");
         }
    }
}

I hope this code will usefull to you.

No comments:

Post a Comment