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.

'declared some strings here
        Dim bbp1 As String
        Dim bbp2 As String
        Dim bbox As String
        Dim MyArr()
        Dim s
        
        'assigned the string a value
        bbp1 = "[player]"
        bbp2 = "[/player]"
        bbox = txtDis.Text
        s = vbCrLf

        'MyArr is what is splitting each line is my textbox
        MyArr = Split(txtDis.Text, s)

        'if then statement. If line 1 is not empty, display the text wrapped in the 2 codes
        If MyArr(0) = "" = False Then
            txtDis.Text = bbp1 & MyArr(0) & bbp2

            'if line 1 and line 2 is not empty, display both lines wrapped in the 2 codes
            If MyArr(0) = "" = False And MyArr(1) = "" = False Then
                txtDis.Text = bbp1 & MyArr(0) & bbp2 & s & bbp1 & MyArr(1) & bbp2

                'if line 1, 2, and 3 is not empty, display all 3 lines wrapped in the 2 codes
                If MyArr(0) = "" = False And MyArr(1) = "" = False And MyArr(2) = "" = False Then
                    txtDis.Text = bbp1 & MyArr(0) & bbp2 & s & bbp1 & MyArr(1) & bbp2 & s & bbp1 & MyArr(2) & bbp2
                End If
            End If
        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

Use,

Dim S As String = TextBox1.Text
        S = S.Trim()
        Dim ar() As String = S.Split(New Char() {vbCr, vbLf}, StringSplitOptions.RemoveEmptyEntries)

        S = "[B]" & Join(ar, "[/B][B]") & "[/B]"
        TextBox2.Text = S
commented: thx. didnt know that +1
commented: helped me with my code +3

Use,

Dim S As String = TextBox1.Text
        S = S.Trim()
        Dim ar() As String = S.Split(New Char() {vbCr, vbLf}, StringSplitOptions.RemoveEmptyEntries)

        S = "[B]" & Join(ar, "[/B][B]") & "[/B]"
        TextBox2.Text = S

Cool :D that works...thanks a lot...I have a lot to learn :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.