I am trying to take input using a textbox in windows forms and so far (pretty simple, nothing special), I have added this to stop it from taking input characters
private void txtStartFrom_KeyPress(object sender, KeyPressEventArgs e)
{
if(!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
}
This is pretty standard so far.
Now I want to stop people from copy pasting any characters too, except numbers. Neither from using right click to paste character (Right click to paste numbers should work) or ctrl+v charaters (here too, ctrl+v for numbers should work)
I have spent the past 2 hours seraching for any viable solution, and have came with none. Please help.
Also, if similar solution exists for my problem, I am sorry to have asked a same question. Please refer me to the solution.
I have tried by writing this piece of code
if (char.IsControl(e.KeyChar))
{
if (!int.TryParse(txtStartFrom.Text.Trim(), out int value))
e.Handled = true;
}
But it doesnt work. And I am unable to debug.
So can anybody explain what is char.IsControl(e.KeyChar) supposed to lookout for.
I’m sorry for not being very brief.
Thanks in advance.