Results 1 to 6 of 6

Thread: change command button values counter

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    3

    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.

  2. #2
    Frenzied Member HanneSThEGreaT's Avatar
    Join Date
    Nov 2003
    Location
    Vereeniging, South Africa
    Posts
    1,492

    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.
    VB.NET MVP 2008 - Present

  3. #3
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: change command button values counter

    Welcome to the VBForums

    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....

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  4. #4
    Frenzied Member HanneSThEGreaT's Avatar
    Join Date
    Nov 2003
    Location
    Vereeniging, South Africa
    Posts
    1,492

    Re: change command button values counter

    Aah, now I see what you mean, duh.. dumb Hannes dumb ! LOL
    VB.NET MVP 2008 - Present

  5. #5
    Frenzied Member HanneSThEGreaT's Avatar
    Join Date
    Nov 2003
    Location
    Vereeniging, South Africa
    Posts
    1,492

    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

    Yes, some tweaking is still required, but the rough framework would be there.
    VB.NET MVP 2008 - Present

  6. #6
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    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
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width