Davey
Davey

Reputation: 129

VB.NET Continuously Adding One to a Text Box

I am trying to create a program that has a button and a text box. Everytime the button is pushed I want it to add one to the text box. I keep getting this error:

Overload resolution failed because no accessible 'Int' accepts this number of arguments

Also I am a huge n00b. Here is where I am at so far, thanks in advance.

Option Strict On

Public Class Form1

  Private Sub btnPlus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlus.Click
    Dim i As Integer = Int.Parse(txtAdd.Text)
    i += 1
    txtAdd.Text = i.ToString()
  End Sub
End Class

Upvotes: 0

Views: 5017

Answers (4)

Russ Cam
Russ Cam

Reputation: 125528

Using the TryParse method will mean that the code does not throw a Format exception if the input cannot be parsed to an integer

Private Sub btnPlus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim i as Integer
    If Integer.TryParse(txtAdd.Text, i) Then
        i += 1
        txtAdd.Text = i.ToString()
    End If

End Sub

Upvotes: 2

foson
foson

Reputation: 10227

Looks like you meant to do: Integer.Parse(txtAdd.Text)

Also, I'd suggest making Integer i a member variable (field) of Form1. That way you wouldn't have to Parse it from string to int.

Public Class Form1

    Dim i As Integer

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        i += 1
        Me.TextBox1.Text = i.ToString()
    End Sub
End Class

Upvotes: 2

VBNight
VBNight

Reputation: 1744

Try Calling Convert.ToInt32(txtAdd.Text)

Dim i As Integer = Convert.ToInt32(txtAdd.Text)

Upvotes: 1

Yes - that Jake.
Yes - that Jake.

Reputation: 17129

Dim i As Integer = Int32.Parse(txtAdd.Text)

or

Dim i As Integer = Integer.Parse(txtAdd.Text)

There's no class called "Int."

Upvotes: 4

Related Questions