visual studio propiedades ejemplo control contraseña c# .net winforms textbox

c# - studio - ¿Cómo hago un cuadro de texto que solo acepta números?



textbox visual studio (30)

3 solución

1)

//Add to the textbox''s KeyPress event //using Regex for number only textBox private void txtBox_KeyPress(object sender, KeyPressEventArgs e) { if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "//d+")) e.Handled = true; }

2) otra solución de msdn

// Boolean flag used to determine when a character other than a number is entered. private bool nonNumberEntered = false; // Handle the KeyDown event to determine the type of character entered into the control. private void textBox1_KeyDown(object sender, KeyEventArgs e) { // Initialize the flag to false. nonNumberEntered = false; // Determine whether the keystroke is a number from the top of the keyboard. if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9) { // Determine whether the keystroke is a number from the keypad. if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9) { // Determine whether the keystroke is a backspace. if (e.KeyCode != Keys.Back) { // A non-numerical keystroke was pressed. // Set the flag to true and evaluate in KeyPress event. nonNumberEntered = true; } } }

}

private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { if (nonNumberEntered == true) { MessageBox.Show("Please enter number only..."); e.Handled = true; } }

fuente http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress(v=VS.90).aspx

3) utilizando el MaskedTextBox: MaskedTextBox

Tengo una aplicación de formularios de Windows con un control de cuadro de texto que solo quiero aceptar valores enteros. En el pasado, he realizado este tipo de validación sobrecargando el evento KeyPress y simplemente eliminando los caracteres que no se ajustaban a la especificación. He examinado el control MaskedTextBox, pero me gustaría una solución más general que pudiera funcionar con una expresión regular o depender de los valores de otros controles.

Idealmente, esto se comportaría de tal manera que al presionar un carácter no numérico no se obtendría ningún resultado o se proporcionaría inmediatamente al usuario comentarios sobre el carácter no válido.


Aquí hay un simple control personalizado de Winforms, derivado del TextBox estándar, que permite solo la entrada a System.Int32 (se podría adaptar fácilmente para otros tipos como System.Int64, etc.). Admite operaciones de copiar / pegar y números negativos:

public class Int32TextBox : TextBox { protected override void OnKeyPress(KeyPressEventArgs e) { base.OnKeyPress(e); NumberFormatInfo fi = CultureInfo.CurrentCulture.NumberFormat; string c = e.KeyChar.ToString(); if (char.IsDigit(c, 0)) return; if ((SelectionStart == 0) && (c.Equals(fi.NegativeSign))) return; // copy/paste if ((((int)e.KeyChar == 22) || ((int)e.KeyChar == 3)) && ((ModifierKeys & Keys.Control) == Keys.Control)) return; if (e.KeyChar == ''/b'') return; e.Handled = true; } protected override void WndProc(ref System.Windows.Forms.Message m) { const int WM_PASTE = 0x0302; if (m.Msg == WM_PASTE) { string text = Clipboard.GetText(); if (string.IsNullOrEmpty(text)) return; if ((text.IndexOf(''+'') >= 0) && (SelectionStart != 0)) return; int i; if (!int.TryParse(text, out i)) // change this for other integer types return; if ((i < 0) && (SelectionStart != 0)) return; } base.WndProc(ref m); }

Actualización 2017 : Mi primera respuesta tiene algunos problemas:

  • puede escribir algo que sea más largo que un número entero de un tipo dado (por ejemplo, 2147483648 es mayor que Int32.MaxValue);
  • más generalmente, no hay una validación real del resultado de lo que se ha escrito;
  • solo maneja int32, tendrá que escribir un control derivado de TextBox específico para cada tipo (Int64, etc.)

Así que se me ocurrió otra versión más genérica, que todavía soporta copiar / pegar, + y - firmar, etc.

public class ValidatingTextBox : TextBox { private string _validText; private int _selectionStart; private int _selectionEnd; private bool _dontProcessMessages; public event EventHandler<TextValidatingEventArgs> TextValidating; protected virtual void OnTextValidating(object sender, TextValidatingEventArgs e) => TextValidating?.Invoke(sender, e); protected override void WndProc(ref Message m) { base.WndProc(ref m); if (_dontProcessMessages) return; const int WM_KEYDOWN = 0x100; const int WM_ENTERIDLE = 0x121; const int VK_DELETE = 0x2e; bool delete = m.Msg == WM_KEYDOWN && (int)m.WParam == VK_DELETE; if ((m.Msg == WM_KEYDOWN && !delete) || m.Msg == WM_ENTERIDLE) { DontProcessMessage(() => { _validText = Text; _selectionStart = SelectionStart; _selectionEnd = SelectionLength; }); } const int WM_CHAR = 0x102; const int WM_PASTE = 0x302; if (m.Msg == WM_CHAR || m.Msg == WM_PASTE || delete) { string newText = null; DontProcessMessage(() => { newText = Text; }); var e = new TextValidatingEventArgs(newText); OnTextValidating(this, e); if (e.Cancel) { DontProcessMessage(() => { Text = _validText; SelectionStart = _selectionStart; SelectionLength = _selectionEnd; }); } } } private void DontProcessMessage(Action action) { _dontProcessMessages = true; try { action(); } finally { _dontProcessMessages = false; } } } public class TextValidatingEventArgs : CancelEventArgs { public TextValidatingEventArgs(string newText) => NewText = newText; public string NewText { get; } }

Para Int32, puedes derivar de él, como esto:

public class Int32TextBox : ValidatingTextBox { protected override void OnTextValidating(object sender, TextValidatingEventArgs e) { e.Cancel = !int.TryParse(e.NewText, out int i); } }

o sin derivación, use el nuevo evento TextValidating como este:

var vtb = new ValidatingTextBox(); ... vtb.TextValidating += (sender, e) => e.Cancel = !int.TryParse(e.NewText, out int i);

pero lo que es bueno es que funciona con cualquier cadena y cualquier rutina de validación.


Dos opciones:

  1. Utilice un NumericUpDown en NumericUpDown lugar. NumericUpDown hace el filtrado por ti, lo cual es bueno. Por supuesto, también les brinda a los usuarios la capacidad de presionar las flechas hacia arriba y hacia abajo en el teclado para aumentar y disminuir el valor actual.

  2. Maneja los eventos de teclado apropiados para evitar cualquier cosa que no sea una entrada numérica. He tenido éxito con estos dos controladores de eventos en un TextBox estándar:

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && (e.KeyChar != ''.'')) { e.Handled = true; } // only allow one decimal point if ((e.KeyChar == ''.'') && ((sender as TextBox).Text.IndexOf(''.'') > -1)) { e.Handled = true; } }

Puede eliminar el cheque para ''.'' (y la comprobación posterior de más de un ''.'' ) si su TextBox no debe permitir decimales. También puede agregar un cheque para ''-'' si su TextBox debería permitir valores negativos.

Si desea limitar al usuario por número de dígitos, use: textBox1.MaxLength = 2; // this will allow the user to enter only 2 digits textBox1.MaxLength = 2; // this will allow the user to enter only 2 digits


Eche un vistazo a la gestión de entrada en WinForm

He publicado mi solución que utiliza los eventos ProcessCmdKey y OnKeyPress en el cuadro de texto. Los comentarios le muestran cómo utilizar un Regex para verificar la pulsación de tecla y bloquear / permitir adecuadamente.


En nuestra página web con la definición de cuadro de texto, podemos agregar un evento de onkeypress para aceptar solo números. No mostrará ningún mensaje, pero evitará que ingrese información incorrecta. Funcionó para mí, el usuario no pudo ingresar nada excepto el número.

<asp:TextBox runat="server" ID="txtFrom" onkeypress="if(isNaN(String.fromCharCode(event.keyCode))) return false;">


Este funciona con copiar y pegar, arrastrar y soltar, tecla abajo, evita el desbordamiento y es bastante simple

public partial class IntegerBox : TextBox { public IntegerBox() { InitializeComponent(); this.Text = 0.ToString(); } protected override void OnPaint(PaintEventArgs pe) { base.OnPaint(pe); } private String originalValue = 0.ToString(); private void Integerbox_KeyPress(object sender, KeyPressEventArgs e) { originalValue = this.Text; } private void Integerbox_TextChanged(object sender, EventArgs e) { try { if(String.IsNullOrWhiteSpace(this.Text)) { this.Text = 0.ToString(); } this.Text = Convert.ToInt64(this.Text.Trim()).ToString(); } catch (System.OverflowException) { MessageBox.Show("Value entered is to large max value: " + Int64.MaxValue.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); this.Text = originalValue; } catch (System.FormatException) { this.Text = originalValue; } catch (System.Exception ex) { this.Text = originalValue; MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK , MessageBoxIcon.Error); } } }


Esto es exactamente para lo que se diseñaron los eventos Validado / Validado.

Aquí está el artículo de MSDN sobre el tema: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.validating.aspx

La versión TL; DR: verifique la propiedad .Text en el evento de Validación y establezca e.Cancel=True cuando los datos no son válidos.

Cuando configura e.Cancel = True, el usuario no puede abandonar el campo, pero tendrá que darles algún tipo de respuesta de que algo está mal. Cambio el color de fondo del cuadro a rojo claro para indicar un problema. Asegúrese de volver a establecerlo en SystemColors.Window cuando se llama a Validar con un buen valor.


Esto podría ser útil. Permite valores numéricos "reales", incluidos los puntos decimales adecuados y los signos de más o menos anteriores. Llámelo desde dentro del evento KeyPress relacionado.

private bool IsOKForDecimalTextBox(char theCharacter, TextBox theTextBox) { // Only allow control characters, digits, plus and minus signs. // Only allow ONE plus sign. // Only allow ONE minus sign. // Only allow the plus or minus sign as the FIRST character. // Only allow ONE decimal point. // Do NOT allow decimal point or digits BEFORE any plus or minus sign. if ( !char.IsControl(theCharacter) && !char.IsDigit(theCharacter) && (theCharacter != ''.'') && (theCharacter != ''-'') && (theCharacter != ''+'') ) { // Then it is NOT a character we want allowed in the text box. return false; } // Only allow one decimal point. if (theCharacter == ''.'' && theTextBox.Text.IndexOf(''.'') > -1) { // Then there is already a decimal point in the text box. return false; } // Only allow one minus sign. if (theCharacter == ''-'' && theTextBox.Text.IndexOf(''-'') > -1) { // Then there is already a minus sign in the text box. return false; } // Only allow one plus sign. if (theCharacter == ''+'' && theTextBox.Text.IndexOf(''+'') > -1) { // Then there is already a plus sign in the text box. return false; } // Only allow one plus sign OR minus sign, but not both. if ( ( (theCharacter == ''-'') || (theCharacter == ''+'') ) && ( (theTextBox.Text.IndexOf(''-'') > -1) || (theTextBox.Text.IndexOf(''+'') > -1) ) ) { // Then the user is trying to enter a plus or minus sign and // there is ALREADY a plus or minus sign in the text box. return false; } // Only allow a minus or plus sign at the first character position. if ( ( (theCharacter == ''-'') || (theCharacter == ''+'') ) && theTextBox.SelectionStart != 0 ) { // Then the user is trying to enter a minus or plus sign at some position // OTHER than the first character position in the text box. return false; } // Only allow digits and decimal point AFTER any existing plus or minus sign if ( ( // Is digit or decimal point char.IsDigit(theCharacter) || (theCharacter == ''.'') ) && ( // A plus or minus sign EXISTS (theTextBox.Text.IndexOf(''-'') > -1) || (theTextBox.Text.IndexOf(''+'') > -1) ) && // Attempting to put the character at the beginning of the field. theTextBox.SelectionStart == 0 ) { // Then the user is trying to enter a digit or decimal point in front of a minus or plus sign. return false; } // Otherwise the character is perfectly fine for a decimal value and the character // may indeed be placed at the current insertion position. return true; }


He estado trabajando en una colección de componentes para completar cosas que faltan en WinForms, aquí está: Formularios avanzados

En particular, esta es la clase para un Regex TextBox

/// <summary>Represents a Windows text box control that only allows input that matches a regular expression.</summary> public class RegexTextBox : TextBox { [NonSerialized] string lastText; /// <summary>A regular expression governing the input allowed in this text field.</summary> [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public virtual Regex Regex { get; set; } /// <summary>A regular expression governing the input allowed in this text field.</summary> [DefaultValue(null)] [Category("Behavior")] [Description("Sets the regular expression governing the input allowed for this control.")] public virtual string RegexString { get { return Regex == null ? string.Empty : Regex.ToString(); } set { if (string.IsNullOrEmpty(value)) Regex = null; else Regex = new Regex(value); } } protected override void OnTextChanged(EventArgs e) { if (Regex != null && !Regex.IsMatch(Text)) { int pos = SelectionStart - Text.Length + (lastText ?? string.Empty).Length; Text = lastText; SelectionStart = Math.Max(0, pos); } lastText = Text; base.OnTextChanged(e); } }

Simplemente agregando algo como myNumbericTextBox.RegexString = "^(//d+|)$"; debería bastar


He hecho algo para esto en CodePlex .

Funciona interceptando el evento TextChanged. Si el resultado es un buen número será almacenado. Si es algo malo, se restaurará el último valor bueno. La fuente es un poco demasiado grande para publicar aquí, pero aquí hay un enlace a la clase que maneja el núcleo de esta lógica.


Hola, puedes hacer algo como esto en el evento de cambio de texto del cuadro de texto.

aqui hay una demo

private void textBox1_TextChanged(object sender, EventArgs e) { string actualdata = string.Empty; char[] entereddata = textBox1.Text.ToCharArray(); foreach (char aChar in entereddata.AsEnumerable()) { if (Char.IsDigit(aChar)) { actualdata = actualdata + aChar; // MessageBox.Show(aChar.ToString()); } else { MessageBox.Show(aChar + " is not numeric"); actualdata.Replace(aChar, '' ''); actualdata.Trim(); } } textBox1.Text = actualdata; }


Lamento despertar a los muertos, pero pensé que alguien podría encontrar esto útil para futuras referencias.

Así es como lo manejo. Maneja números de punto flotante, pero se puede modificar fácilmente para los enteros.

Básicamente solo puedes presionar 0 - 9 y .

Solo puedes tener un 0 antes del .

Todos los demás caracteres se ignoran y la posición del cursor se mantiene.

private bool _myTextBoxChanging = false; private void myTextBox_TextChanged(object sender, EventArgs e) { validateText(myTextBox); } private void validateText(TextBox box) { // stop multiple changes; if (_myTextBoxChanging) return; _myTextBoxChanging = true; string text = box.Text; if (text == "") return; string validText = ""; bool hasPeriod = false; int pos = box.SelectionStart; for (int i = 0; i < text.Length; i++ ) { bool badChar = false; char s = text[i]; if (s == ''.'') { if (hasPeriod) badChar = true; else hasPeriod = true; } else if (s < ''0'' || s > ''9'') badChar = true; if (!badChar) validText += s; else { if (i <= pos) pos--; } } // trim starting 00s while (validText.Length >= 2 && validText[0] == ''0'') { if (validText[1] != ''.'') { validText = validText.Substring(1); if (pos < 2) pos--; } else break; } if (pos > validText.Length) pos = validText.Length; box.Text = validText; box.SelectionStart = pos; _myTextBoxChanging = false; }

Aquí hay una versión int modificada rápidamente:

private void validateText(TextBox box) { // stop multiple changes; if (_myTextBoxChanging) return; _myTextBoxChanging = true; string text = box.Text; if (text == "") return; string validText = ""; int pos = box.SelectionStart; for (int i = 0; i < text.Length; i++ ) { char s = text[i]; if (s < ''0'' || s > ''9'') { if (i <= pos) pos--; } else validText += s; } // trim starting 00s while (validText.Length >= 2 && validText.StartsWith("00")) { validText = validText.Substring(1); if (pos < 2) pos--; } if (pos > validText.Length) pos = validText.Length; box.Text = validText; box.SelectionStart = pos; _myTextBoxChanging = false; }


Lo manejaría en el evento KeyDown.

void TextBox_KeyDown(object sender, KeyEventArgs e) { char c = Convert.ToChar(e.PlatformKeyCode); if (!char.IsDigit(c)) { e.Handled = true; } }


No olvide que un usuario puede pegar un texto no válido en un TextBox texto.

Si desea restringir eso, siga el siguiente código:

private void ultraTextEditor1_TextChanged(object sender, EventArgs e) { string append=""; foreach (char c in ultraTextEditor1.Text) { if ((!Char.IsNumber(c)) && (c != Convert.ToChar(Keys.Back))) { } else { append += c; } } ultraTextEditor1.Text = append; }


Parece que muchas de las respuestas actuales a esta pregunta están analizando manualmente el texto de entrada. Si está buscando un tipo numérico incorporado específico (por ejemplo, int o double ), ¿por qué no delegar el trabajo al método TryParse ese tipo? Por ejemplo:

public class IntTextBox : TextBox { string PreviousText = ""; int BackingResult; public IntTextBox() { TextChanged += IntTextBox_TextChanged; } public bool HasResult { get; private set; } public int Result { get { return HasResult ? BackingResult : default(int); } } void IntTextBox_TextChanged(object sender, EventArgs e) { HasResult = int.TryParse(Text, out BackingResult); if (HasResult || string.IsNullOrEmpty(Text)) { // Commit PreviousText = Text; } else { // Revert var changeOffset = Text.Length - PreviousText.Length; var previousSelectionStart = Math.Max(0, SelectionStart - changeOffset); Text = PreviousText; SelectionStart = previousSelectionStart; } } }

Si quieres algo más genérico pero aún compatible con Visual Studio''s Designer:

public class ParsableTextBox : TextBox { TryParser BackingTryParse; string PreviousText = ""; object BackingResult; public ParsableTextBox() : this(null) { } public ParsableTextBox(TryParser tryParse) { TryParse = tryParse; TextChanged += ParsableTextBox_TextChanged; } public delegate bool TryParser(string text, out object result); public TryParser TryParse { set { Enabled = !(ReadOnly = value == null); BackingTryParse = value; } } public bool HasResult { get; private set; } public object Result { get { return GetResult<object>(); } } public T GetResult<T>() { return HasResult ? (T)BackingResult : default(T); } void ParsableTextBox_TextChanged(object sender, EventArgs e) { if (BackingTryParse != null) { HasResult = BackingTryParse(Text, out BackingResult); } if (HasResult || string.IsNullOrEmpty(Text)) { // Commit PreviousText = Text; } else { // Revert var changeOffset = Text.Length - PreviousText.Length; var previousSelectionStart = Math.Max(0, SelectionStart - changeOffset); Text = PreviousText; SelectionStart = previousSelectionStart; } } }

Y, por último, si quieres algo totalmente genérico y no te importa el soporte de Designer:

public class ParsableTextBox<T> : TextBox { TryParser BackingTryParse; string PreviousText; T BackingResult; public ParsableTextBox() : this(null) { } public ParsableTextBox(TryParser tryParse) { TryParse = tryParse; TextChanged += ParsableTextBox_TextChanged; } public delegate bool TryParser(string text, out T result); public TryParser TryParse { set { Enabled = !(ReadOnly = value == null); BackingTryParse = value; } } public bool HasResult { get; private set; } public T Result { get { return HasResult ? BackingResult : default(T); } } void ParsableTextBox_TextChanged(object sender, EventArgs e) { if (BackingTryParse != null) { HasResult = BackingTryParse(Text, out BackingResult); } if (HasResult || string.IsNullOrEmpty(Text)) { // Commit PreviousText = Text; } else { // Revert var changeOffset = Text.Length - PreviousText.Length; var previousSelectionStart = Math.Max(0, SelectionStart - changeOffset); Text = PreviousText; SelectionStart = previousSelectionStart; } } }


Pruebe un MaskedTextBox . Toma un formato de máscara simple para que pueda limitar la entrada a números o fechas o lo que sea.


Puedes usar el evento TextChanged

private void textBox_BiggerThan_TextChanged(object sender, EventArgs e) { long a; if (! long.TryParse(textBox_BiggerThan.Text, out a)) { // If not int clear textbox text or Undo() last operation textBox_LessThan.Clear(); } }


Se deben aceptar tanto los enteros como los flotantes, incluidos los números negativos.

private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { // Text string text = ((Control) sender).Text; // Is Negative Number? if (e.KeyChar == ''-'' && text.Length == 0) { e.Handled = false; return; } // Is Float Number? if (e.KeyChar == ''.'' && text.Length > 0 && !text.Contains(".")) { e.Handled = false; return; } // Is Digit? e.Handled = (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar)); }


Simplemente use un control NumericUpDown y establezca la visibilidad de esos feos botones hacia abajo en false .

numericUpDown1.Controls[0].Visible = false;

NumericUpDown es en realidad una colección de controles que contienen un ''cuadro de giro'' (botones hacia arriba), un cuadro de texto y un código para validar y cambiar todo de forma conjunta.

Calificación:

YourNumericUpDown.Controls[0].visible = false

Ocultará los botones mientras mantiene activo el código subyacente.

Si bien no es una solución obvia, es simple y eficaz. .Controls[1] ocultaría la parte del cuadro de texto si quisiera hacerlo en su lugar.


Supongo que, a partir del contexto y las etiquetas que usaste, estás escribiendo una aplicación .NET C #. En este caso, puede suscribirse al evento de cambio de texto y validar cada golpe de tecla.

private void textBox1_TextChanged(object sender, EventArgs e) { if (System.Text.RegularExpressions.Regex.IsMatch(textBox1.Text, "[^0-9]")) { MessageBox.Show("Please enter only numbers."); textBox1.Text = textBox1.Text.Remove(textBox1.Text.Length - 1); } }


También estaba buscando la mejor forma de verificar solo los números en el cuadro de texto y el problema con la pulsación de tecla era que no es compatible con la función de pegar o copiar con el botón derecho o el portapapeles, por lo que surgió este código que valida el momento en que el cursor abandona el campo de texto y también verifica campo vacío. (versión adaptada de newguy)

private void txtFirstValue_MouseLeave(object sender, EventArgs e) { int num; bool isNum = int.TryParse(txtFirstValue.Text.Trim(), out num); if (!isNum && txtFirstValue.Text != String.Empty) { MessageBox.Show("The First Value You Entered Is Not a Number, Please Try Again", "Invalid Value Detected", MessageBoxButtons.OK, MessageBoxIcon.Error); txtFirstValue.Clear(); } }


Usando el enfoque descrito en share he creado una solución más genérica:

public abstract class ValidatedTextBox : TextBox { private string m_lastText = string.Empty; protected abstract bool IsValid(string text); protected sealed override void OnTextChanged(EventArgs e) { if (!IsValid(Text)) { var pos = SelectionStart - Text.Length + m_lastText.Length; Text = m_lastText; SelectionStart = Math.Max(0, pos); } m_lastText = Text; base.OnTextChanged(e); } }

"ValidatedTextBox", que contiene todos los comportamientos de validación no triviales. Todo lo que queda por hacer es heredar de esta clase y anular el método "IsValid" con la lógica de validación necesaria. Por ejemplo, al usar esta clase, es posible crear "RegexedTextBox" que solo aceptará cadenas que coincidan con expresiones regulares específicas:

public abstract class RegexedTextBox : ValidatedTextBox { private readonly Regex m_regex; protected RegexedTextBox(string regExpString) { m_regex = new Regex(regExpString); } protected override bool IsValid(string text) { return m_regex.IsMatch(Text); } }

Después de eso, heredando de la clase "RegexedTextBox", podemos crear fácilmente los controles "PositiveNumberTextBox" y "PositiveFloatingPointNumberTextBox":

public sealed class PositiveNumberTextBox : RegexedTextBox { public PositiveNumberTextBox() : base(@"^/d*$") { } } public sealed class PositiveFloatingPointNumberTextBox : RegexedTextBox { public PositiveFloatingPointNumberTextBox() : base(@"^(/d+/" + CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator + @")?/d*$") { } }


Y solo porque siempre es más divertido hacer cosas en una línea ...

private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar); }

NOTA: Esto NO impide que un usuario copie / pegue en este cuadro de texto. No es una forma segura de desinfectar sus datos.


podría usar el evento TextChanged / Keypress, usar una expresión regular para filtrar los números y realizar alguna acción.


simplemente use este código en el cuadro de texto:

private void textBox1_TextChanged(object sender, EventArgs e) { double parsedValue; if (!double.TryParse(textBox1.Text, out parsedValue)) { textBox1.Text = ""; } }


FAIL-SAFE y método simple "recursivo", que se puede usar con múltiples cuadros de texto.

Bloquea los caracteres tecleados incorrectos y también los valores pegados, etc. Solo acepta números enteros, y la longitud máxima del número es la longitud máxima de un tipo de cadena (que es int, ¡realmente larga!)

public void Check_If_Int_On_TextChanged(object sender, EventArgs e) { // This method checks that each inputed character is a number. Any non-numeric // characters are removed from the text TextBox textbox = (TextBox)sender; // If the text is empty, return if (textbox.Text.Length == 0) { return; } // Check the new Text value if it''s only numbers byte parsedValue; if (!byte.TryParse(textbox.Text[(textbox.Text.Length - 1)].ToString(), out parsedValue)) { // Remove the last character as it wasn''t a number textbox.Text = textbox.Text.Remove((textbox.Text.Length - 1)); // Move the cursor to the end of text textbox.SelectionStart = textbox.Text.Length; } }


Al hacer clic en el botón se puede verificar el texto del cuadro de texto mediante for loop:

char[] c = txtGetCustomerId.Text.ToCharArray(); bool IsDigi = true; for (int i = 0; i < c.Length; i++) { if (c[i] < ''0'' || c[i] > ''9'') { IsDigi = false; } } if (IsDigi) { // do something }


Respuesta más sencilla:

_textBox.TextChanged += delegate(System.Object o, System.EventArgs e) { TextBox _tbox = o as TextBox; _tbox.Text = new string(_tbox.Text.Where(c => (char.IsDigit(c)) || (c == ''.'')).ToArray()); };


int Number; bool isNumber; isNumber = int32.TryPase(textbox1.text, out Number); if (!isNumber) { (code if not an integer); } else { (code if an integer); }


private void txt3_KeyPress(object sender, KeyPressEventArgs e) { for (int h = 58; h <= 127; h++) { if (e.KeyChar == h) //58 to 127 is alphabets tat will be blocked { e.Handled = true; } } for(int k=32;k<=47;k++) { if (e.KeyChar == k) //32 to 47 are special characters tat will { be blocked e.Handled = true; } } }

prueba esto es muy simple