6 Replies - 25913 Views - Last Post: 10 November 2007 - 05:49 PM Rate Topic: -----

#1 rockstar4cs  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 16-April 07

Button click

Post icon  Posted 12 October 2007 - 11:18 PM

I made a button named Bntcalculate and I want to count how many times that button is clicked and then out put that to a message box. How would I measure every time the button is clicked? If you could just point me to a tutorial or even tell me what I need it seems like such an easy method but I'm at loss any help I'd be greatful for thank you.
Is This A Good Question/Topic? 0
  • +

Replies To: Button click

#2 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: Button click

Posted 13 October 2007 - 12:14 AM

Declare a variable which has scope within your form and not just one of the procedures.
Was This Post Helpful? 1
  • +
  • -

#3 rockstar4cs  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 16-April 07

Re: Button click

Posted 13 October 2007 - 09:50 AM

I'm basicly brand new at VB I just got done learning java programing could I just loop btncalculate to count how many times its clicked?
Was This Post Helpful? 0
  • +
  • -

#4 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: Button click

Posted 13 October 2007 - 10:05 AM

Think about variable scope and lifetime (I don't know java but Im sure this applies there as well). If you declare a variable in a function, calling it twice is considered as two independent calls. Your program would use a function as if it were used for the first time. Use variables which are global as far as your form is concerned. To do this declare your counter variable in the beginning and increment it when the button is clicked like this:
Public Class Form1
	Dim counter As Integer   'Variables declared at the beginning remain global within your form. To
												 'allow the variables to be accessible by all forms, replace Dim by Public

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		counter += 1
	End Sub
End Class


This post has been edited by Louisda16th: 13 October 2007 - 10:07 AM

Was This Post Helpful? 0
  • +
  • -

#5 rockstar4cs  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 16-April 07

Re: Button click

Posted 13 October 2007 - 04:01 PM

That worked thanks but I made 2 radio buttons named bill and chris and I only want the counter to count whatever button is selected. So if radio chris is selected and the button is clicked twice and the radio button bill is selected and clicked 3 times the counter will only reconize to count whatever button is selected at the time. I dont need to make 2 counters do I? Heres what I made but its not working.

If radchris.Checked = True Then
			counter += 1
		End If
		If radbill.Checked = True Then
			counter += 1
		End If



I then make the counter display in a message box
  MessageBox.Show("chris entered" & counter & "temperatures(s)" & ControlChars.CrLf & _
		"bill entered" & counter & "temperatures(s)" & ControlChars.CrLf & _
		"The average temperature is" & lbltemp.Text)



But when it displays in the message box it gives the same number of clicks for both it doesnt read the radio buttons.
Was This Post Helpful? 0
  • +
  • -

#6 aceofspades686  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 6
  • View blog
  • Posts: 334
  • Joined: 08-October 07

Re: Button click

Posted 13 October 2007 - 09:45 PM

In this case you would need to declare two variables, one for Chris and one for Bill. Increment those accordingly.

You could alternatively create an array holding two counters instead, but this is one case where I'm going to recommend the K.I.S.S. method unless you're program is going to extend way beyond this.
Was This Post Helpful? 0
  • +
  • -

#7 ferrari12508  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3
  • View blog
  • Posts: 1,115
  • Joined: 02-November 07

Re: Button click

Posted 10 November 2007 - 05:49 PM

use a global accumulator
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1