I've assigned the following method to all of my
private void textBox18_KeyPress_1(object sender, KeyPressEventArgs e)
{
char a = Convert.ToChar(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator);
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) &&
(e.KeyChar != a))
{
e.Handled = true;
}
// only allow one decimal point
if ((e.KeyChar == a) && ((sender as TextBox).Text.IndexOf(a) > -1))
{
e.Handled = true;
}
}
It basically allows one decimal separator (of any kind), and allows only numbers. I'd rather disable "paste" as well on this method. Is this possible?
I know some users might redirect me here
how to disable copy, Paste and delete features on a textbox using C#
But my code doesn't recognize e.Control
and e.KeyCode
. Even when I add using Windows.Forms
at the form beginning. And even then, I wouldn't know if those solutions would work.