Results 1 to 7 of 7

Thread: [2005]

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    11

    Question [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.

  2. #2
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: [2005]

    Add some error trapping:
    VB Code:
    1. '...
    2. Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    3.     On Error GoTo eh
    4. [B]        '...
    5.         'your code...
    6.         '...[/B]
    7.     Exit Sub
    8. eh:
    9.     MsgBox("Wrong input.", MsgBoxStyle.Exclamation, "Error")
    10. End Sub
    11. '...

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    11

    Re: [2005]

    But if I want to avoid ANY notifications or messageboxes? Just that the program doesn't do anything

  4. #4
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005]

    With .NET you shouldn't use On Error

    Use this instead:

    VB Code:
    1. Try
    2.         'Normal Code here
    3.  
    4.         Catch ex As Exception
    5.  
    6.         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.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  5. #5

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Try
    2.     'Do something.
    3. Catch
    4.     'Do nothing.
    5. 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:
    1. Dim number As Integer
    2.  
    3. If Integer.TryParse(myTextBox.Text, number) Then
    4.     'The "number" variable now contains the parsed value.
    5. End If
    TryParse returns a Boolean that indicates whether the conversion was successful. If it returns False you just do nothing.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [2005]

    Quote 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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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
  •  



Click Here to Expand Forum to Full Width