how to do this in .net - the Usual Story

Posted by jmls on 20-Apr-2010 01:24

Is that I need to do something simple, and it's taking me forever !

I need to use a standard windows text box, but only allow numerics. No progress formatting here

I found the following code

Private Overloads Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
       
Dim isKey As Boolean = e.KeyChar.IsDigit(e.KeyChar)
     
Dim isDecimal As Boolean = e.KeyChar.ToString = "."
       
If Not isKey And Not isDecimal Then
            e.Handled = True
        End If
End
Sub

but I cannot get e:KeyChar:IsDigit to work, as KeyChar appears to be a character !

I've also tried to use the Char() method, but cannot find where it is defined (All c# examples point to using system;)

Arrggh

All Replies

Posted by Admin on 20-Apr-2010 01:31

I need to use a standard windows text box, but only allow numerics. No progress formatting here

 

Infragistics Controls are not an option? We've written code that translates an ABL Format String to an Infragistics InputMask.

I've also tried to use the Char() method, but cannot find where it is defined (All c# examples point to using system;)

 

AFAIK Char() is a C# method or keyword, not a method of an object. CHR or ASC might work as equivalent ABL functions.

Posted by Håvard Danielsen on 20-Apr-2010 07:44

Private Overloads Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
       
Dim isKey As Boolean = e.KeyChar.IsDigit(e.KeyChar)
     
Dim isDecimal As Boolean = e.KeyChar.ToString = "."
       
If Not isKey And Not isDecimal Then
            e.Handled = True
        End If
End
Sub

but I cannot get e:KeyChar:IsDigit to work, as KeyChar appears to be a character !


IsDigit is a static method, so you can call it directly on the class as follows:

@VisualDesigner.
method private void textBox1_KeyPress( sender as System.Object, e as System.Windows.Forms.KeyPressEventArgs ):
       define variable isKey     as logical no-undo.
       define variable isDecimal as logical no-undo.
       assign    
             isKey = System.Char:IsDigit(e:KeyChar)
             isDecimal = (e:KeyChar = session:numeric-decimal-point).
       if not isKey and not isDecimal then
            e:handled = true.
        
       return.
end method.

I used the KeyPress event, which passes the KeyPressEventArgs similar to your C# sample.

I'm not convinced this is the best way to test for numerics, not even in C#, you can for example add as many decimal points as you like and you cannot backspace.

I would have used Infragistics as Mike suggests.

Posted by jmls on 20-Apr-2010 08:21

Thanks to all, I did end up using the IG control .

On 20 April 2010 13:44, Havard Danielsen

Posted by Admin on 20-Apr-2010 08:31

Thanks to all, I did end up using the IG control .

 

Let me know when translating FORMAT strings is of interest for you.

This thread is closed