in the BUTTON's CLICK event, declare a STATIC variable, as follows:
Private Sub Command1_Click()
STATIC lngClicks As Long
lngclicks = lngClicks+1
If lngClicks = 1 then
Command1.Caption = "This button has been Clicked "&lngclicks&" time."
Else
Command1.Caption = "This button has been Clicked "&lngclicks&" times."
End Sub
==================
This answer is using VB-6 code, since you didn't specify a different version of the language
This answer came straight from a textbook I have, called "Learn to Program with Visual Basic Examples". I have linked to the Barnes and Noble page in the sources section. This sample is discussed from pages 436-441.
You could also use a Form (module) level variable or Public (Global) level variable to do this, but Microsoft's recommendation is to declare the variable as close to where you need it as possible. If you need to access the Click Counter from other procedures, you will need to declare it differently:
If you only need to access it from other procedures in the same form, you should declare it at the top of the form, in the General/Declarations section as follows:
Private m_intClicks as Integer
(you would then need to change the code that refers to the "intClicks" variable to refer to the one with the "m_" in front).
If you need to access the variable from other forms, you should declare it as a GLOBAL variable in a STANDARD MODULE as follows:
Add the standard module by going to "Project>Add Module" and clicking the "Module" icon and "Open" button. Then you change the variable declaration to the following:
Public g_lngClicks as Long , and change the procedures to reference that variable. the "g_" prefix is a standard notation that denotes variables of PUBLIC (or "GLOBAL") scope that can be seen by any procedure in any module or form.
2007-01-23 04:48:07
·
answer #1
·
answered by Richard H 7
·
0⤊
0⤋
Why bother with creating a custom control? Why not just put some code in the button click event which increments a counter. If you need to keep track over a longer period of time you could store it in a database as well.
2007-01-23 01:53:51
·
answer #2
·
answered by Doug B 3
·
4⤊
0⤋
I agree with Doug also, but if you only want to keep track of the clicks during the lifetime of the app, just use the .Tag property like so :
Private Sub cmdTest_Click()
...'Make sure you initialize to 0 (zero)
...cmdTest.Tag = CInt(cmdTest.Tag) + 1
End Sub
Simple, yet effective. Good luck!
2007-01-23 04:17:59
·
answer #3
·
answered by Special Ed 5
·
0⤊
0⤋
Here is a sample code of what you basically need to do.
Dim a as double 'This should be a global variable
Private sub button_click .......blah blah blah
a += 1
TextBox1.Text = a 'This is to represent the text
'property of whatever control you
'want to use to display the number.
End Sub
2007-01-23 01:56:02
·
answer #4
·
answered by Chip 7
·
1⤊
0⤋
I agree with Doug B, but if you must do this try:
// C#
class ClickCountButton : public Button
{
ClickCountButton()
{
OnClick += new EventHandler(this, countClicks);
}
private void countClicks(object sender, EvenrArgs e)
{
mClicks++;
}
private int mClicks = 0;
public int ButtonClicks
{
get { return mClicks; }
}
}
2007-01-23 01:57:36
·
answer #5
·
answered by Pfo 7
·
0⤊
1⤋