|
-
Jan 7th, 2007, 03:03 PM
#1
Thread Starter
New Member
[2005]
http://pastebin.com/853709
How can I make the program not give an error message if the user leaves the textboxes empty or puts a letter in them instead of a number.
I'm having trouble with stuff like "Error! Can't change TextBox1 to Integer" or something...
I've tried numerous tricks and asked at IRC, but no use... I'm trying to make it "do nothing" if there is something else than numbers in the textboxes.
-
Jan 7th, 2007, 03:12 PM
#2
Re: [2005]
Add some error trapping:
VB Code:
'...
Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
On Error GoTo eh
[B] '...
'your code...
'...[/B]
Exit Sub
eh:
MsgBox("Wrong input.", MsgBoxStyle.Exclamation, "Error")
End Sub
'...
-
Jan 7th, 2007, 03:22 PM
#3
Thread Starter
New Member
Re: [2005]
But if I want to avoid ANY notifications or messageboxes? Just that the program doesn't do anything
-
Jan 7th, 2007, 03:42 PM
#4
Re: [2005]
With .NET you shouldn't use On Error
Use this instead:
VB Code:
Try
'Normal Code here
Catch ex As Exception
End Try
Just don't put anything in the Catch part of the code
Last edited by stimbo; Jan 7th, 2007 at 03:45 PM.
-
Jan 7th, 2007, 03:42 PM
#5
Re: [2005]
Then just remove the "MsgBox(...)" line under "eh:".
-
Jan 7th, 2007, 09:07 PM
#6
Re: [2005]
For half a picosecond's more efficiency, don't declare an Exception object in your Catch block if you're not going to use it:
VB Code:
Try
'Do something.
Catch
'Do nothing.
End Try
Having said that, you should always avoid throwing exceptions if it's at all possible. Converting the contents of a TextBox to a number doesn't need to throw an exception under any circumstances:
VB Code:
Dim number As Integer
If Integer.TryParse(myTextBox.Text, number) Then
'The "number" variable now contains the parsed value.
End If
TryParse returns a Boolean that indicates whether the conversion was successful. If it returns False you just do nothing.
-
Jan 8th, 2007, 12:43 AM
#7
Re: [2005]
 Originally Posted by gavio
Then just remove the "MsgBox(...)" line under "eh:".
No, for .NET code you should use .NET code and not VB 6 "On Error GoTo"
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
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
|