Newbe question, incrementing a value

I'm trying to increment a value every time button_1 is clicked. Can someone tell me why the following code doesn't work. Thanks in advance.

Ken

Public Class Form1

Dim number As Integer = 0

Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

number = number + 1

End Sub



Answer this question

Newbe question, incrementing a value

  • john.shu

    Thanks GrandpaB and Shlizar Axis,
    It worked!
    I have one more question on this topic. With over 500 lines of code in the Select case statement and using the Button1_Click event to increment the value, if I add a second event to decrement (Button2_Click ) this will in effect double the size of the program. This isn't very efficient. Is there another way to accomplish this task and does anyone know why my original approach did not work
    Once again everyone, thank you for all your help

  • ddefrain01

    I sorry this is what I have and I can't get it to work.

    Public Class Form1

    Dim number As Integer = 0

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    number = number + 1

    End Sub

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint

    Select Case number

    Case 25

    e.Graphics.FillRectangle(Brushes.Green, 100, 25, 30, 5)

    e.Graphics.FillRectangle(Brushes.Green, 100, 30, 30, 5)


  • timg_msft

    Ken

    This code works for me.

    Public Class Form1

    Dim number As Integer = 0

    Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    number = number + 1

    MessageBox.Show("Number = " & number)

    End Sub

    End Class


  • Lih

    When i tried your 2nd code sample (the one with the Form1_Paint event), i noticed that the application didn't even reach the "Form1_Paint" event, it just kept on counting... Nothing more. So, i walked through the help file and noticed that you had to "draw" something in order to go to the "Form1_Paint" event. That's how i came up with my "Me.Controls.Add(pictureBox1)" sample in order to "force" the application to go into the "Form1_Paint" event. Not a very clean way of working, but it worked in my application though. (^_^ )
    Could you tell us more about the 500 lines of code in the Select Case statement What are you trying to with the Select Case Statement Like GrandpaB said in his post. A Breakpoint is a programmers best friend in solving bugs. (^_^ ) By going through your application, step-by-step, you can verify if the application is actually doing like you want it to...
    Anyway, if you need to copy and paste 500 lines of code in order to have your application do the same thing when decreasing the value of "Number", you need to take out the repeating code and create a seperate Procedure/Function for it. For instance (just a sketch):
    Public Class Form1

    Dim number As Integer = 0

    Sub MyDrawing(ByVal x as integer, y as integer, w as integer, h as integer)

    Dim formGraphics As System.Drawing.Graphics

    formGraphics =
    Me.CreateGraphics()
    formGraphics.FillRectangle(Brushes.Green, x, y, w, h)
    formGraphics.Dispose()

    End Sub

    Sub CheckCase()

    Select Case number

    Case 5

    MyDrawing(0,125,30,5)

    Case 10

    MyDrawing(25,100,40,4)

    Case 15

    MyDrawing(50,75,50,3)

    Case 20

    MyDrawing(75,50,40,4)

    Case 25

    MyDrawing(100,25,30,5)

    End Select

    End Sub

    Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    number = number + 1


    'Remove the "Comment" mark from the next line if you would like to check the value of "Number" with out being spammed with MessageBoxes. (^_^ )

    'Me
    .Text = "The current number is: " & number

    Call CheckCase

    End Sub

    Public Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    number = number - 1


    'Remove the "Comment" mark from the next line if you would like to check the value of "Number" with out being spammed with MessageBoxes. (^_^ )

    'Me
    .Text = "The current number is: " & number

    Call CheckCase

    End Sub

    End
    Class

    What i did with this sample is basically take out the Drawing events and placed them into a seperate procedure called "MyDrawing". That way you can draw a Rectangle by simply calling the procedure and providing it with enough parameters to draw it on a specific location and with the desired measurements.
    Next up is the Select Case. By reducing the code within the Select case to about 1 or 2 lines of code per Case, it would be a lot easier to maintain. But since you want to decrease the value of Number and use the same case again, you can create a seperate Procedure for the case as well. That way, you can use the same case for both buttons while only "defining' it once.
    That should really reduce the amount of code drastically. (^_^ )
    But, if you could tell us more about the code, then maybe we can give you a few more pointers.

  • Cara2006

    Sorry for the delay in responding, I was sidetracked.
    GrandpaB, thanks for the breakpoint tip. I'm sure I'll give that feature quite a workout.
    Shlizar Axis, thanks for that explanation. As far as the Select Case statement code is concerned, I'm trying to do is draw a meter with 25 graduations. Three red, four yellow, and eighteen green. Depending on the "number" value the rectangles will paint. A value of three will paint all three red rectangles (case 3), a value of five (case 5) all three red two yellow. The fowling is a section from case 5.

    Case 5

    Dim formGraphics As System.Drawing.Graphics

    formGraphics = Me.CreateGraphics()

    formGraphics.FillRectangle(Brushes.Yellow, 100, 125, 30, 5)

    formGraphics.FillRectangle(Brushes.Yellow, 100, 130, 30, 5)

    formGraphics.FillRectangle(Brushes.Red, 100, 135, 30, 5)

    formGraphics.FillRectangle(Brushes.Red, 100, 140, 30, 5)

    formGraphics.FillRectangle(Brushes.Red, 100, 145, 30, 5)

    formGraphics.DrawLine(Pens.Yellow, 75, 125, 95, 125)

    Dim t As New System.Drawing.Font("Arial", 8, FontStyle.Bold)

    formGraphics.DrawString("55 Gallons", t, Brushes.Yellow, _

    15, 117)

    outline()

    formGraphics.Dispose()

    As per your suggestion I'm going to try and create a Procedure to simplify this task.

    Thanks again everyone.

    Ken


  • banksdenise

    Ken,

    I do not see any errors in you code as listed. You might try adding this:

    number = number + 1
    MsgBox("Number = " & number.ToString)



  • PAPutzback

    GrandpaB,

    I've been using the MS on line tutorial and a friend is going to loan me the MS VB 6 reference library.

    It's slow going but, with the help of this forum and the above material I think I'll get to a point where I be able to write simple programs.

    Ken


  • Sameer Gadewar

    I had the same result after trying to run the code you mentioned in your post Kennm. After trying out some stuff i got to the following code, it's not very clean, but heck... It works... (^_^ )

    Dim number As Integer = 0

    Dim pictureBox1 As New PictureBox()

    Private Sub Form11_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    AddHandler pictureBox1.Paint, AddressOf pictureBox1_Paint

    pictureBox1.Dock = DockStyle.Fill

    'pictureBox1.BackColor = Color.White

    End Sub

    Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    number = number + 1

    Select Case number

    Case 10

    ' Add the PictureBox control to the Form.

    Me.Controls.Add(pictureBox1)

    End Select

    End Sub

    Private Sub pictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint

    Select Case number

    Case 10

    ' Create pen.

    Dim blackPen As New Pen(Color.Black, 3)

    ' Create rectangle.

    Dim rect As New Rectangle(100, 25, 30, 5)

    ' Draw rectangle to screen.

    e.Graphics.DrawRectangle(blackPen, rect)

    e.Graphics.FillRectangle(Brushes.Green, 100, 25, 30, 5)

    End Select

    End Sub

    Hopefully this helps.


  • Tweek

    Ken,

    Good, now you know that your button is incrementing the variable number, and MessageBox.Show() also works! Did MsgBox() work on your system and does this answer your question



  • rimmei

    Kennm,

    Are you familiar with setting a breakpoint and stepping through your code This is a great way to gain more information about your program and how it works and what is going wrong.

    If you know how to step throught your code try it and report back with more information; it will help us help you pinpoint the problem. If you are unfamilair with stepping through code, use VB Express' Help and search on breakpoint and/or debug.



  • David_Beardsley_CSG_Pro_Svcs

    Hello everyone and thanks for replying.
    Shlizar Axis- I've added your example and I still can't get it to work. Are you suggesting to add the entire Select Case number - End Select section of code to the Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    GrandpaB- tried adding your suggesting to each case statement to no avail.
    I don't understand why the Select Case "number" isn't updating when Button_1 is selected.

  • reO

    Kennm,

    You might try this:

    Case 25
    Dim formGraphics As System.Drawing.Graphics
    formGraphics = Me.CreateGraphics()
    formGraphics.FillRectangle(Brushes.Green, 100, 25, 30, 5)
    formGraphics.FillRectangle(Brushes.Green, 100, 30, 30, 5)
    formGraphics.Dispose()



  • DaveHinATL

    Kennm,

    Neither Shlizar nor I have any idea what is in your 500 lines of code or what you are trying to do, but my guess is that you need to plan and organize your code more efficiently. Perhaps Shlizar's suggestion to create a function will work; perhaps you need to create your own object. There are many different approaches you may take to make your code more efficient and maintainable.

    Your question leads me to believe that you would benefit greatly by purchasing a basic programming text and following the examples step by step. There are also numerous websites that have small examples that can be down loaded. Looking over the code and understanding what the programmer did is also very helpful.

    If the answers we provided have answeres your basic question, please mark the reply as having answered you question. This will help others know that there may be a solution to a similar problem that they are having. Best of luck.



  • FrenchiInLA

    The example GranpaB just posted worked way better then the one i did. (^_^ ) In my example i had to use the Select Case in both the Button1_Click event as well as the _Paint event, otherwise the a rectangle would be drawn on the form immediately.

    But thanks to GrandpaB i tried the following code:

    Public Class Form1

    Dim number As Integer = 0

    Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    number = number + 1


    'Remove the "Comment" mark from the next line if you would like to check the value of "Number" with out being spammed with MessageBoxes. (^_^ )

    'Me
    .Text = "The current number is: " & number

    Select Case number

    Case 25

    'Remove the "Comment" mark from the next line if you would like to verify if you've clicked the Button 25 times. (^_^ )

    'MsgBox(
    "You've just clicked " & number & " times! Here's your reward:")

    Dim formGraphics As System.Drawing.Graphics

    formGraphics =
    Me.CreateGraphics()
    formGraphics.FillRectangle(Brushes.Green, 100, 25, 30, 5)
    formGraphics.FillRectangle(Brushes.Green, 100, 30, 30, 5)
    formGraphics.Dispose()

    End Select

    End Sub

    End
    Class


    This way you don't even need the "_Paint" event. (^_^ )-b

  • Newbe question, incrementing a value