943,515 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Marked Solved
  • Views: 842
  • VB.NET RSS
Sep 7th, 2009
0

Wrapping text in code

Expand Post »
Ok, so I'm kinda new to programming, but I'm quick to grasp it. I like programming.

Anyway, I'm trying to write a program that will do this.

If a user types in a name in the first line of a textbox and clicks a button, the name will be wrapped in 2 bb-codes. If the user types in 2 names, 1 name on each line, both names are wrapped in the same bb-code...and so on...I'm using textboxes for ease of copying and pasting full lists of names and making each line in the textbox count.

So far, I got this code.

vb.net Syntax (Toggle Plain Text)
  1. 'declared some strings here
  2. Dim bbp1 As String
  3. Dim bbp2 As String
  4. Dim bbox As String
  5. Dim MyArr()
  6. Dim s
  7.  
  8. 'assigned the string a value
  9. bbp1 = "[player]"
  10. bbp2 = "[/player]"
  11. bbox = txtDis.Text
  12. s = vbCrLf
  13.  
  14. 'MyArr is what is splitting each line is my textbox
  15. MyArr = Split(txtDis.Text, s)
  16.  
  17. 'if then statement. If line 1 is not empty, display the text wrapped in the 2 codes
  18. If MyArr(0) = "" = False Then
  19. txtDis.Text = bbp1 & MyArr(0) & bbp2
  20.  
  21. 'if line 1 and line 2 is not empty, display both lines wrapped in the 2 codes
  22. If MyArr(0) = "" = False And MyArr(1) = "" = False Then
  23. txtDis.Text = bbp1 & MyArr(0) & bbp2 & s & bbp1 & MyArr(1) & bbp2
  24.  
  25. 'if line 1, 2, and 3 is not empty, display all 3 lines wrapped in the 2 codes
  26. If MyArr(0) = "" = False And MyArr(1) = "" = False And MyArr(2) = "" = False Then
  27. txtDis.Text = bbp1 & MyArr(0) & bbp2 & s & bbp1 & MyArr(1) & bbp2 & s & bbp1 & MyArr(2) & bbp2
  28. End If
  29. End If
  30. End If

All is well...Except one thing. This code above is written to accept 3 names and wrap them in code. It works just as I wanted it. EXCEPT, if I write in only ONE name, an error popups up and says IndexOutOfRangeException was unhandled: Index was outside the bounds of the array.

If I write 2 names in the textbox, I get the same error. If I write 3 names in the textbox, the code works fine, the names are wrapped in the code as I want them. If I write in 4 names, the first 3 names are wrapped in code and the last one disappears. But that is how the code should work because I have not written anything for a 4th name and line yet, and just proves that anything that is 3 or higher is taken down to 3 and wrapped. Anything 2 and lower generates an error. Except if I have nothing written in the box, nothing happens. Again, how it's supposed to work.

So my question is, how do I get this to work. Since I'm new, I'm probably using or doing the code the wrong way. I want to be able to type in 1 name and have it wrapped in the codes, or write in 10 names, 1 on each line and have them wrapped in the code.

Any suggestions on the type of code I should use? And should I use IF THEN statements or CASE statements? Or maybe DO WHILE/UNTIL LOOP?

I saw some people used sr.Readline (line) or something like that...is that better to use than the one I'm using?

Thanks
Reputation Points: 13
Solved Threads: 6
Junior Poster
PcPro12 is offline Offline
182 posts
since Sep 2006
Sep 7th, 2009
2

Re: Wrapping text in code

Use,
vb.net Syntax (Toggle Plain Text)
  1. Dim S As String = TextBox1.Text
  2. S = S.Trim()
  3. Dim ar() As String = S.Split(New Char() {vbCr, vbLf}, StringSplitOptions.RemoveEmptyEntries)
  4.  
  5. S = "[B]" & Join(ar, "[/B][B]") & "[/B]"
  6. TextBox2.Text = S
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Sep 7th, 2009
0

Re: Wrapping text in code

Click to Expand / Collapse  Quote originally posted by adatapost ...
Use,
vb.net Syntax (Toggle Plain Text)
  1. Dim S As String = TextBox1.Text
  2. S = S.Trim()
  3. Dim ar() As String = S.Split(New Char() {vbCr, vbLf}, StringSplitOptions.RemoveEmptyEntries)
  4.  
  5. S = "[B]" & Join(ar, "[/B][B]") & "[/B]"
  6. TextBox2.Text = S
Cool that works...thanks a lot...I have a lot to learn
Last edited by PcPro12; Sep 7th, 2009 at 3:25 am.
Reputation Points: 13
Solved Threads: 6
Junior Poster
PcPro12 is offline Offline
182 posts
since Sep 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: update data code help
Next Thread in VB.NET Forum Timeline: sql sumbit command





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC