Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 149,921 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,988 people online right now. Registration is fast and FREE... Join Now!




exception Handeling

 
Reply to this topicStart new topic

exception Handeling, Restrict user input to numbers only

simsjunkie2003
11 Dec, 2007 - 02:45 PM
Post #1

New D.I.C Head
*

Joined: 9 Dec, 2007
Posts: 2


My Contributions
The problem I am haveing is that will accept alpha charicters on user input which creates an exception
error and crashes the program and I have no Idea how to fix this. my code for my change follows.
Any Assistance would be appreciater.


CODE


Public Class Form1

    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
        Dim Leftover As Integer
        Leftover = Integer.Parse(txtEnterPennies.Text)
        txtDollars.Text = (Leftover \ 100).ToString
        Leftover = Leftover Mod 100
        txtQuarters.Text = (Leftover \ 25).ToString
        Leftover = Leftover Mod 25
        txtDimes.Text = (Leftover \ 10).ToString
        Leftover = Leftover Mod 10
        txtNickels.Text = (Leftover \ 5).ToString
        Leftover = Leftover Mod 5
        txtPennies.Text = Leftover.ToString
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        txtEnterPennies.Text = ""
        txtDollars.Text = ""
        txtQuarters.Text = ""
        txtDimes.Text = ""
        txtNickels.Text = ""
        txtPennies.Text = ""
    End Sub

End Class



Dick

This post has been edited by simsjunkie2003: 11 Dec, 2007 - 02:46 PM
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Exception Handeling
11 Dec, 2007 - 03:00 PM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,317



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Moved to VB.NET forum.

You need to look at using a Try/Catch to handle your exceptions.

You can find a tutorial here that will help explain Exception Handling.

If you still have questions then feel free to post them.
User is offlineProfile CardPM
+Quote Post

bruce2424
RE: Exception Handeling
26 Jun, 2008 - 10:45 PM
Post #3

New D.I.C Head
*

Joined: 26 Jun, 2008
Posts: 2



Thanked: 1 times
My Contributions
hi sims

you can use exception handling in vb.net.

use try..cath statements in vb.net .

try{

your code

}catch
{
you can catch ur error as exception here.
}

look these links for more details

http://vb.net-informations.com/language/vb..._exceptions.htm

http://vb.net-informations.com/language/vb...s_tutorials.htm

bruce

User is offlineProfile CardPM
+Quote Post

Bort
RE: Exception Handeling
27 Jun, 2008 - 01:54 AM
Post #4

D.I.C Regular
Group Icon

Joined: 18 Sep, 2006
Posts: 487



Thanked: 5 times
Dream Kudos: 350
My Contributions
You can also use an If...Then statement to determine if the input is all numeric, or if there are letters in there too.

CODE

        If IsNumeric(TextBox1.Text) = True Then

                Insert your code here

        End If


Hope this helps
User is offlineProfile CardPM
+Quote Post

Damage
RE: Exception Handeling
27 Jun, 2008 - 02:57 AM
Post #5

D.I.C Addict
Group Icon

Joined: 5 Jun, 2008
Posts: 752



Thanked: 7 times
Dream Kudos: 75
My Contributions
that last suggestion makes is probably the best isn't it? It's better to prevent the user from entering the wrong data than to allow them to enter whatever they want and catch the resultant error.
User is online!Profile CardPM
+Quote Post

Jayman
RE: Exception Handeling
27 Jun, 2008 - 08:19 AM
Post #6

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,317



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
It is not a question of which is better, they are both error handling techniques. One is preventative (user cannot enter invalid data), while the other is not.

Both will work effectively well. It is totally up to you which implementation you choose to use.
User is offlineProfile CardPM
+Quote Post

Damage
RE: Exception Handeling
27 Jun, 2008 - 03:33 PM
Post #7

D.I.C Addict
Group Icon

Joined: 5 Jun, 2008
Posts: 752



Thanked: 7 times
Dream Kudos: 75
My Contributions
sorry, i'm not arguing or anything.I'm new an therefore really curious about the best ways to go about things. If he uses a try catch block wouldn't the the user be forced to wait until execution before they realise their mistake? But then again by testing each character as it's input wouldn't that raise overhead? Like I said just curious
User is online!Profile CardPM
+Quote Post

Locke37
RE: Exception Handeling
27 Jun, 2008 - 05:48 PM
Post #8

Contributor of the Year
Group Icon

Joined: 20 Mar, 2008
Posts: 1,270



Thanked: 56 times
Dream Kudos: 325
My Contributions
QUOTE(bruce2424 @ 26 Jun, 2008 - 11:45 PM) *

hi sims

you can use exception handling in vb.net.

use try..cath statements in vb.net .

try{

your code

}catch
{
you can catch ur error as exception here.
}

look these links for more details

http://vb.net-informations.com/language/vb..._exceptions.htm

http://vb.net-informations.com/language/vb...s_tutorials.htm

bruce


Umm I think you have your languages mixed up a little bit...there are no such things as braces in VB.NET smile.gif That's either C++ or Java.

This post has been edited by Locke37: 27 Jun, 2008 - 05:48 PM
User is offlineProfile CardPM
+Quote Post

AdamSpeight2008
RE: Exception Handeling
27 Jun, 2008 - 06:57 PM
Post #9

LINQ D.I.C.
Group Icon

Joined: 29 May, 2008
Posts: 863



Thanked: 54 times
Dream Kudos: 2250
My Contributions
Presuming you are trying to restrict a textbox to only allow the user to enter numbers.
Try the following
CODE

  Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If e.KeyChar < "0" Or e.KeyChar > "9" Then e.Handled = True

    End Sub

User is offlineProfile CardPM
+Quote Post

AdamSpeight2008
RE: Exception Handeling
27 Jun, 2008 - 09:30 PM
Post #10

LINQ D.I.C.
Group Icon

Joined: 29 May, 2008
Posts: 863



Thanked: 54 times
Dream Kudos: 2250
My Contributions
I've had another idea how to go about this

Add an errorProvider control to the form.
CODE

Private Sub txtEnterPennies_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtEnterPennies.TextChanged
If System.Text.RegularExpressions.Regex.IsMatch(Me.txtEnterPennies.Text, "^[-]{0,1}((\d+)|(\d*.\d+))$") = False Then
  Me.ErrorProvider1.SetError(TextBox1, "Numbers only")
  btnCalculate.enabled = False ' Disable button
Else
  btnCalculate.enabled.Tag = True ' Enable button
  Me.ErrorProvider1.Clear()
End If
End Sub

User is offlineProfile CardPM
+Quote Post

nofear217
RE: Exception Handeling
7 Jul, 2008 - 07:31 AM
Post #11

D.I.C Regular
Group Icon

Joined: 8 Nov, 2007
Posts: 253



Thanked: 6 times
Dream Kudos: 175
My Contributions
And yet another way that is slightly modified from above using the keypress event.
CODE

Dim isKey As Boolean = Char.IsDigit(e.KeyChar)

If Not isKey Then
    e.KeyChar = Nothing
End If


This post has been edited by nofear217: 7 Jul, 2008 - 07:32 AM
User is offlineProfile CardPM
+Quote Post

gever
RE: Exception Handeling
16 Jul, 2008 - 11:10 PM
Post #12

New D.I.C Head
*

Joined: 19 Jun, 2008
Posts: 19



Thanked: 1 times
My Contributions
QUOTE(Damage @ 27 Jun, 2008 - 04:33 PM) *

Umm I think you have your languages mixed up a little bit...there are no such things as braces in VB.NET That's either C++ or Java.


sori for the wrong coding , not much femilier with vb.net thats y.

plz refer like this

Try
your code
exit from Try
Catch [Exception [As Type]]
code - if the exception occurred this code will execute


i found from the following website , just clarify it.

http://vb.net-informations.com/language/vb..._exceptions.htm

bruce.




User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 02:47PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month