Here is what you need (I hope):
------------------------- CurrencyTextBoxValidator.cs
using System;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using System.Windows.Forms;
namespace YourApp
{
/// <summary>
/// Summary description for CurrencyTextBoxValidator.
/// </summary>
public class CurrencyTextBoxValidator : System.Windows.Forms.TextBox
{
char keyCharValue;
public CurrencyTextBoxValidator()
{
InitializeComponent();
}
#region Initialize
private void InitializeComponent()
{
this.KeyPress += new
System.Windows.Forms.KeyPressEventHandler(this.Cur rencyTextBox_KeyPress);
this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.TextChanged += new EventHandler(this.CurrencyTextBox_TextChanged);
}
#endregion
#region public methods
/// <summary>
/// Get the Currency Value (without space) of the TextBox
/// </summary>
/// <returns> -1 when an error occurs</returns>
public int GetValueOfCurrency()
{
try
{
if (this.Text != "")
return (Convert.ToInt32(this.Text.Replace(" ", "")));
else
return -1;
}
catch(Exception)
{
return -1;
}
}
/// <summary>
/// Take the Currency Value (without space) and put it in the TextBox
/// </summary>
public void PutCurrencyIn(string currencyValue)
{
this.Text = currencyValue;
AddSpaceInNumber();
}
#endregion
#region private methods
private void AddSpaceInNumber()
{
for (int posOfSpaceInText = this.Text.Length; posOfSpaceInText > 3;
posOfSpaceInText -= 3)
this.Text = this.Text.Insert(posOfSpaceInText - 3, " ");
}
#endregion
#region mask handlers
private void CurrencyTextBox_KeyPress(object sender,
System.Windows.Forms.KeyPressEventArgs e)
{
keyCharValue = e.KeyChar;
}
private void CurrencyTextBox_TextChanged(object sender, System.EventArgs
e)
{
this.TextChanged -= new EventHandler(this.CurrencyTextBox_TextChanged);
if (!Char.IsDigit(keyCharValue) && keyCharValue != ' ' &&
Convert.ToInt32(keyCharValue) != 8)
{
this.Text = this.Text.Replace(keyCharValue.ToString(), "");
this.Select(this.Text.Length, 0);
}
else
{
this.SuspendLayout();
this.Text = this.Text.Replace(" ", "");
AddSpaceInNumber();
this.Select(this.Text.Length, 0);
this.ResumeLayout();
}
this.TextChanged += new EventHandler(this.CurrencyTextBox_TextChanged);
}
#endregion
}
}
------------------------
Best regards.
--
Aymeric GAURAT APELLI
Consultant 3IE (
http://www.3ie.org)
"Dmitry Karneyev" <ka******@msn.com> wrote in message
news:ut**************@TK2MSFTNGP09.phx.gbl...
How to make TextBox control that allows to input only numerics, currency
etc ?
Or can I download such stuff ?