Question Add a couple of splits from a line of textfile into a DataGridView

Socarsky

Well-known member
Joined
Dec 27, 2012
Messages
173
Location
Jakarta/Indonesia
Programming Experience
Beginner
Below line taken from a text file:
stay cool#sakin ol, sakin kal#Don't be angry with me now just stay cool.#1#A

My form writes things in a row that contains 5 specific area that three of them related a language word first, contains a word second one is a meaning of that word and third one is a sentence of the word. So I only want to split two of them which separated "dash" into a DataGridView. I actually can make it by whole line in the text file but I only give a place for two of them, word and meaning. Can I restrict split function with a char for only two times?
 
Here is my method to retrieve two strings from a line of txtfile

Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        Dim getTheString As String
        If (File.Exists(mytxtFile)) Then
            Dim line As String = Nothing
            Dim SR As New StreamReader(mytxtFile)
            line = SR.ReadLine()
            If line Is Nothing Then
                SR.Close()
                Dim writeWordDetails As StreamWriter
                writeWordDetails = AppendText(mytxtFile)
                writeWordDetails.WriteLine(txtWord.Text & "#" & txtMeaning.Text & "#" & txtSentence.Text)
                writeWordDetails.Flush()
                writeWordDetails.Close()
            Else
                Do While line IsNot Nothing
                    If line.StartsWith(txtWord.Text) = True Then
                        getTheString = line
                        Dim strSplit As String() = getTheString.Split(New [Char]() {"#"c}).Take(2).ToArray()    'you cannot find this instance of MSDN of Split function page. Great solution!
                        MessageBox.Show("A match found in the data you just typed." & vbCrLf & "Therefore we cannot add that word.", "Seeking a match", MessageBoxButtons.OK, MessageBoxIcon.Error)
                        txtWord.Text = strSplit(0)
                        txtMeaning.Text = strSplit(1)
                        'txtSentence.Text = strSplit(2)
                    End If
                    line = SR.ReadLine()

                    If line Is Nothing Then
                        SR.Close()
                        Dim writeWordDetails As StreamWriter
                        writeWordDetails = AppendText(mytxtFile)
                        writeWordDetails.WriteLine(txtWord.Text & "#" & txtMeaning.Text & "#" & txtSentence.Text)
                        writeWordDetails.Flush()
                        writeWordDetails.Close()
                        Exit Do
                    End If
                Loop
                SR.Close()
            End If
        Else
            MessageBox.Show("The file is not exist wherever the program seeks")
        End If
    End Sub
 
Back
Top