Hello everyone. I'm new to this worldand i have a "little" problem that i encountered yesterday and i'm stuck here.
I have a project where i have two listboxes and i want to populate them from two different text files on Form Load and to save the items from the listboxes to the same text files on Form Closing.
I've written some code but my main problem is the fact that the listboxes are saved on Form Closing and populated on Form Load but on every Form Closing and Form Load the listboxes are duplicating the items. For example:
Form Load
ListBox1.Items = "First Item", "Second Item", "Third Item"
ListBox2.Items = "Item one", "Item two", "Item three"
Form Closing
and then i have:
Form Load
ListBox1.Items = "First Item", "Second Item", "Third Item", "First Item", "Second Item", "Third Item"
ListBox2.Items = "Item one", "Item two", "Item three", "Item one", "Item two", "Item three",
Form Closing
And this is goes like that for every Load and Close of the Form. The code is down here:
With this code i'm trying to populate the listboxes from the specific text files.
Code:Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim currPath As String = Environment.CurrentDirectory() Dim Items1 As String = currPath + "\programs.dat" Dim Items2 As String = currPath + "\address.dat" Try R = New IO.StreamReader(Items2) While (R.Peek() > -1) ListBox1.Items.Add(R.ReadLine) End While R.Close() R = New IO.StreamReader(Items1) While (R.Peek() > -1) ListBox2.Items.Add(R.ReadLine) End While R.Close() Catch ex As Exception End Try End Sub
With this code i'm trying to save the items from the text files to the listboxes:
The conclusion is that on every load the listboxes are populated again with items from the text and everything is duplicated on every close and load of the form.Code:Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing Dim currPath As String = Environment.CurrentDirectory() Dim Items1 As String = currPath + "\programs.dat" Dim Items2 As String = currPath + "\address.dat" Using SW As New IO.StreamWriter(address, True) For Each itm As String In Me.ListBox1.Items SW.WriteLine(itm) Next End Using Using SW As New IO.StreamWriter(programs, True) For Each itm As String In Me.ListBox2.Items SW.WriteLine(itm) Next End Using End Sub
Thanks i hope you understood my problem![]()


and i have a "little" problem that i encountered yesterday and i'm stuck here.
Reply With Quote
