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:
Thanks for reading.
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.