|
-
Nov 27th, 2003, 04:22 AM
#1
Thread Starter
New Member
VB>NET : Allow a Textbox only to Accept Numeric [SOLVED]
Hi all
Does anyone know how to Allow a User only to Type numeric characters only into a textbox.
I know in VB6 you could catch the char and set it to '0' if not valid
Code:
If not KeyAscii = '27' then
But how do you make it work in VB.NET ?
I have tried adding the Numberbox control to my app but somehow I get errors with this control when exiting the App
Last edited by StaticInc; Nov 27th, 2003 at 04:38 AM.
'When all esle fails
On Error Resume Next
-
Nov 27th, 2003, 04:34 AM
#2
like this...
VB Code:
[Color=Blue]Private[/color] [Color=Blue]Sub[/color] TextBox1_KeyPress([Color=Blue]ByVal[/color] sender [Color=Blue]As[/color] [Color=Blue]Object[/color], [Color=Blue]ByVal[/color] e [Color=Blue]As[/color] System.Windows.Forms.KeyPressEventArgs) [Color=Blue]Handles[/color] TextBox1.KeyPress
[Color=Blue]If[/color] e.KeyChar.IsNumber(e.KeyChar) [Color=Blue]Then
[/color] e.Handled = [Color=Blue]False
[/color] [Color=Blue]Else
[/color] e.Handled = [Color=Blue]True
[/color] [Color=Blue]End[/color] [Color=Blue]If
[/color] [Color=Blue]End[/color] [Color=Blue]Sub[/color]
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
-
Nov 27th, 2003, 04:37 AM
#3
Thread Starter
New Member
I knew I forgot to check something
Thanx It Works
'When all esle fails
On Error Resume Next
-
Nov 27th, 2003, 05:07 AM
#4
Lively Member
But if i have many Text boxs then do i have to write in every text box keypress event
Can we call some function in formkeypress event
with Regards
sameer Mulgaonkar

-
Nov 27th, 2003, 05:58 AM
#5
Hyperactive Member
better make an array of those txt boxes or declare them dynamically and then u can use that
-
Nov 27th, 2003, 07:19 AM
#6
you need to make a Handler for all the textboxes like this then...
VB Code:
[Color=Blue]Private[/color] [Color=Blue]WithEvents[/color] txtBox [Color=Blue]As[/color] TextBox
[Color=Blue]Private[/color] [Color=Blue]Sub[/color] Form1_Load([Color=Blue]ByVal[/color] sender [Color=Blue]As[/color] System.Object, [Color=Blue]ByVal[/color] e [Color=Blue]As[/color] System.EventArgs) [Color=Blue]Handles[/color] [Color=Blue]MyBase[/color].Load
[Color=Blue]Dim[/color] ctl [Color=Blue]As[/color] Control
[Color=Blue]For[/color] [Color=Blue]Each[/color] ctl [Color=Blue]In[/color] [Color=Blue]Me[/color].Controls
[Color=Blue]If[/color] [Color=Blue]TypeOf[/color] ctl [Color=Blue]Is[/color] TextBox [Color=Blue]Then
[/color] [Color=Blue]AddHandler[/color] ctl.KeyPress, [Color=Blue]AddressOf[/color] txtBox_KeyPress
[Color=Blue]End[/color] [Color=Blue]If
[/color] [Color=Blue]Next
[/color] [Color=Blue]End[/color] [Color=Blue]Sub
[/color] [Color=Blue]Private[/color] [Color=Blue]Sub[/color] txtBox_KeyPress([Color=Blue]ByVal[/color] sender [Color=Blue]As[/color] [Color=Blue]Object[/color], [Color=Blue]ByVal[/color] e [Color=Blue]As[/color] System.Windows.Forms.KeyPressEventArgs) [Color=Blue]Handles[/color] txtBox.KeyPress
[Color=Blue]If[/color] e.KeyChar.IsNumber(e.KeyChar) [Color=Blue]Then
[/color] e.Handled = [Color=Blue]False
[/color] [Color=Blue]Else
[/color] e.Handled = [Color=Blue]True
[/color] [Color=Blue]End[/color] [Color=Blue]If
[/color] [Color=Blue]End[/color] [Color=Blue]Sub[/color]
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|