Help With String Application

Thesolis

Member
Joined
Apr 22, 2009
Messages
16
Programming Experience
1-3
Hi, I'm working on a program that reads text entered into a textbox and creates a list of items by separating entries by commas. Everything is working fine, except, because I am using an array to store the items in the list, I have a limited number of entries. This is only a problem because, as it is written currently, each comma counts as an individual entry. Therefore, users can enter ",,," for example, and it would use up 3 places in the array.

I'm looking for a way to check to make sure there is text after the comma before adding an entry.

I've attempted to use the .substring command to check the character after, which works until it gets to the last character. Because there is no character after that, it will give an error.

Here is my code for extra clarity:

VB.NET:
Public Class frmMain

    Dim intCounter As Integer 'Stores the # of commas in the string

    Dim Messages(49) As MessageProperties

    Structure MessageProperties
        Dim strText As String
    End Structure

    Private Sub btnAccept_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAccept.Click

        intCounter = 0

        For i = 0 To UBound(Messages)
            Messages(i).strText = ""
        Next i

        Dim strInput As String

        strInput = txtInput.Text.Trim

        For i = 0 To strInput.Length - 1
            If strInput.Substring(i, 1) <> "," Then
                If strInput.Substring(i, 1) <> " " Then Messages(intCounter).strText &= strInput.Substring(i, 1)
            Else : intCounter += 1
            End If
          
        Next i

        Dim strMessage As String
        Try
            For i = 0 To intCounter
                If Messages(i).strText <> "" Then strMessage &= Messages(i).strText & ControlChars.CrLf Else 
            Next i
        Catch ex As Exception
            MsgBox("Too many entries, maximum is 50")
            Exit Sub
        End Try

        If strMessage <> "" Then MsgBox(strMessage) Else Exit Sub


    End Sub

    'Checks to make sure the user entered in text
    Private Sub txtInput_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtInput.TextChanged
        If txtInput.Text.Trim <> "" Then btnAccept.Enabled = True Else btnAccept.Enabled = False
    End Sub

    'Sub for fade effect
    Private Sub tmrFadeIn_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrFadeIn.Tick
        Me.Opacity += 0.025
        If Me.Opacity = 1 Then tmrFadeIn.Enabled = False
    End Sub

End Class


Thanks for reading.
 
You're turning something that's very simple into something complicated. All you need is a String array and you don't need to specify the size.
VB.NET:
Dim substrings As String() = myTextBox.Text.Split(","c)
That's it, that's all. The array will contain the exact number of entries that were in the original string. If you don't want to include empty values then it's marginally more complex:
VB.NET:
Dim substrings As String() = myTextBox.Text.Split(New Char() {","c}, StringSplitOptions.RemoveEmptyEntries)
 
Thank you for your quick response, I wasn't aware of the split function. Thanks again. I have a question, though, what is the purpose of the c following the "," and the "New Char()"?
VB.NET:
Dim substrings As String() = txtInput.Text.Split({","}, StringSplitOptions.RemoveEmptyEntries)
seems to have the same effect.
 
Last edited:
If you can do that then you must be using VS 2010 and not VS 2008 as your profile says. In VB 2008 you must specify the type of an array unless you're assigning it to a variable as you're declaring it. Only VB 2010 will infer the type of an array that way.

"," => a String literal containing a single Char
","c => a Char literal
 
Back
Top