Text WordWrap Function Help

Goatee-Guy
01-24-2008, 11:55 AM
Hey i got this text function i need to fix in need of some help.

Public Function WordWrap(InputWord As TextBox, OutputText As TextBox)
'Wraps
'ok the first thing is i obviously need to get rid of all these dims
'Second connects to first and that is instead of w1 w2 w3 etc for the input lines i need to put
'just one word like TheWord = InputWord & " Red" for all of them
'so when i add more i don't have to renumber them
'Lastly i want to make the sections booleen so if i call wordwrap (text1, text2, true, false, true)
' I would get just sections 1 & 3
Dim Wrapped As String, Original As String, w1 As String, w2 As String, w3 As String
Dim w4 As String, w5 As String, w6 As String, w7 As String, w8 As String, w9 As String
Dim w10 As String, w11 As String, w12 As String, w13 As String, w14 As String, w15 As String
Dim w16 As String, w17 As String, w18 As String, w19 As String, w20 As String
Dim w21 As String, w22 As String, w23 As String, w24 As String, w25 As String
Dim w26 As String ', w27 As String, w28 As String, w29 As String, w30 As String, w31 As String
Dim w32 As String, w33 As String, w34 As String, w35 As String, w36 As String, w37 As String
Dim w38 As String, w39 As String, w40 As String, w41 As String, w42 As String, w43 As String
Dim w44 As String, w45 As String, w46 As String, w47 As String, w48 As String, w49 As String
Dim w50 As String, w51 As String, w52 As String, w53 As String, w54 As String, w55 As String
Dim w56 As String, w57 As String, w58 As String, w59 As String, w60 As String, w61 As String
Dim w62 As String, w63 As String, w64 As String, w65 As String, w66 As String, w67 As String
Dim w68 As String, w69 As String, w70 As String, w71 As String, w72 As String, w73 As String
Dim w74 As String, w75 As String, w76 As String, w77 As String, w78 As String, w79 As String
Dim w80 As String, w81 As String, w82 As String, w83 As String, w84 As String, w85 As String
Dim w86 As String, w87 As String, w88 As String, w89 As String, w90 As String, w91 As String
Dim w92 As String, w93 As String, w94 As String, w95 As String, w96 As String, w97 As String
Dim w98 As String, w99 As String, w100 As String, w101 As String, w102 As String, w103 As String
Original = InputWord

'Section 1
w1 = InputWord & " Red"
w2 = InputWord & " Yellow"
w3 = InputWord & " Purple"
w4 = InputWord & " Orange"
w5 = InputWord & " Blue"
w6 = InputWord & " Tan"
w7 = InputWord & " Teal"
w8 = InputWord & " Black"
w9 = InputWord & " Brown"
w10 = InputWord & " Grey"
w11 = InputWord & " Chrome"
w12 = InputWord & " Gold"

'Section 2
w13 = InputWord & " paper"
w14 = InputWord & " pen"
w15 = InputWord & " pencil"
w16 = InputWord & " marker"
w17 = InputWord & " ball point pen"
w18 = InputWord & " crayon"
w19 = InputWord & " felt tip pen"

'Section 3
w20 = InputWord & " staples"
w21 = InputWord & " ruler"
w22 = InputWord & " paper clips"
w23 = InputWord & " bookmarks"
w24 = InputWord & " clock"
w25 = InputWord & " desk phone"
w26 = InputWord & " answering machine"
'End Of Selections
Wrapped = Original & vbCrLf & w1 & vbCrLf & w2 & vbCrLf & w3 & vbCrLf & w4 & vbCrLf & w5 & vbCrLf & w6 & vbCrLf & w7 & vbCrLf & w8 & vbCrLf & w9 & vbCrLf & w10 & vbCrLf & w11 & vbCrLf & w12 & vbCrLf & w13 & vbCrLf & w14 & vbCrLf & w15 & vbCrLf & w16 & vbCrLf & w17 & vbCrLf & w18 & vbCrLf & w19 & vbCrLf & w20 & vbCrLf & w21 & vbCrLf & w22 & vbCrLf & w23 & vbCrLf & w24 & vbCrLf & w25 & vbCrLf & w26 '& vbCrLf & w27 & vbCrLf & w28 & vbCrLf & w29 & vbCrLf & w30 & vbCrLf & w31 & vbCrLf & w32 & vbCrLf & w33 & vbCrLf & w34 & vbCrLf & w35 & vbCrLf & w36 & vbCrLf & w37 & vbCrLf & w38 & vbCrLf & w39 & vbCrLf & w40 & vbCrLf & w41 & vbCrLf & w42 & vbCrLf & w43 & vbCrLf & w44 & vbCrLf & w45 & vbCrLf & w46 & vbCrLf & w47 & vbCrLf & w48 & vbCrLf & w49 & vbCrLf & w50 & vbCrLf & w51 & vbCrLf & w52 & vbCrLf & w53 & vbCrLf & w54 & vbCrLf & w55 & vbCrLf & w56 & vbCrLf & w57 & vbCrLf & w58 & vbCrLf & w59 & vbCrLf & w60 & vbCrLf _
& w61 & vbCrLf & w62 & vbCrLf & w63 & vbCrLf & w64 & vbCrLf & w65 & vbCrLf & w66 & vbCrLf & w67 & vbCrLf & w68 & vbCrLf & w69 & vbCrLf & w70 & vbCrLf & w71 & vbCrLf & w72 & vbCrLf & w73 & vbCrLf & w74 & vbCrLf & w75 & vbCrLf & w76 & vbCrLf & w77 & vbCrLf & w78 & vbCrLf & w79 & vbCrLf & w80 & vbCrLf & w81 & vbCrLf & w82 & vbCrLf & w83 & vbCrLf & w84 & vbCrLf & w85 & vbCrLf & w86 & vbCrLf & w87 & vbCrLf & w88 & vbCrLf & w89 & vbCrLf & w90 & vbCrLf & w91 & vbCrLf & w92 & vbCrLf & w93 & vbCrLf & w94 & vbCrLf & w95 & vbCrLf & w96 & vbCrLf & w97 & vbCrLf & w98 & vbCrLf & w99 & vbCrLf & w100 & vbCrLf & w101 & vbCrLf & w102 & vbCrLf & w103 & vbCrLf & w104 & vbCrLf & w105 & vbCrLf & w106 & vbCrLf & w107 & vbCrLf & w108 & vbCrLf & w109 & vbCrLf & w110 & vbCrLf & w111 & vbCrLf & w112 & vbCrLf & w113 & vbCrLf & w114 & vbCrLf & w115 & vbCrLf & w116 & vbCrLf & w117 & vbCrLf & w118 & vbCrLf & w119 & vbCrLf & w120
OutputText = Wrapped
End Function

the master
01-24-2008, 12:32 PM
To get rid of all those dims you need an array (http://www.vbexplorer.com/VBExplorer/vararray.asp)

Goatee-Guy
01-24-2008, 12:54 PM
I realize that i guess i need this.. Dim sArray() as String. Ok now how do i implement this
Dim Wrapped As String, Original As String
dim sArray()<--- instead of ---> w1 As String, w2 As String, w3 As String
then
'Section 1
sArray = InputWord & " Red"
sArray = InputWord & " Yellow"
sArray = InputWord & " Purple"
sArray = InputWord & " Orange"
is this right?

the master
01-24-2008, 01:02 PM
You have declared a dynamic array there. Do you know how many elements will be in the array each time? Say you have 4 colors then you should declare it with a 3 (0 to 3 is 4 elements)


dim sArray(3) as string


Remember to declare it as something. If you dont then VB will assume a variant. Its best to declare it as what you want (string for text, integer or long for numbers etc)


When you are putting values into an array you need to specify which element you are putting them in


sArray(0) = inputword & " red" '0 for line 1
sArray(1) = inputword & " yellow" '1 for line 2


If you want you can make the arrays start at 1 instead of 0 by adding "option base 1" (no quotes) to the very top of your code (where you should already have "option explicit")

Goatee-Guy
01-24-2008, 01:12 PM
Yes they are strings. No the number of elements are going to be always different. Like if i got this
sArray = InputWord & " Red"
sArray = InputWord & " Yellow"
sArray = InputWord & " Purple"
sArray = InputWord & " Orange"
i will have to be able to go like this
sArray = InputWord & " Red"
sArray = InputWord & " Yellow"
sArray = InputWord & " New Entry"
sArray = InputWord & " Purple"
sArray = InputWord & " Orange"
i need to avoid numbering them alltogether because i got hundreds and they are all in different sections which i need to put new entires in various places. I can't be bothered renaming 500 or more entries everytime i update it.

Roger_Wgnr
01-24-2008, 01:39 PM
I think I must be missing something in what you are trying to do but I will do this anyway.
Your function that you posted could be simplified like this (also Included the section variables you mentioned)
Public Function WordWrap(InputWord As String, Sec1 as Boolean, Sec2 as Boolean, Sec3 as Boolean) As String
Dim OutputText as String
'Section 1
If Sec1 Then
OutputText = InputWord & " Red" & VbCrLF & _
InputWord & " Yellow" & VbCrLF & _
InputWord & " Purple" & VbCrLF & _
InputWord & " Orange" & VbCrLF & _
InputWord & " Blue" & VbCrLF & _
InputWord & " Tan" & VbCrLF & _
InputWord & " Teal" & VbCrLF & _
InputWord & " Black" & VbCrLF & _
InputWord & " Brown" & VbCrLF & _
InputWord & " Grey" & VbCrLF & _
InputWord & " Chrome" & VbCrLF & _
InputWord & " Gold" & VbCrLF
End If

'Section 2
If Sec2 Then
OutputText = OutputText & _
InputWord & " paper" & VbCrLF & _
InputWord & " pen" & VbCrLF & _
InputWord & " pencil" & VbCrLF & _
InputWord & " marker" & VbCrLF & _
InputWord & " ball point pen" & VbCrLF & _
InputWord & " crayon" & VbCrLF & _
InputWord & " felt tip pen" & VbCrLF
End If

'Section 3
If Sec3 Then
OutputText = OutputText & _
InputWord & " staples" & VbCrLF & _
InputWord & " ruler" & VbCrLF & _
InputWord & " paper clips" & VbCrLF & _
InputWord & " bookmarks" & VbCrLF & _
InputWord & " clock" & VbCrLF & _
InputWord & " desk phone" & VbCrLF & _
InputWord & " answering machine" & VbCrLF

'End of Selections
WordWrap = OutputText

End Function
You would call the function like this
OutputTextBox.Text = WordWrap(InputtextBox.Text, True, False, True)
This keeps the function as the return without modifiying any input variables.

For what your code was doing there is no need for mutipule variables.\

Saw the post you made while I was typing this and modified the Function layout to make it easier to modify

the master
01-24-2008, 01:42 PM
In that case a collection might be better for you. You add things to it and it will automatically number them for you.


dim colData as collection
set colData=new collection

coldata.add "data1"
coldata.add "data2"


Then to read them back you can do a loop

dim x as integer
for x = 1 to coldata.count 'Collections always start at 1
something=coldata.item(x)
next

'Use coldata.item(iten number here) to get an item


Always remember to set the collection to nothing when you are finished though. If you only use it in a sub/function then do this at the end of the function. If you use it in lots of subs/functions then you will most likely want to do this in form_unload


set coldata = nothing

Goatee-Guy
01-24-2008, 02:00 PM
Yes roger thats exactly what im going for. The Master i thank you also you have helped me thank you. Im going to check that out and post back up a little bit later if it worked out ok. Thanks guys

Goatee-Guy
01-24-2008, 04:32 PM
I used what was suggested, but it does not display the text.. Don't know why though, it should..

Public Function WordWrap(InputWord As String, OutputText As String, Sec1 As Boolean, Sec2 As Boolean, Sec3 As Boolean) As String

'Section 1
If Sec1 Then
OutputText = InputWord & " Red" & vbCrLf & _
InputWord & " Yellow" & vbCrLf & _
InputWord & " Purple" & vbCrLf & _
InputWord & " Orange" & vbCrLf & _
InputWord & " Blue" & vbCrLf & _
InputWord & " Tan" & vbCrLf & _
InputWord & " Teal" & vbCrLf & _
InputWord & " Black" & vbCrLf & _
InputWord & " Brown" & vbCrLf & _
InputWord & " Grey" & vbCrLf & _
InputWord & " Chrome" & vbCrLf & _
InputWord & " Gold" & vbCrLf
End If

'Section 2
If Sec2 Then
OutputText = OutputText & _
InputWord & " paper" & vbCrLf & _
InputWord & " pen" & vbCrLf & _
InputWord & " pencil" & vbCrLf & _
InputWord & " marker" & vbCrLf & _
InputWord & " ball point pen" & vbCrLf & _
InputWord & " crayon" & vbCrLf & _
InputWord & " felt tip pen" & vbCrLf
End If

'Section 3
If Sec3 Then
OutputText = OutputText & _
InputWord & " staples" & vbCrLf & _
InputWord & " ruler" & vbCrLf & _
InputWord & " paper clips" & vbCrLf & _
InputWord & " bookmarks" & vbCrLf & _
InputWord & " clock" & vbCrLf & _
InputWord & " desk phone" & vbCrLf & _
InputWord & " answering machine" & vbCrLf
End If
'End of Selections
WordWrap = OutputText

End Function

Roger_Wgnr
01-24-2008, 04:49 PM
First change the Function from
Public Function WordWrap(InputWord As String, OutputText As String, Sec1 As Boolean, Sec2 As Boolean, Sec3 As Boolean) As String
toPublic Function WordWrap(InputWord As String, Sec1 As Boolean, Sec2 As Boolean, Sec3 As Boolean) As Stringthen How are you calling the function? you need to place the following in a button click or some event to call the function.OutputTextBox.Text = WordWrap(InputtextBox.Text, True, False, True)Be sure to change OutputTextBox to the name of the Multiline textbox to display the results in.
And change InputTextBox to the name of your input box.

Lastly make sure you have the multiline property set for your output textbox.

Note also when posting code be sure to use the .. code tags.

Goatee-Guy
01-24-2008, 04:51 PM
Call WordWrap(Text1, Text2, True, False, False)
Thats how i call it & in a command button

Goatee-Guy
01-24-2008, 04:58 PM
ahh i see whats going on..
Public Function WordWrap(InputWord As textbox, OutputText As textbox, Sec1 As Boolean, Sec2 As Boolean, Sec3 As Boolean) As String

Goatee-Guy
01-24-2008, 05:02 PM
got it, it's funny how it's the small typo that sometimes gets ya.
:chuckle:

Goatee-Guy
01-24-2008, 05:38 PM
**** it was working great then i got a too many line continuations error..
:(

Goatee-Guy
01-24-2008, 05:57 PM
Stuck :(

Roger_Wgnr
01-24-2008, 05:58 PM
You can only use so many line continuations (underscores) in a single string function to get around this when you have alot of lines just break it in parts.
If Sec1 Then
OutputText = InputWord & " Red" & vbCrLf & _ 'This is the first part
InputWord & " Yellow" & vbCrLf & _
InputWord & " Purple" & vbCrLf & _
InputWord & " Orange" & vbCrLf & _
InputWord & " Blue" & vbCrLf & _
InputWord & " Tan" & vbCrLf & _
OutputText = OutputText & InputWord & " Teal" & vbCrLf & _ 'This is the second part
InputWord & " Black" & vbCrLf & _
InputWord & " Brown" & vbCrLf & _
InputWord & " Grey" & vbCrLf & _
InputWord & " Chrome" & vbCrLf & _
InputWord & " Gold" & vbCrLf
End If
The other point I was making is that passing the TEXTBOX as a parameter to the function is not a good method to use. A fuction should be designed to accept values and return a value.

As such it is better to pass the Input string from the textbox and then return a string that is assinged to the textbox.
for example if the Input textbox is named TEXT1 and the Output textbox is named TEXT2 then instead of using Call WordWrap(Text1, Text2, True, False, False)
you should use
Text2.Text = WordWrap(Text1.Text, True, False, False)
Then your function declaration line should be Public Function WordWrap(InputWord As String, Sec1 As Boolean, Sec2 As Boolean, Sec3 As Boolean) As Stringinstead ofPublic Function WordWrap(InputWord As textbox, OutputText As textbox, Sec1 As Boolean, Sec2 As Boolean, Sec3 As Boolean) As String

Goatee-Guy
01-24-2008, 06:10 PM
ok i used Text2.Text = WordWrap(Text1.Text, True, False, False)
and it said type mismatch for .text so i chopped the .text so it said text1 then it said type mismatch for true
and this was set correct also
Public Function WordWrap(InputWord As String, Sec1 As Boolean, Sec2 As Boolean, Sec3 As Boolean) As String

Goatee-Guy
01-24-2008, 06:12 PM
What if i just said public sub instead of public function?

Goatee-Guy
01-24-2008, 06:24 PM
This didn't work OutputText = OutputText & "blue " & InputWord & vbCrLf & _
still too many line continuations. I really thought it would but it didn't.

the master
01-25-2008, 02:22 AM
There is a small typo in Roger_Wgnr's code. When starting the second lot you dont have an underscore


InputWord & " Tan" & vbCrLf & _
OutputText = OutputText & InputWord & " Teal" & vbCrLf & _ 'This is the second part


Should be...

InputWord & " Tan" & vbCrLf
OutputText = OutputText & InputWord & " Teal" & vbCrLf & _ 'This is the second part


I cant see why you got a type missmatch error but using a sub will give you the same result. Are you sure you havnt copy/pasted this function anywhere? I cant see any other reason for that error

Goatee-Guy
01-25-2008, 08:33 AM
Hey your a real savior, i fixed the typo it works nicely now. The reason i said make it a sub instead of function is because he said..

"The other point I was making is that passing the TEXTBOX as a parameter to the function is not a good method to use. A fuction should be designed to accept values and return a value.

As such it is better to pass the Input string from the textbox and then return a string that is assinged to the textbox.

Not sure but it seems to work fine. I meant if i changed it to a Sub would that be better because i have always seen Subs with the Text Parameter but probably not functions. How much of a difference does it make?

the master
01-25-2008, 08:38 AM
Passing a textbox to a sub or function makes no difference at all. The point he was trying to make is that you should pass a value in as a parameter and get a value out as a return value. You shouldnt pass the whole control and you shouldnt pass the input and output as parameters

Any function that processes data and returns it should be written in this format

output=myfunction(input)


Subs dont let you return a value which means you have to pass both of them

mysub(input,output)


Both methods will ofcourse work but you *should* use the first method

Goatee-Guy
01-25-2008, 09:10 AM
ok im still getting a type mismatch
Public Function WordWrap(InputWord As String, Sec1 As Boolean, Sec2 As Boolean, Sec3 As Boolean) As String
Dim OutputText As String
but i need to have other functions with the same setup

Public Function WordWrap2(InputWord As String, Sec1 As Boolean, Sec2 As Boolean, Sec3 As Boolean) As String
Dim OutputText As String
i have these 2 i guess since they have the same name Outputtext but they are 2 seperate functions im confused..

the master
01-25-2008, 09:38 AM
Where have you defined outputtext?

Roger_Wgnr
01-25-2008, 09:51 AM
Perhaps you need to zip up your project and post it so we can see what is happening.
I have no problems using the code so it may be with the way you are calling the functions. Post the calling code lines.

By the way just to be sure what version of VB are you using?

Goatee-Guy
01-25-2008, 07:55 PM
VB6, im calling it like this
Text2 = WordWrap(OutputText)

the master
01-26-2008, 02:33 AM
I believe the output text should be on the other side of the function. Between the brackets it where you should put your inputs and where you have text2 you should have your output.

You should also put text2.text. I know that is the default property but when your reading it its easier if you say its .text

wbeard52
01-26-2008, 07:30 PM
You want to use a function as previously mentioned.

Remember, a function returns something to the calling code while a sub does not return anything to a calling sub.

Since you are trying to get the output textbox populated with this code, a function is the way to go :D

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum