Wrapping text in code

Please support our VB.NET advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2006
Posts: 181
Reputation: PcPro12 is an unknown quantity at this point 
Solved Threads: 6
PcPro12 PcPro12 is offline Offline
Junior Poster

Wrapping text in code

 
0
  #1
Sep 7th, 2009
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.

  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
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,721
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 501
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Wrapping text in code

 
2
  #2
Sep 7th, 2009
Use,
  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
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 181
Reputation: PcPro12 is an unknown quantity at this point 
Solved Threads: 6
PcPro12 PcPro12 is offline Offline
Junior Poster

Re: Wrapping text in code

 
0
  #3
Sep 7th, 2009
Originally Posted by adatapost View Post
Use,
  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.
Reply With Quote Quick reply to this message  
Reply

Tags
vb.netcode, wrapingcode

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the VB.NET Forum


Views: 413 | Replies: 2
Thread Tools Search this Thread



Tag cloud for vb.netcode, wrapingcode
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC