2 Replies - 5214 Views - Last Post: 26 April 2008 - 03:54 PM Rate Topic: -----

#1 moulino  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 18-April 08

How to display Items only once in a list box?

Post icon  Posted 26 April 2008 - 01:21 PM

Hi people, i need your help once again, im using a list box to retrieve info from my text file and display it on my form when i click on 'Display' button..when clicked the program works correctly but the only problem is that if i click on display again it display the same thing again just below the previous output..im pasting my codes here..i need your advice.. :)

Private Sub cmdDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDisplay.Click
		Dim message As String
		Dim mysr As StreamReader = File.OpenText("C:\product.txt")
		If File.Exists("C:\product.txt") Then
			print_inventory()
		Else
			message = "Either no file has yet been created or "
			message = message & "the file is not where expected."
			MessageBox.Show(message, "File Not Found", MessageBoxButtons.OK)

		End If



	End Sub


	Private Sub print_inventory()
		Dim item As String = ""
		Dim unit_price As Double
		Dim quantity As Double
		Dim supplier As String = ""
		Dim date_received As String = ""
		Dim total_price As Double



		Dim mysr As StreamReader = File.OpenText("C:\product.txt")
		Dim fmtStr As String = "{0, -25} {1, -25} {2, -25} {3, -25}{4, -25}{5, -20}"


		lstStock.Items.Add(String.Format(fmtStr, "Product", "Unit_Price", "Qty", "Date", "Supplier", "Total_Price"))
		Do While mysr.Peek <> -1
			item = mysr.ReadLine
			unit_price = CDbl(mysr.ReadLine)
			quantity = CDbl(mysr.ReadLine)
			date_received = mysr.ReadLine
			supplier = mysr.ReadLine
			total_price = unit_price * quantity

			Dim fmtStr1 As String = "{0, -20} {1, -21} {2, -12}{3, -12}{4,-12}{5, -19}"
			lstStock.Items.Add(String.Format(fmtStr1, item, unit_price, quantity, date_received, supplier, total_price))


		Loop
		MsgBox("Complete! All data is loaded!", MsgBoxStyle.Information, "Completion")
		mysr.Close()
	End Sub 


Is This A Good Question/Topic? 0
  • +

Replies To: How to display Items only once in a list box?

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: How to display Items only once in a list box?

Posted 26 April 2008 - 03:27 PM

Clear the ListBox before you add the new items.

ListBox1.Items.Clear()


Was This Post Helpful? 0
  • +
  • -

#3 moulino  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 18-April 08

Re: How to display Items only once in a list box?

Posted 26 April 2008 - 03:54 PM

thanks a lot..it works
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1