Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Emulating word wrap with textbox array

Status
Not open for further replies.

catalina36

Programmer
Aug 28, 2000
36
US
Does anybody know if it's possible to emulate word wrap with an array of textboxes? Specifically, I just want the current word being typed in a textbox to wrap to the next textbox in the array if the maxlength is exceeded while typing that word. While I could use a single multiline textbox, I would prefer not to.

Any suggestions?
[sig][/sig]
 
If you have an array of 3 textboxes, named Text1 and having indices 1, 2 and 3, you could probably set something up like this:

'30 character max length
Const MAX_LENGTH = 30

Private Sub Text1_Change(Index as Integer)

Dim CurrWord as String

If (Len(Text1(Index).Text) > MAX_LENGTH) then
If (Index < Text1.Count) Then
CurrWord = GetLastWord(Text1(Index).Text)

Text1(Index + 1).Text = CurrWord & &quot; &quot; & Text1(Index + 1).Text
Text1(Index).Text = Left$(Text1(Index).Text, Len(Text1(Index).Text) - Len(CurrWord))
End If
End If

End Sub

'This function gets the last word in a string
'There may be an easier way to implement this function
Public Function GetLastWord(strLine as String) as String

Dim iCharCount as Integer

iCharCount = Len(strLine)
Do Until (Mid$(strLine, iCharCount - 1, 1) = &quot; &quot;)
iCharCount = iCharCount - 1

If(iCharCount = 1) Then
Exit Do
End If

Loop

GetLastWord = Mid$(strLine, iCharCount)

End Function

Whenever you type in a textbox, it will check to see if you have exceeded the maximum length. If you have, it will find the last word typed, put that word in the next textbox (if there is a next textbox) and remove it from the current textbox.
This event will fire recursively, so when you move the word to the next textbox, the Change event for that textbox will fire also, and move the last word to the next textbox if necessary.

Hope this helps.

Steve [sig][/sig]
 
Steve,

This is just what I was looking for.

I appreciate your help,
Jon [sig][/sig]
 
Another approach:

Private Sub Text1_Change(Index As Integer)
Static LastWord As String

LastWord = LastWord & Right$(Text1(Index), 1)
If Right$(LastWord, 1) = &quot; &quot; Then LastWord = &quot;&quot;
If Len(Text1(Index).Text) > 30 Then
Text1(Index).Text = Left$(Text1(Index).Text, (Len(Text1(Index).Text) - (Len(LastWord) + 1)))
Index = Index + 1
LastWord = Left$(LastWord, (Len(LastWord) - 1))
Text1(Index).Text = LastWord
Text1(Index).SetFocus
SendKeys &quot;{END}&quot;
LastWord = &quot;&quot;
End If
End Sub

HTH
Rowland [sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top