|
-
Oct 11th, 2012, 02:54 PM
#1
Thread Starter
New Member
Loading items to a listbox from a text files results in duplicates on every Form load
Hello everyone. I'm new to this world and 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:
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
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.
Thanks i hope you understood my problem
-
Oct 12th, 2012, 01:08 PM
#2
Re: Loading items to a listbox from a text files results in duplicates on every Form
Thread moved from the 'CodeBank VB.Net' forum (which is for you to post working code examples, not questions) to the 'VB.Net' forum
-
Oct 12th, 2012, 01:50 PM
#3
Re: Loading items to a listbox from a text files results in duplicates on every Form
Well there's your problem ....
Using SW As New IO.StreamWriter(address, True)
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|