Highlighted
Alex_Castro Respected Contributor.
Respected Contributor.
90 views

Detecting Numeric keys in WinForms

Jump to solution

What is the command to detect if the key that was pressed was numeric (e.g. 0 – 9) on a Windows Form? Or conversely, if the key that was pressed was NOT numeric? In C# the command is something like:

if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar)

0 Likes
1 Solution

Accepted Solutions
Micro Focus Expert
Micro Focus Expert

Re: Detecting Numeric keys in WinForms

Jump to solution

The corresponding code in Visual COBOL would be:

if not type Char::IsControl(e::KeyChar) and not type Char::IsDigit(e::KeyChar)
   set textBox1::Text to "not digit"
else
   set textBox1::Text to "is digit"
end-if
0 Likes
2 Replies
Micro Focus Expert
Micro Focus Expert

Re: Detecting Numeric keys in WinForms

Jump to solution

The corresponding code in Visual COBOL would be:

if not type Char::IsControl(e::KeyChar) and not type Char::IsDigit(e::KeyChar)
   set textBox1::Text to "not digit"
else
   set textBox1::Text to "is digit"
end-if
0 Likes
Alex_Castro Respected Contributor.
Respected Contributor.

Re: Detecting Numeric keys in WinForms

Jump to solution

The command you suggested was giving me an “Invalid conditional expression” error. So I tried it as follows: I moved the key that was pressed into a variable which I named “digit” and I then compared it against that variable as follows:

if type Char::IsDigit(digit)
      do something
end-if.

This works for what I am trying to achieve. Thanks for your help!

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.