hello. I have a 1 button namely "Increment" in form 1 and a textbox in form 2

I use this code to increment by and hide the form

   Dim i As Integer = 0
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        form2.TextBox1.Text = CStr(i)
    End Sub

    Private Sub Increment_Click(sender As System.Object, e As System.EventArgs) Handles Increment.Click
        i = i + 1
        form2.TextBox1.Text = CStr(i)
    End Sub`

and i have a button name "Decrement" that when i click it it decrement by 1
Form2.textbox1.text = Val(Form2.textbox1.Text - 1)

my problem is this, after i click the decrement button it decrement but when i click the increment button it increment by 2. i dont know what will i do . help me thanks :D

This is because you are not decreasing the value of i in your decrement function, you are decreasing the textbox entry not i maybe if I illustrate below:

  1. Form Loads, i=0 Textbox1.Text = i i.e. 0
  2. Press Increment, i=i+1 = 0+1 = 1, TextBox1.Text = i = 1
  3. Press Decrement, i=1 (your code does not affect i), Textbox1.text = Textbox1.Text -1 = 0
  4. Press Increment, i=i+1 = 1+1 =2, Textbox1.Text = i = 2

Hi,
G_Waddell is right.
Form2.textbox1.text = Cstr(Val(Form2.textbox1.Text) - 1)
Cheers.

Form2.textbox1.text = Cstr(Val(Form2.textbox1.Text) - 1)
this code is not working its increment the value by 3 whenever i click again the increment button after i performed the decrement

Dim i As Integer = 0
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        form2.TextBox1.Text = CStr(i)
    End Sub
    Private Sub Increment_Click(sender As System.Object, e As System.EventArgs) Handles Increment.Click
        i = i++
        form2.TextBox1.Text = CStr(i)
    End Sub
    Private Sub Decrement_Click(sender As System.Object, e As System.EventArgs) Handles Decrement.Click
        i = i--
        form2.TextBox1.Text = CStr(i)

I believe this is what you are looking for.
Copy and Replace that code on yours.

Hi,

In your decriment function code instead of having:

Form2.textbox1.text = Val(Form2.textbox1.Text - 1)

Change it to

i = i - 1
Form2.textbox1.text = i

Use the same method in the increment_click action and just replace + to -, it incremented by 2 after decremented because you get the value of the textbox not the variable where you stored the result.

This article has been dead for over six months. Start a new discussion instead.