954,582 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

VB6: Command Button As 2ABC

Hello. I'm relatively new to Visual Basic 6, but I've been getting around pretty good. But there's one thing I need help with. You know how when your texting someone on your phone and you have to press the same button multiple times to get the letter(s) you want? Well I was wondering how exactly would I go about doing that with a Command Buttons and the Textbox?

I tried doing this:

Private Sub Command1_Click()
	Text1.Text = "2"
	Text1.Text = "A"
	Text1.Text = "B"
	Text1.Text = "C"
	End Sub

But when I click the button, "C" only appears in the Text Box? I want to click the button multiple times, so I would have the options for the above characters, or any characters I choose. Then after one of those characters are entered, I move on to the next button to finish what I am entering, just like texting. BTW, I know I could easily use the keyboard to enter text in this textbox, but I'm currently exploring my options with the textbox. What would this code look like?

Thanks in advance.

BasicGamer
Newbie Poster
5 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

You need to use a counter in the button click and based on the counter value decide what is displayed in the textbox.

debasisdas
Posting Genius
6,872 posts since Feb 2007
Reputation Points: 666
Solved Threads: 434
 

Try the following -

Private Sub Command1_Click()

Static xCount As Integer

Select Case xCount
    Case Is = 0
        Text1.Text = "2"
        xCount = xCount + 1
    
    Case Is = 1
        Text1.Text = "A"
        xCount = xCount + 1
    
    Case Is = 2
        Text1.Text = "B"
        xCount = xCount + 1
    
    Case Is = 3
        Text1.Text = "C"
        xCount = 0
End Select
End Sub
AndreRet
Senior Poster
3,922 posts since Jan 2008
Reputation Points: 334
Solved Threads: 350
 

That code works, but it keeps deleting the entire text, instead of adding to the existing text.

BasicGamer
Newbie Poster
5 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

Then use the following -

Text1.Text = Text1.Text & "2" 'or A, B,C etc.
AndreRet
Senior Poster
3,922 posts since Jan 2008
Reputation Points: 334
Solved Threads: 350
 

Oh yeah, I tried that too, but when I click the button multiple times "2ABC" is eventually displayed as a whole, instead of the button cycling through "2", "A", "B", "C", then back to "2".

I want it to cycle through the characters, but not replacing the entire text. Like if I wanted to spell someone's name, I'll use "JACK" as an example, Command Button 1 would act as 2ABC, and Command Button 2 would act as 5JKL. I would keep Clicking Command Button 2 until "J" appears. Then keep clicking Command Button 1 until "A" appears. Then keep clicking Command Button again until "C". And finally, keep clicking Command Button 2 until K appears. The text box will now have JACK displayed.

The code earlier, replaces the entire text. And Adding Text1.Text & "" after Text1.Text, while eventually add "2ABC" as a whole in the textbox when clicked multiple times.

Thanks for all your help, btw.

BasicGamer
Newbie Poster
5 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

Add a timer to your form and set its enabled property to False, Interval to say 2000 (2 Seconds)

I have modified the code a bit, have not tested. Let me know if it works.

Dim strIn As String

Private Sub Command1_Click()
 
Static xCount As Integer

Timer1.Enabled = True
 
Select Case xCount
    Case Is = 0
        strIn = "2"
        xCount = xCount + 1
 
    Case Is = 1
        strIn = "A"
        xCount = xCount + 1
 
    Case Is = 2
        strIn = "B"
        xCount = xCount + 1
 
    Case Is = 3
        strIn = "C"
        xCount = 0
End Select

If Len(Text1) = 1 Then
    Text1.Text = strIn
        Else
    Text1.Text = Text1.Text & strIn
End If
End Sub

Private Sub Timer1_Timer()

Text1.SetFocus
Text1.SelStart = Len(Text1.Text)
Timer1.Enabled = False
End Sub
AndreRet
Senior Poster
3,922 posts since Jan 2008
Reputation Points: 334
Solved Threads: 350
 

I've found that it still replaces the first letter.:@ Below is something else I've tried, see if it will work for you. The problem is that we have to manipulate the cursor position without focus to the textbox, but with the command button that has focus... Selstart etc would have worked wonders under the textbox's change event.:-/

Add another textbox "Text2" to your form. Make it the same width and height as text1 and place it on top of text1. Set both textboxes Appearnce Property to Flat for enhanced look.

The following code. I'll play around tomorrow to see if I can get a better solution....;)

Dim strIn As String

Private Sub Command1_Click()

Static xCount As Integer

Timer1.Enabled = True
Text2.Visible = True

Select Case xCount
Case Is = 0
strIn = "2"
xCount = xCount + 1

Case Is = 1
strIn = "A"
xCount = xCount + 1

Case Is = 2
strIn = "B"
xCount = xCount + 1

Case Is = 3
strIn = "C"
xCount = 0
End Select

Text2.Text = strIn
End Sub

Private Sub Timer1_Timer()

Text2.Visible = False
Select Case Len(Text1.Text)

Case Is = 0
Text2.Width = Text1.Width - 150
Text2.Left = Text1.Left + 150

Case Is = 1
Text2.Width = Text1.Width - 300
Text2.Left = Text1.Left + 300

Case Is = 2
Text2.Width = Text1.Width - 450
Text2.Left = Text1.Left + 450
End Select

Text1.SetFocus
Text1.Text = Text1.Text & Text2.Text
Text1.SelStart = Len(Text1.Text)
Timer1.Enabled = False
End Sub

AndreRet
Senior Poster
3,922 posts since Jan 2008
Reputation Points: 334
Solved Threads: 350
 

OMG. You are awesome. It works, it works! lol.


I want to thank you for taking time to help me with this. I really appreciate it.

BasicGamer
Newbie Poster
5 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

It was a huge pleasure. Happy coding.:)

AndreRet
Senior Poster
3,922 posts since Jan 2008
Reputation Points: 334
Solved Threads: 350
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You