Results 1 to 8 of 8

Thread: Very simple beginner problem.

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2014
    Posts
    21

    Very simple beginner problem.

    Hey VBForums. I have only recently started learning VB, only a few weeks experience on very minor projects etc.

    I am making a program that tracks basketball scores.
    Very simple.
    Press button.
    number in textbox increases by one.

    Thats the idea.
    But I have three buttons. One for a single point, a double and a triple.

    So my code for one team is.

    Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles T1Score1.Click
    Dim score1 As Integer = CInt(TextBox1.Text)
    score1 += 1
    TextBox1.Text = score1
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles T1Score3.Click
    Dim score1 As Integer = CInt(TextBox1.Text)
    score1 += 3
    TextBox1.Text = score1
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles T1Score2.Click
    Dim score1 As Integer = CInt(TextBox1.Text)
    score1 += 2
    TextBox1.Text = score1
    End Sub

    When I run the program and press a button to add the score I get an error message saying
    "Conversion from string "" to type 'Integer' is not valid."

    I don't really understand what the problem is. Can anybody explain it?

    I did this thing earlier when I wrote the code incorrectly like this.
    Dim score1 As Integer
    score1 = (+2)
    TextBox1.Text = score1

    but that didn't work.

    BUTTTT

    When I ran the program and pressed it the buttons above +1 and +3 would work as intended. Thanks in advance if anybody can explain this problem. It's for an assessment task.

  2. #2

    Thread Starter
    Junior Member
    Join Date
    Apr 2014
    Posts
    21

    Re: Very simple beginner problem.

    Name:  CODE.jpg
Views: 113
Size:  21.0 KB

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Apr 2014
    Posts
    21

    Re: Very simple beginner problem.

    Name:  SCORE.PNG
Views: 110
Size:  10.6 KB

    Thought this may be relevant.

  4. #4
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Very simple beginner problem.

    That is VB.NET you posted in the VB6 section! I have requested the thread moved to the correct section.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  5. #5
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    750

    Re: Very simple beginner problem.

    Changing CInt to Val will work.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Apr 2014
    Posts
    21

    Re: Very simple beginner problem.

    Quote Originally Posted by Nightwalker83 View Post
    That is VB.NET you posted in the VB6 section! I have requested the thread moved to the correct section.
    Oh yeah.. Sorry about that haha. Well you can see that i'm very new to this then. :P

  7. #7
    Fanatic Member
    Join Date
    Dec 2007
    Location
    West Yorkshire, UK
    Posts
    791

    Re: Very simple beginner problem.

    right at the very start of your code, put this line
    Code:
    Option Strict On
    This will disallow all implicit conversions. However, most usefully, when the program stops and throws an error, you can hover the mouse over the highlighted error and the IDE will give you a suggestion of how to fix the problem.

    CInt is a hangover from VB6 and it does not work as you would expect. If your string contains characters that cannot be converted, such as spaces etc it will simply throw an error.
    You should be using a vb.net equivalent such as Convert.ToInt32, MSDN example here.
    It allows you to verify that the value can be converted and deal with errors much more easily than CInt

    Alternatively, instead of using a textbox for numeric input, you could use a NumericUpDown which will only allow numeric input.

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Apr 2014
    Posts
    21

    Re: Very simple beginner problem.

    Thank you so much Such a simple fix thank you so much. I've been searching everywhere.

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