Dim str As IO.StreamReader = New IO.StreamReader("F:\记录", System.Text.Encoding.Default)
Dim ts As String() = str.ReadToEnd.Split(Chr(10)) '如果chr(10)不行可以换chr(13)
ListBox1.Items.AddRange(ts)
str.Close()
启动程序时候读取
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
删除指定行内容
Dim sw As System.IO.StreamWriter
sw = New System.IO.StreamWriter("F:\采花\采花地址.txt", False, System.Text.Encoding.Default) 'false 全新保存,true是指以追加的方式保存文件
For i = 0 To ListBox1.Items.Count - 1
sw.WriteLine(ListBox1.Items(i))
Next
sw.Close()
保存ListBox1内容
当我删除指定行内容后,保存 下次再启动程序,ListBox1里面就会出现一个空行
这个空行怎么解决
因为我这个程序点击ListBox1的选项就有事件发生,可是ListBox1出现空行,点击到空行 就会出错
求解决的办法!
------解决方案--------------------
这里是因为换行符的原因,若我没有估错的话,这一个空行应该是listbox中的最后一项为空行。因此,将数据读取修改如下即可:
Dim sr As System.IO.StreamReader = New System.IO.StreamReader("E:\记录.txt", System.Text.Encoding.Default)
While (Not sr.EndOfStream)
ListBox1.Items.Add(sr.ReadLine())
End While