Skip to main content
Copy edited. Reformatted the sample code for readability. Removed the two excess "}"s from the sample code (they are probably a copy-paste error by the OP)
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 110
  • 134

restricting Restricting the characters in the text box

On the text box, it should allow the user to enter only 6six decimal places. Ex:For example, 1.012345 or 1,012345.

If 7seven decimal places are tried, the entry should not be allowed.

Please refer to the below code.

private void textBox1_KeyDown_1(object sender, KeyEventArgs e)  {   bool numericKeys = (   ( ((e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9) ||   (e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9)) &&  e.Modifiers != Keys.Shift) || ||  e.KeyCode == Keys.OemMinus || ||  e.KeyCode == Keys.OemPeriod ||  e.KeyCode == Keys.Decimal  ||  e.KeyCode == Keys.Oemcomma);    );  bool navigationKeys = (   e.KeyCode == Keys.Up ||   e.KeyCode == Keys.Right ||   e.KeyCode == Keys.Down ||   e.KeyCode == Keys.Left ||   e.KeyCode == Keys.Home ||   e.KeyCode == Keys.End ||   e.KeyCode == Keys.Tab);   bool editKeys = (e.KeyCode == Keys.Delete ||   e.KeyCode == Keys.Back);     TextBox aTextbox = (TextBox)sender;   aTextbox.Text = "2,33333";   if (!string.IsNullOrEmpty(aTextbox.Text))   {   double maxIntensity;     string aTextValue = aTextbox.Text;     bool value = double.TryParse(aTextValue,  NumberStyles.Any,  CultureInfo.InvariantCulture.NumberFormat,   out maxIntensity);  if (value out maxIntensity);    if (value)  {  {  string aText = maxIntensity.ToString();     MessageBox.Show("the value is {0}", aText);     string[] args = aText.Split('.');   if (numericKeys)   {   bool handleText = true;   if (args.Length > 2)   {   handleText = false;   }   else   {   if (args.Length == 2 && args[1] != null && args[1].Length > 5)   {   handleText = false;   }   }   if (!handleText)   {   e.SuppressKeyPress = true;   e.Handled = true;   }     }   }   }     if (!(numericKeys || editKeys || navigationKeys))   {   e.SuppressKeyPress = true;   e.Handled = true;   }  }  } } 
bool value = double.TryParse(aTextValue,  NumberStyles.Any,  CultureInfo.InvariantCulture.NumberFormat,  out maxIntensity); if (value) { string aText = maxIntensity.ToString(); } 

Splitting the strings to seperateseparate the real part and mantissa part (both are stored are strings), then I check the length of the mantissa part. If the length of the string is more than 5, I'd like to suppress the key.

Is there any otheraother approach to do this?

restricting the characters in the text box

On the text box, it should allow the user to enter only 6 decimal places. Ex: 1.012345 or 1,012345

If 7 decimal places are tried, the entry should not be allowed.

Please refer the below code

private void textBox1_KeyDown_1(object sender, KeyEventArgs e)  {   bool numericKeys = (   (((e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9) ||   (e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9)) && e.Modifiers != Keys.Shift) || e.KeyCode == Keys.OemMinus || e.KeyCode == Keys.OemPeriod || e.KeyCode == Keys.Decimal || e.KeyCode == Keys.Oemcomma);  bool navigationKeys = (   e.KeyCode == Keys.Up ||   e.KeyCode == Keys.Right ||   e.KeyCode == Keys.Down ||   e.KeyCode == Keys.Left ||   e.KeyCode == Keys.Home ||   e.KeyCode == Keys.End ||   e.KeyCode == Keys.Tab);   bool editKeys = (e.KeyCode == Keys.Delete || e.KeyCode == Keys.Back);     TextBox aTextbox = (TextBox)sender;   aTextbox.Text = "2,33333";   if (!string.IsNullOrEmpty(aTextbox.Text))   {   double maxIntensity;     string aTextValue = aTextbox.Text;     bool value = double.TryParse(aTextValue, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out maxIntensity);  if (value) {  string aText = maxIntensity.ToString();     MessageBox.Show("the value is {0}", aText);     string[] args = aText.Split('.');   if (numericKeys)   {   bool handleText = true;   if (args.Length > 2)   {   handleText = false;   }   else   {   if (args.Length == 2 && args[1] != null && args[1].Length > 5)   {   handleText = false;   }   }   if (!handleText)   {   e.SuppressKeyPress = true;   e.Handled = true;   }     }   }   }     if (!(numericKeys || editKeys || navigationKeys))   {   e.SuppressKeyPress = true;   e.Handled = true;   }  }  } } 
bool value = double.TryParse(aTextValue, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out maxIntensity); if (value) { string aText = maxIntensity.ToString(); } 

Splitting the strings to seperate the real part and mantissa part (both are stored are strings), then I check the length of the mantissa part. If the length of the string is more than 5, I'd like to suppress the key.

Is there any other approach to do this?

Restricting the characters in the text box

On the text box, it should allow the user to enter only six decimal places. For example, 1.012345 or 1,012345.

If seven decimal places are tried, the entry should not be allowed.

Please refer to the below code.

private void textBox1_KeyDown_1(object sender, KeyEventArgs e) { bool numericKeys = ( ( ((e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9) || (e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9)) &&  e.Modifiers != Keys.Shift) ||   e.KeyCode == Keys.OemMinus ||   e.KeyCode == Keys.OemPeriod ||  e.KeyCode == Keys.Decimal  ||  e.KeyCode == Keys.Oemcomma   );  bool navigationKeys = ( e.KeyCode == Keys.Up || e.KeyCode == Keys.Right || e.KeyCode == Keys.Down || e.KeyCode == Keys.Left || e.KeyCode == Keys.Home || e.KeyCode == Keys.End || e.KeyCode == Keys.Tab); bool editKeys = (e.KeyCode == Keys.Delete ||   e.KeyCode == Keys.Back); TextBox aTextbox = (TextBox)sender; aTextbox.Text = "2,33333"; if (!string.IsNullOrEmpty(aTextbox.Text)) { double maxIntensity; string aTextValue = aTextbox.Text; bool value = double.TryParse(aTextValue,  NumberStyles.Any,  CultureInfo.InvariantCulture.NumberFormat,    out maxIntensity);    if (value)  {  string aText = maxIntensity.ToString(); MessageBox.Show("the value is {0}", aText); string[] args = aText.Split('.'); if (numericKeys) { bool handleText = true; if (args.Length > 2) { handleText = false; } else { if (args.Length == 2 && args[1] != null && args[1].Length > 5) { handleText = false; } } if (!handleText) { e.SuppressKeyPress = true; e.Handled = true; } } } } if (!(numericKeys || editKeys || navigationKeys)) { e.SuppressKeyPress = true; e.Handled = true; } } 
bool value = double.TryParse(aTextValue,  NumberStyles.Any,  CultureInfo.InvariantCulture.NumberFormat,  out maxIntensity); if (value) { string aText = maxIntensity.ToString(); } 

Splitting the strings to separate the real part and mantissa part (both are stored are strings), then I check the length of the mantissa part. If the length of the string is more than 5, I'd like to suppress the key.

Is there aother approach to do this?

edited tags
Link
Roger Lipscombe
  • 92.5k
  • 59
  • 255
  • 402

onOn the text box, it should be allowedallow the user to enter only 6 decimal places ex . Ex: 1.012345 or 1,012345 if 7

If 7 decimal placeplaces are tried to enter, the entry should not be allowed. Please

Please refer the below code

private void textBox1_KeyDown_1(object sender, KeyEventArgs e) { ///

private void textBox1_KeyDown_1(object sender, KeyEventArgs e)  {  bool numericKeys = ( (((e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9) || (e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9)) && e.Modifiers != Keys.Shift) || e.KeyCode == Keys.OemMinus || e.KeyCode == Keys.OemPeriod || e.KeyCode == Keys.Decimal || e.KeyCode == Keys.Oemcomma); bool navigationKeys = ( e.KeyCode == Keys.Up || e.KeyCode == Keys.Right || e.KeyCode == Keys.Down || e.KeyCode == Keys.Left || e.KeyCode == Keys.Home || e.KeyCode == Keys.End || e.KeyCode == Keys.Tab); bool editKeys = (e.KeyCode == Keys.Delete || e.KeyCode == Keys.Back);   TextBox aTextbox = (TextBox)sender; aTextbox.Text = "2,33333"; if (!string.IsNullOrEmpty(aTextbox.Text)) { double maxIntensity; string aTextValue = aTextbox.Text; bool value = double.TryParse(aTextValue, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out maxIntensity); if (value) { string aText = maxIntensity.ToString(); MessageBox.Show("the value is {0}", aText); string[] args = aText.Split('.'); if (numericKeys) { bool handleText = true; if (args.Length > 2) { handleText = false; } else { if (args.Length == 2 && args[1] != null && args[1].Length > 5) { handleText = false; } } if (!handleText) { e.SuppressKeyPress = true; e.Handled = true; } } } } if (!(numericKeys || editKeys || navigationKeys)) { e.SuppressKeyPress = true; e.Handled = true; }     }      } } 

}

toTo achieve the culture neutrality  , the text value is converted to doubledouble first and then doublethe double value is converted to string.

bool value = double.TryParse(aTextValue, NumberStyles.Any, CultureInfo.InvariantCulturestring.NumberFormat, out maxIntensity);

 bool value = double.TryParse(aTextValue, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out maxIntensity); if (value)  {   string aText = maxIntensity.ToString(); } 

}

and splitting Splitting the strings to seperate the real part and mantissa part (both are stored are strings), then I check the length of the mantissa part. ifIf the length of the string is more than 5, suprressI'd like to suppress the key.

isIs there any other approach to do this.?

on the text box, it should be allowed to enter only 6 decimal places ex : 1.012345 or 1,012345 if 7 decimal place tried to enter, the entry should not be allowed. Please refer the below code

private void textBox1_KeyDown_1(object sender, KeyEventArgs e) { ///

 bool numericKeys = ( (((e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9) || (e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9)) && e.Modifiers != Keys.Shift) || e.KeyCode == Keys.OemMinus || e.KeyCode == Keys.OemPeriod || e.KeyCode == Keys.Decimal || e.KeyCode == Keys.Oemcomma); bool navigationKeys = ( e.KeyCode == Keys.Up || e.KeyCode == Keys.Right || e.KeyCode == Keys.Down || e.KeyCode == Keys.Left || e.KeyCode == Keys.Home || e.KeyCode == Keys.End || e.KeyCode == Keys.Tab); bool editKeys = (e.KeyCode == Keys.Delete || e.KeyCode == Keys.Back);   TextBox aTextbox = (TextBox)sender; aTextbox.Text = "2,33333"; if (!string.IsNullOrEmpty(aTextbox.Text)) { double maxIntensity; string aTextValue = aTextbox.Text; bool value = double.TryParse(aTextValue, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out maxIntensity); if (value) { string aText = maxIntensity.ToString(); MessageBox.Show("the value is {0}", aText); string[] args = aText.Split('.'); if (numericKeys) { bool handleText = true; if (args.Length > 2) { handleText = false; } else { if (args.Length == 2 && args[1] != null && args[1].Length > 5) { handleText = false; } } if (!handleText) { e.SuppressKeyPress = true; e.Handled = true; } } } } if (!(numericKeys || editKeys || navigationKeys)) { e.SuppressKeyPress = true; e.Handled = true; }   }      } 

}

to achieve the culture neutrality  , the text value is converted to double first and then double value is converted to string.

bool value = double.TryParse(aTextValue, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out maxIntensity);

  if (value)  {   string aText = maxIntensity.ToString(); 

}

and splitting the strings to seperate the real part and mantissa part (both are stored are strings) then check the length of the mantissa part. if the length of string is more than 5, suprress the key

is there any other approach to do this.

On the text box, it should allow the user to enter only 6 decimal places. Ex: 1.012345 or 1,012345

If 7 decimal places are tried, the entry should not be allowed.

Please refer the below code

private void textBox1_KeyDown_1(object sender, KeyEventArgs e)  {  bool numericKeys = ( (((e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9) || (e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9)) && e.Modifiers != Keys.Shift) || e.KeyCode == Keys.OemMinus || e.KeyCode == Keys.OemPeriod || e.KeyCode == Keys.Decimal || e.KeyCode == Keys.Oemcomma); bool navigationKeys = ( e.KeyCode == Keys.Up || e.KeyCode == Keys.Right || e.KeyCode == Keys.Down || e.KeyCode == Keys.Left || e.KeyCode == Keys.Home || e.KeyCode == Keys.End || e.KeyCode == Keys.Tab); bool editKeys = (e.KeyCode == Keys.Delete || e.KeyCode == Keys.Back); TextBox aTextbox = (TextBox)sender; aTextbox.Text = "2,33333"; if (!string.IsNullOrEmpty(aTextbox.Text)) { double maxIntensity; string aTextValue = aTextbox.Text; bool value = double.TryParse(aTextValue, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out maxIntensity); if (value) { string aText = maxIntensity.ToString(); MessageBox.Show("the value is {0}", aText); string[] args = aText.Split('.'); if (numericKeys) { bool handleText = true; if (args.Length > 2) { handleText = false; } else { if (args.Length == 2 && args[1] != null && args[1].Length > 5) { handleText = false; } } if (!handleText) { e.SuppressKeyPress = true; e.Handled = true; } } } } if (!(numericKeys || editKeys || navigationKeys)) { e.SuppressKeyPress = true; e.Handled = true; }   }  } } 

To achieve the culture neutrality, the text value is converted to double first and then the double value is converted to string.

bool value = double.TryParse(aTextValue, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out maxIntensity); if (value) { string aText = maxIntensity.ToString(); } 

Splitting the strings to seperate the real part and mantissa part (both are stored are strings), then I check the length of the mantissa part. If the length of the string is more than 5, I'd like to suppress the key.

Is there any other approach to do this?

Source Link
Loading