none
Change a specific line from a txt. file RRS feed

  • Question

  • I am trying to change a specific line according to a selected index. However it does not work. This is what I got so far 

    Dim lines() As String = System.IO.File.ReadAllLines("C:\Users\User\source.txt") Dim i As Integer = sourceList.SelectedIndex lines(i) = sourceTextBox.Text System.IO.File.WriteAllLines("C:\Users\User\source.txt", lines)

    Monday, June 18, 2018 11:34 AM

All replies

  • Hi

    What is the relationship between sourceList.SelectedIndex

    and lines()  ? What does sourcelist contain?

    It doesn't seem like a good idea to me.


    Regards Les, Livingston, Scotland

    Monday, June 18, 2018 12:09 PM
  • I am trying to change a specific line according to a selected index. However it does not work.

    You should step the code through line-by-line in the debugger and watch the values of the variables.  In particular, what is i?  Is it within the range of lines in the file?   'does not work' is not an adequate description of the problem.  You need to post the example file, the source list, which item is selected in the list, and the result you expected to see. Generally, if you set out in detail exactly what you expect to happen, it becomes obvious why it isn't happening.

    Monday, June 18, 2018 12:33 PM
  • I would suggest trying the following pattern.

    Public Class Form1
        Private fileName As String = IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "People.txt")
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            If Not IO.File.Exists(fileName) Then
                Button1.Enabled = False
                Exit Sub
            End If
    
            ListBox1.Items.AddRange(IO.File.ReadAllLines(IO.Path.Combine(fileName)))
    
            If ListBox1.Items.Count > 0 Then
                ListBox1.SelectedIndex = 0
            End If
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim lines = ListBox1.Items.Cast(Of String).ToList()
            lines(ListBox1.SelectedIndex) = TextBox1.Text
    
            IO.File.WriteAllLines(fileName, lines.ToArray())
    
            ListBox1.Items.Clear()
            ListBox1.Items.AddRange(lines.ToArray())
    
        End Sub
    
        Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) _
            Handles ListBox1.SelectedIndexChanged
    
            TextBox1.Text = ListBox1.Text
    
        End Sub
    End Class

    One small change to Button1 code

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim index As Integer = ListBox1.SelectedIndex
        Dim lines = ListBox1.Items.Cast(Of String).ToList()
        lines(index) = TextBox1.Text
    
        IO.File.WriteAllLines(fileName, lines.ToArray())
    
        ListBox1.Items.Clear()
        ListBox1.Items.AddRange(lines.ToArray())
        ListBox1.SelectedIndex = index
    End Sub


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Monday, June 18, 2018 12:36 PM
    Moderator
  • Hi beckss,

    According to your description, you want to change a specific line from txt file, but you said that it didn't work, where did you encounter the problem? Was there some error message?

    From your code, I guess that you add one ListBox and Textbox in Form, I do one simple sample that you can take a look:

     Dim thefile As String
        Dim count As Int32
        Dim lines As String()
        Private Sub Frmreadtxt_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            thefile = "D:\Testfile\Test4.txt"
            lines = System.IO.File.ReadAllLines(thefile)
            count = lines.Count
            For i As Integer = 0 To count - 1
                ListBox1.Items.Add(i)
            Next
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim value As Integer = ListBox1.SelectedItem
            lines(value) = TextBox1.Text
            System.IO.File.WriteAllLines(thefile, lines)
        End Sub

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Stanly Fan Thursday, June 21, 2018 6:09 AM
    Tuesday, June 19, 2018 6:29 AM
    Moderator