change command button values counter
Hi, This is my first thread here.
I'm sitting in college writing a program for a virtual mobile phone, just come across the need to change the value of a command button based on how many times I press it. AKA: The text message system.
pressing 2 twice = B pressing it once = A.
Looking forward for any advice. Will just use my 'counter' deal just now.
Re: change command button values counter
Make use of a Static variable, then increment it. If it's value is 1, print A, if its value is 2 then print B.
Re: change command button values counter
Welcome to the VBForums :wave:
It took some time to find a solution (because, I am not an expert :)).
I used 2 Commandbuttons - Command1 for "ABC" and Command2 for "DEF".
And a timer control Timer1. And a textbox Text1 for displaying the text.
Code:
Option Explicit
Dim intCounter As Integer
Dim counter As Integer
Private Sub Form_Load()
Command1.Caption = "ABC"
Command2.Caption = "DEF"
intCounter = -1
Timer1.Interval = 300
End Sub
Private Sub Timer1_Timer()
If counter < 3 Then
counter = counter + 1
Else
intCounter = -1
Timer1.Enabled = False
End If
End Sub
Private Sub Command1_Click() '~~~> For "ABC"
If intCounter < 65 Or intCounter > 67 Then '~~> Change these ASCII values on each button
intCounter = 65 '~~> Change these ASCII values on each button
Text1.Text = Text1.Text & Chr(intCounter)
Else
intCounter = intCounter + 1
If intCounter > 67 Then '~~> Change these ASCII values on each button
intCounter = 65 '~~> Change these ASCII values on each button
Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1) & Chr(intCounter)
Else
Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1) & Chr(intCounter)
End If
End If
If Timer1.Enabled = False Then
Timer1.Enabled = True
End If
counter = 0
End Sub
Private Sub Command2_Click() '~~~> For "DEF"
If intCounter < 68 Or intCounter > 70 Then '~~> Change these ASCII values on each button
intCounter = 68 '~~> Change these ASCII values on each button
Text1.Text = Text1.Text & Chr(intCounter)
Else
intCounter = intCounter + 1
If intCounter > 70 Then '~~> Change these ASCII values on each button
intCounter = 68 '~~> Change these ASCII values on each button
Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1) & Chr(intCounter)
Else
Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1) & Chr(intCounter)
End If
End If
If Timer1.Enabled = False Then
Timer1.Enabled = True
End If
counter = 0
End Sub
Play with the above code.... It's not a perfect solution, but I think it will give you some idea.... :wave:
Re: change command button values counter
Aah, now I see what you mean, duh.. dumb Hannes dumb ! LOL
Re: change command button values counter
Wait a minute. I was on the right track :)
Look here :
Code:
Private Sub Command1_Click()
Static iClicked As Integer
Dim sbut As String
iClicked = iClicked + 1
Select Case iClicked
Case 1
sbut = "A"
Case 2
sbut = "B"
Case 3
sbut = "C"
Case Is > 3
iClicked = 0
End Select
Text1.Text = sbut
End Sub
If you were to use the same logic here for each button, it would work :D
Yes, some tweaking is still required, but the rough framework would be there.
Re: change command button values counter
HanneSThEGreaT Code modifed.
Code:
Dim iClicked As Integer
Private Sub Command1_Click()
iClicked = iClicked + 1
Select Case iClicked
Case 1
Text1.Text = "A"
Case 2
Text1.Text = "B"
iClicked = 0
Case Else
iClicked = 0
End Select
End Sub