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 = 0Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
number = number + 1
End Sub

Newbe question, incrementing a value
john.shu
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.Clicknumber = 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 25e.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.Clicknumber = number + 1
MessageBox.Show(
"Number = " & number) End SubEnd
ClassLih
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
Cara2006
Case
5 Dim formGraphics As System.Drawing.GraphicsformGraphics =
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_PaintpictureBox1.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.Clicknumber = 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 SubHopefully 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
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
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