VB6 How do I make 3 single line textboxes act like a split multi-line textbox?

I have a Visual Basic 6 form where there is a need for multi-line textbox functionality but the location of the controls do not allow for a multi-line textbox.  The first textbox is near the right side of the screen.  The second box is near a little lower and near the middle of the screen.  The third text box is on the left side of the screen.  Because the text is in different locations, I don't know of how I could use a multi-line textbox.  How can I get the three textboxes to share text changes.  For example, if textbox 1 has a  maxlength of 10 and the user types in 15 characters, 10 characters should go into textbox1 and the next 5 characters should go into textbox2.  If the user deletes 6 characters while textbox 2 has the focus, textbox2.text should change length to 0 and textbox1.text would change to len(9) and get the focus. .
SteveQ2Asked:
Who is Participating?
 
AbhishekSharmaConnect With a Mentor Commented:
Put three textbox in a form named Text1, Text2, Text3 and try below code

Option Explicit
Dim strTemp As String

Private Sub Form_Load()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""

End Sub

Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
  strTemp = Trim(Text1.Text) & Trim(Text2.Text) & Trim(Text3.Text)
  Text1.Text = ""
  Text2.Text = ""
  Text3.Text = ""
  Text1.Text = Mid(strTemp, 1, 10)
  If Len(strTemp) > 10 Then Text2.Text = Mid(strTemp, 11, 10)
  If Len(strTemp) > 20 Then Text3.Text = Mid(strTemp, 21, 10)
 
End Sub

Private Sub Text2_KeyUp(KeyCode As Integer, Shift As Integer)
  strTemp = Trim(Text1.Text) & Trim(Text2.Text) & Trim(Text3.Text)
  Text1.Text = ""
  Text2.Text = ""
  Text3.Text = ""
  Text1.Text = Mid(strTemp, 1, 10)
  If Len(strTemp) > 10 Then Text2.Text = Mid(strTemp, 11, 10)
  If Len(strTemp) > 20 Then Text3.Text = Mid(strTemp, 21, 10)
 
End Sub
Private Sub Text3_KeyUp(KeyCode As Integer, Shift As Integer)
  strTemp = Trim(Text1.Text) & Trim(Text2.Text) & Trim(Text3.Text)
  Text1.Text = ""
  Text2.Text = ""
  Text3.Text = ""
  Text1.Text = Mid(strTemp, 1, 10)
  If Len(strTemp) > 10 Then Text2.Text = Mid(strTemp, 11, 10)
  If Len(strTemp) > 20 Then Text3.Text = Mid(strTemp, 21, 10)
 
End Sub
0
 
23637269Commented:
There are many ways to take care of this.
Your can use the keypress event or keydown or keyup event of the textbox to track how many chars have been entered.  When you reach the max txtBox2.SetFocus, then txtBox3 etc...


0
 
23637269Connect With a Mentor Commented:
Create a form with text1, text2, text3
Click within the Code Snippet then Control A to select all
GoTo your Form's Source Area And Control V to paste and try it out.

I can convert the textboxes to an array if you want.  
It would make the code cleaner.
I wanted to give you whatever would help you out the best.

Thanks
Roger

P.S. If the ''''''''''' are not aligned in what is displayed here it will be in VB.  
Because the font is not proportional.
Option Explicit
Dim bIgnoreLengthChange As Boolean
Dim lMaxFirstSet As Long
Dim lMaxSecondSet As Long
Dim lMaxThirdSet As Long
Dim lReply As Long
Private Sub Form_Load()
    'Init Text Box Value So We Do Not Need To Worry About Length.
    bIgnoreLengthChange = True
    Text1.Text = ""
    lMaxFirstSet = 10
    
    Text2.Text = ""
    lMaxSecondSet = 10
    
    Text3.Text = ""
    lMaxThirdSet = 10
    
    'Now we need to process any changes to the text box.
    bIgnoreLengthChange = False
End Sub
Private Sub Text1_Change()
    
    If bIgnoreLengthChange Then Exit Sub
    
    'You may want to think about the mode you want when the text is edited.
    'If Insert Mode Is On and everything has a value
    'The last number in the last text box will get erased when digit is inserted.
    
    If Len(Text1) = lMaxFirstSet Then '''''''''''''''''''''''''''''''''''''''
        'At this point the max length was reached so just change focus.     '
        'Need to process any changes.                                       '
        bIgnoreLengthChange = False                                         '
        Text2.SetFocus                                                      '
        Exit Sub                                                            '
    End If ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
                                                                             
    If Len(Text1.Text) > (lMaxFirstSet - 1) Then ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        'This should only happen if the user pastes a number from clipboard.                                      '
        'Or the user edits existing data.                                                                         '
                                                                                                                  '
        'You want to process the change you send.                                                                 '
        'For example if the number is too big the                                                                 '
        'Rules will take care of it.                                                                              '
        bIgnoreLengthChange = False                                                                               '
                                                                                                                  '
        If Len(Text1.Text) = 0 Then ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''    '
            'Second TextBox is Empty all we need to do is paste.                                             '    '
                                                                                                             '    '
            Text2.Text = Mid(Text1.Text, lMaxFirstSet + 1, Len(Text2.Text) - lMaxFirstSet)                   '    '
        Else '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''    '
            'At this point there is data already in second txtBox.                                           '    '
            'Ask user what they want to do.                                                                  '    '
                                                                                                             '    '
            lReply = MsgBox("Click Yes To And I'll Move -->" & Mid(Text1.Text, lMaxFirstSet + 1, Len(Text1.Text)) & "<-- To The Beginning." & vbCrLf & _
            "Of Next Field Giving You " & Mid(Text1.Text, lMaxFirstSet + 1, Len(Text1.Text)) & Trim(Text2.Text) & vbCrLf & vbCrLf & _
            "Or NO to CLEAR and set to -->" & Mid(Text1.Text, lMaxFirstSet + 1, Len(Text1.Text)) & "<--" & vbCrLf & vbCrLf & "Modify 2nd Set", vbYesNo, "How Do You Want Me To Handle This?")
                                                                                                             '    '
                                                                                                             '    '
            If lReply = vbYes Then ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''  '    '
                'Just Insert Text and Follow The Rules You Specify To Handle It.                          '  '    '
                Text2.Text = Mid(Text1.Text, lMaxFirstSet + 1, Len(Text1.Text)) & Text2.Text              '  '    '
                Text1.Text = Mid(Text1.Text, 1, lMaxFirstSet)                                             '  '    '
                DoEvents                                                                                  '  '    '
            Else ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''  '    '
                'Clear and insert the overrun.                                                            '  '    '
                Text2.Text = Mid(Text1.Text, lMaxFirstSet + 1, Len(Text1.Text))                           '  '    '
            End If ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''  '    '
                                                                                                             '    '
                                                                                                             '    '
        End If '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''    '
                                                                                                                  '
        'Now we need to trim any extra numbers...                                                                 '
        Text1.Text = Mid(Text1.Text, 1, lMaxFirstSet)                                                             '
        DoEvents                                                                                                  '
                                                                                                                  '
    End If ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
    
    Select Case KeyAscii ''''''''''''''''''''''''''''''''''''''''''''''''''''
                                                                            '
        Case 48 To 57 ' 0 to 9 ''''''''''''''''''''''''''''''''''''''''''   '
            'If you want to do anything special...                      '   '
                                                                        '   '
            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''   '
                                                                            '
        Case 8 To 10, 13, 27 'Control Characters'''''''''''''''''''''''''   '
            'If you don't want to allow"                                '   '
            'KeyAscii = 0                                               '   '
                                                                        '   '
            'To stop the beep when you press enter.                     '   '
            If KeyAscii = 13 Then '''''''''                             '   '
                'vbEnter                  '                             '   '
                SendKeys (vbTab)          '                             '   '
                KeyAscii = 0              '                             '   '
            End If ''''''''''''''''''''''''                             '   '
            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''   '
                                                                            '
        Case 65 To 90 'Upper Case Letters '''''''''''''''''''''''''''''''   '
            'If You Want To Force To Lower Case Then                    '   '
            'KeyAscii = KeyAscii + 32                                   '   '
                                                                        '   '
            'If you don't want to allow any Upper Case Letters          '   '
            'KeyAscii = 0                                               '   '
                                                                        '   '
            'If you don't want to do anything                           '   '
             KeyAscii = KeyAscii                                        '   '
            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''   '
                                                                            '
        Case 97 To 122 'Lower Case Letters ''''''''''''''''''''''''''''''   '
            'If you Want To Force To Upper Case Then                    '   '
            'KeyAscii = KeyAscii - 32                                   '   '
                                                                        '   '
            'If you don't want to allow Any LowerCase Then              '   '
            'KeyAscii = 0                                               '   '
            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''   '
                                                                            '
        Case Else '''''''''''''''''''''''''''''''''''''''''''''''''''''''   '
            'Discard anything else                                      '   '
            'KeyAscii = 0                                               '   '
            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''   '
                                                                            '
    End Select ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
End Sub
Private Sub Text2_Change()
    
    If bIgnoreLengthChange Then Exit Sub
    
    'You may want to think about the mode you want when the text is edited.
    'If Insert Mode Is On and everything has a value
    'The last number in the last text box will get erased when digit is inserted.
    
    If Len(Text2) = lMaxSecondSet Then '''''''''''''''''''''''''''''''''''''''
        'At this point the max length was reached so just change focus.     '
        'Need to process any changes.                                       '
        bIgnoreLengthChange = False                                         '
        Text3.SetFocus                                                      '
        Exit Sub                                                            '
    End If ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
                                                                             
    If Len(Text2.Text) > (lMaxSecondSet - 1) Then ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        'This should only happen if the user pastes a number from clipboard.                                      '
        'Or the user edits existing data.                                                                         '
                                                                                                                  '
        'You want to process the change you send.                                                                 '
        'For example if the number is too big the                                                                 '
        'Rules will take care of it.                                                                              '
        bIgnoreLengthChange = False                                                                               '
                                                                                                                  '
        If Len(Text3.Text) = 0 Then ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''    '
            'Third TextBox is Empty all we need to do is paste.                                             '    '
                                                                                                             '    '
            Text3.Text = Mid(Text2.Text, lMaxSecondSet + 1, Len(Text2.Text) - lMaxSecondSet)                   '    '
        Else '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''    '
            'At this point there is data already in second txtBox.                                           '    '
            'Ask user what they want to do.                                                                  '    '
                                                                                                             '    '
            lReply = MsgBox("Click Yes To And I'll Move -->" & Mid(Text2.Text, lMaxSecondSet + 1, Len(Text2.Text)) & "<-- To The Beginning." & vbCrLf & _
            "Of Next Field Giving You " & Mid(Text2.Text, lMaxSecondSet + 1, Len(Text2.Text)) & Trim(Text3.Text) & vbCrLf & vbCrLf & _
            "Or NO to CLEAR and set to -->" & Mid(Text2.Text, lMaxSecondSet + 1, Len(Text2.Text)) & "<--" & vbCrLf & vbCrLf & "Modify 3rd Set", vbYesNo, "How Do You Want Me To Handle This?")
                                                                                                             '    '
            If lReply = vbYes Then ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''  '    '
                'Just Insert Text and Follow The Rules You Specify To Handle It.                          '  '    '
                Text3.Text = Mid(Text2.Text, lMaxSecondSet + 1, Len(Text2.Text)) & Text3.Text             '  '    '
            Else ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''  '    '
                'Clear and insert the overrun.                                                            '  '    '
                Text3.Text = Mid(Text2.Text, lMaxSecondSet + 1, Len(Text2.Text))                          '  '    '
            End If ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''  '    '
                                                                                                             '    '
        End If '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''    '
                                                                                                                  '
        'Now we need to trim any extra numbers...                                                                 '
        Text2.Text = Mid(Text2.Text, 1, lMaxSecondSet)                                                            '
        DoEvents                                                                                                  '
                                                                                                                  '
    End If ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 
End Sub
Private Sub Text2_KeyPress(KeyAscii As Integer)
 
    Select Case KeyAscii ''''''''''''''''''''''''''''''''''''''''''''''''''''
                                                                            '
        Case 48 To 57 ' 0 to 9 ''''''''''''''''''''''''''''''''''''''''''   '
            'If you want to do anything special...                      '   '
                                                                        '   '
            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''   '
                                                                            '
        Case 8 To 10, 13, 27 'Control Characters'''''''''''''''''''''''''   '
            'If you don't want to allow"                                '   '
            'KeyAscii = 0                                               '   '
                                                                        '   '
            'To stop the beep when you press enter.                     '   '
            If KeyAscii = 13 Then '''''''''                             '   '
                'vbEnter                  '                             '   '
                SendKeys (vbTab)          '                             '   '
                KeyAscii = 0              '                             '   '
            End If ''''''''''''''''''''''''                             '   '
            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''   '
                                                                            '
        Case 65 To 90 'Upper Case Letters '''''''''''''''''''''''''''''''   '
            'If You Want To Force To Lower Case Then                    '   '
            'KeyAscii = KeyAscii + 32                                   '   '
                                                                        '   '
            'If you don't want to allow any Upper Case Letters          '   '
            'KeyAscii = 0                                               '   '
                                                                        '   '
            'If you don't want to do anything                           '   '
             KeyAscii = KeyAscii                                        '   '
            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''   '
                                                                            '
        Case 97 To 122 'Lower Case Letters ''''''''''''''''''''''''''''''   '
            'If you Want To Force To Upper Case Then                    '   '
            'KeyAscii = KeyAscii - 32                                   '   '
                                                                        '   '
            'If you don't want to allow Any LowerCase Then              '   '
            'KeyAscii = 0                                               '   '
            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''   '
                                                                            '
        Case Else '''''''''''''''''''''''''''''''''''''''''''''''''''''''   '
            'Discard anything else                                      '   '
            'KeyAscii = 0                                               '   '
            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''   '
                                                                            '
    End Select ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
End Sub
Private Sub Text3_Change()
    
    If bIgnoreLengthChange Then Exit Sub
    'You may want to think about the mode you want when the text is edited.
    'If Insert Mode Is On and everything has a value
    'The last number in the last text box will get erased when digit is inserted.
    
    If Len(Text3) > lMaxThirdSet Then '''''''''''''''''''''''''''''''''''''''
        'At this point the max length                                       '
        bIgnoreLengthChange = True                                          '
        Text3.Text = Mid(Text3.Text, 1, lMaxThirdSet)                       '
        bIgnoreLengthChange = False                                         '
        Text1.SetFocus                                                      '
        DoEvents                                                            '
        Exit Sub                                                            '
    End If ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
                                                                             
End Sub
Private Sub Text3_KeyPress(KeyAscii As Integer)
 
    Select Case KeyAscii ''''''''''''''''''''''''''''''''''''''''''''''''''''
                                                                            '
        Case 48 To 57 ' 0 to 9 ''''''''''''''''''''''''''''''''''''''''''   '
            'If you want to do anything special...                      '   '
                                                                        '   '
            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''   '
                                                                            '
        Case 8 To 10, 13, 27 'Control Characters'''''''''''''''''''''''''   '
            'If you don't want to allow"                                '   '
            'KeyAscii = 0                                               '   '
                                                                        '   '
            'To stop the beep when you press enter.                     '   '
            If KeyAscii = 13 Then '''''''''                             '   '
                'vbEnter                  '                             '   '
                SendKeys (vbTab)          '                             '   '
                KeyAscii = 0              '                             '   '
            End If ''''''''''''''''''''''''                             '   '
            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''   '
                                                                            '
        Case 65 To 90 'Upper Case Letters '''''''''''''''''''''''''''''''   '
            'If You Want To Force To Lower Case Then                    '   '
            'KeyAscii = KeyAscii + 32                                   '   '
                                                                        '   '
            'If you don't want to allow any Upper Case Letters          '   '
            'KeyAscii = 0                                               '   '
                                                                        '   '
            'If you don't want to do anything                           '   '
             KeyAscii = KeyAscii                                        '   '
            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''   '
                                                                            '
        Case 97 To 122 'Lower Case Letters ''''''''''''''''''''''''''''''   '
            'If you Want To Force To Upper Case Then                    '   '
            'KeyAscii = KeyAscii - 32                                   '   '
                                                                        '   '
            'If you don't want to allow Any LowerCase Then              '   '
            'KeyAscii = 0                                               '   '
            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''   '
                                                                            '
        Case Else '''''''''''''''''''''''''''''''''''''''''''''''''''''''   '
            'Discard anything else                                      '   '
            'KeyAscii = 0                                               '   '
            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''   '
                                                                            '
    End Select ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
End Sub

Open in new window

0
 
SteveQ2Author Commented:
AbhishekSharma and 23637269.  Thank you very much for sharing your VB expertise!  I was so stuck on the textboxes sharing text.  I'm sorry to have to split the points.  Abishek, your solution was great but every time I type a character the cursor returns to the front of the word.  23637269, your solution was also great.  The scrolling forward works great but when I delete a character in textbox1 or 2, it doesn't scroll the text back from 2 or 3 as text is removed from an earlier box.  I hope you don't think I'm criticizing either solution.  II love them both.  I'm only trying to explain why I couldn't give all of the points to one of you and not the other since both of your solutions are amazing.  I was so so so stuck!  I am so impressed that you both came up with such great solutions in such a short amount of time.  500 points to both of you if I could!  Thank you!  Thank you!  Thank you!
0
 
SteveQ2Author Commented:
AbhishekSharma and 23637269. Thank you very much for sharing your VB expertise! I was so stuck on the textboxes sharing text. I'm sorry to have to split the points to 250 for each of you. Abishek, your solution was great but every time I type a character the cursor returns to the front of the word. 23637269, your solution was also great. The scrolling forward works great but when I delete a character in textbox1 or 2, it doesn't scroll the text back from 2 or 3 as text is removed from an earlier box. I hope you don't think I'm criticizing either solution. II love them both. I'm only trying to explain why I couldn't give all of the points to one of you and not the other since both of your solutions are amazing. I was so so so stuck! I am so impressed that you both came up with such great solutions in such a short amount of time. 500 points to both of you if I could! Thank you!

Hey!  I clicked on Multiple Solutions and assigned 250 points to each of you.  The system then gave me an error message saying permission to multiple solutions was denied.  It then said Amishek had the accepted solution and 23637269 assisted the solution.  That's incorrect.   Both of your solutions were great.  I'm sorry the system only gave you an assisted solution ranking Abishek.  I also accepted your solution but the feedback system changed it to an assisted solution without asking me to confirm.
0
All Courses

From novice to tech pro — start learning today.