Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to force numeric entry?
Message
General information
Forum:
ASP.NET
Category:
Forms
Miscellaneous
Thread ID:
00746296
Message ID:
00746573
Views:
24
Hi Ahmad,

You can create a custom class based another class. In the textbox example, you can create your own textbox (called NumericTextBox) that has all the characteristics of the standard TextBox but also the new functionality of the KeyPress event. The KeyPress event of the NumericTextBox would override the functionality of the same event in original TextBox. To create the class, you would define the class header as follows:
Public Class NumericTextBox
  Inherits System.Windows.Forms.TextBox
This code would allow the NumericTextBox clss to "inherit" all the functionality of the System.Windows.Forms.TextBox and allow you to overrride methods as well as add new methods and properties to extend how the class operates.

If you don't subclass, then you would need to use the code I sent you on every textbox that you want to have this functionality. If you ever need to modify the code to alter this functionality, then you would need to do this every place you placed the code. By subclassing, you create a class that does what you want and use the control on as many forms as you want. If you need to modify the code, then you change it once in the class and everywhere that uses this class will get the new functionality.

>Cathi, Thank you for your response
>
>Excuse me for this I am new to Class issues, & I didn’t got what you mean by subclassing a textbox?
>And what if I have about 10 texboxes in the same form?
>
>Thanks again
>
>
>>I would recommend subclassing a textbox to allow you to reuse it. Then you need to put code in the KeyPress event to trap the keystrokes. Here is the KeyPress sample code:
>>
>>
>>Private Sub NumericTextBox_KeyPress(ByVal sender As Object, _
>>      ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
>>  Dim KeyAscii As Integer
>>  KeyAscii = Asc(e.KeyChar)
>>
>>  Select Case KeyAscii
>>
>>    Case 48 To 57, 8, 13      ' these are the digits 0-9, backspace,
>>      ' and carriage return
>>      ' we're OK on these, don't do anything
>>
>>    Case 45                   ' minus sign
>>
>>      ' The number can only have one minus sign, so
>>      ' if we already have one, throw this one away
>>      If InStr(Me.Text, "-") <> 0 Then
>>        KeyAscii = 0
>>      End If
>>
>>      ' if the insertion point is not sitting at zero
>>      ' (which is the beginning of the field), throw away the minus
>>      ' sign (because it's not valid except in first position)
>>      If Me.SelectionStart <> 0 Then
>>        KeyAscii = 0
>>      End If
>>
>>    Case 46                   ' this is a period (decimal point)
>>
>>      ' if we already have a period, throw it away
>>      If InStr(Me.Text, ".") <> 0 Then
>>        KeyAscii = 0
>>      End If
>>
>>    Case Else
>>      ' provide no handling for the other keys
>>      KeyAscii = 0
>>
>>  End Select
>>
>>  ' If we want to throw the keystroke away, then set the event
>>  ' as already handled. Otherwise, let the keystroke be handled normally.
>>  If KeyAscii = 0 Then
>>    e.Handled = True
>>  Else
>>    e.Handled = False
>>  End If
>>
>>End Sub
>>
>>
>>>Hi all,
>>>
>>>How to force a form textbox to accept numaric only, while the textbox is using a DataBindings?
>>>
>>>Thanks for the help
-----------------------------------------

Cathi Gero, CPA
Prenia Software & Consulting Services
Microsoft C# / .NET MVP
Mere Mortals for .NET MVP
cgero@prenia.com
www.prenia.com
Weblog: blogs.prenia.com/cathi
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform