Hola amigos, en esta ocasion veremos como lograr que solo se puedan introducir numeros en un TextBox a traves de una clase C#:
public class TextBoxAdd{
static int _decimales = 3;
/// <summary>
/// Cantidad de decimales
/// </summary>
public int Decimales{
get {
return (_decimales);
}
set {
_decimales = value;
}
}
/// <summary>
/// Solo números
/// </summary>
/// <param name="_TextBox">Objeto TextBox</param>
/// <param name="e">KeyPressEventArgs</param>
public static void OnlyNumeric(System.Windows.Forms.TextBox _TextBox,
System.Windows.Forms.KeyPressEventArgs e){
if (e.KeyChar == 8){
e.Handled = false;
return;
}
if (_TextBox.Text.Length == 0) {
if((e.KeyChar == Convert.ToChar("-" ) ) ) {
e.Handled = false;
return;
}
}
bool IsDec = false;
int nroDec = 0;
for (int i = 0; i < _TextBox.Text.Length; i++){
if (_TextBox.Text == '.')
IsDec = true;
if (IsDec && nroDec++ >= _decimales){
e.Handled = true;
return;
}
}
if (e.KeyChar >= 48 && e.KeyChar <= 57)
e.Handled = false;
else if (e.KeyChar == 46)
e.Handled = (IsDec) ? true : false;
else
e.Handled = true;
}
}
Bueno, esto es todo, visiten mi blog:
Si desean que les haga un sistema/aplicación/programa:
public class TextBoxAdd{
static int _decimales = 3;
/// <summary>
/// Cantidad de decimales
/// </summary>
public int Decimales{
get {
return (_decimales);
}
set {
_decimales = value;
}
}
/// <summary>
/// Solo números
/// </summary>
/// <param name="_TextBox">Objeto TextBox</param>
/// <param name="e">KeyPressEventArgs</param>
public static void OnlyNumeric(System.Windows.Forms.TextBox _TextBox,
System.Windows.Forms.KeyPressEventArgs e){
if (e.KeyChar == 8){
e.Handled = false;
return;
}
if (_TextBox.Text.Length == 0) {
if((e.KeyChar == Convert.ToChar("-" ) ) ) {
e.Handled = false;
return;
}
}
bool IsDec = false;
int nroDec = 0;
for (int i = 0; i < _TextBox.Text.Length; i++){
if (_TextBox.Text == '.')
IsDec = true;
if (IsDec && nroDec++ >= _decimales){
e.Handled = true;
return;
}
}
if (e.KeyChar >= 48 && e.KeyChar <= 57)
e.Handled = false;
else if (e.KeyChar == 46)
e.Handled = (IsDec) ? true : false;
else
e.Handled = true;
}
}
Bueno, esto es todo, visiten mi blog:
Si desean que les haga un sistema/aplicación/programa: