adding item from text box to list box

Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jul 2007
Posts: 58
Reputation: bpacheco1227 is an unknown quantity at this point 
Solved Threads: 0
bpacheco1227 bpacheco1227 is offline Offline
Junior Poster in Training

adding item from text box to list box

 
0
  #1
Nov 25th, 2008
I always have a problem with this. I get an error when trying to build the program "argument exception unhandled". Any help would be appreciated, thanks.

  1. Private Sub addBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addBtn.Click
  2. Dim newLineIndex As Integer = 0
  3.  
  4. If My.Computer.FileSystem.FileExists(path & "stocks.txt") Then
  5. Do Until newLineIndex = -1
  6. lstdisplay.Items.Add(txtinput.Text)
  7.  
  8. Loop
  9. End If
  10. End Sub
Last edited by bpacheco1227; Nov 25th, 2008 at 4:56 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 10
Reputation: markd220 is an unknown quantity at this point 
Solved Threads: 2
markd220 markd220 is offline Offline
Newbie Poster

Re: adding item from text box to list box

 
0
  #2
Nov 25th, 2008
Not really sure what you're trying to do, but to add a string from the textbox to the list it's as simple as:

lstdisplay.Items.Add(txtinput.Text)

Are you trying to add the contents of the text file into a listbox? Also what are you trying to do with the loop?
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 58
Reputation: bpacheco1227 is an unknown quantity at this point 
Solved Threads: 0
bpacheco1227 bpacheco1227 is offline Offline
Junior Poster in Training

Re: adding item from text box to list box

 
0
  #3
Nov 25th, 2008
yes, I'm trying to append contents of the textbox to the end of the listbox. The loop isn't correct I removed it already, I just need to get the contents of the textbox to append to the end of the listbox. Here is the complete code
  1. Public Class Form1
  2. Private path As String = "E:\Visual Basic\"
  3. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  4. lstdisplay.Items.Clear()
  5. My.Computer.FileSystem.OpenTextFileReader(path & "stocks.txt")
  6. lstdisplay.DataSource = IO.File.ReadAllLines(path & "stocks.txt")
  7. End Sub
  8.  
  9. Private Sub exitBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitBtn.Click
  10. Me.Close()
  11. End Sub
  12. 'Private Sub txtinput_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtinput.Enter
  13. ' txtinput.SelectAll()
  14. 'End Sub
  15. Private Sub addBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addBtn.Click
  16. 'Dim newLineIndex As Integer = 0
  17.  
  18. 'If My.Computer.FileSystem.FileExists(path & "stocks.txt") Then
  19. 'Do Until newLineIndex = -1
  20. lstdisplay.Items.Add(txtinput.Text)
  21. txtinput.Focus()
  22. ' Loop
  23. 'End If
  24. End Sub
  25.  
  26.  
  27.  
  28. End Class
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 58
Reputation: bpacheco1227 is an unknown quantity at this point 
Solved Threads: 0
bpacheco1227 bpacheco1227 is offline Offline
Junior Poster in Training

Re: adding item from text box to list box

 
0
  #4
Nov 26th, 2008
any thoughts on this one?
Reply With Quote Quick reply to this message  
Join Date: Dec 2002
Posts: 461
Reputation: waynespangler is on a distinguished road 
Solved Threads: 56
waynespangler waynespangler is offline Offline
Posting Pro in Training

Re: adding item from text box to list box

 
0
  #5
Nov 27th, 2008
Will this help?
  1. Dim zz As String = My.Computer.FileSystem.ReadAllText(path & "stocks.txt")
  2. Dim a As String() = Split(zz, vbNewLine)
  3. ListBox1.Items.AddRange(a)
Wayne

It is hard to understand how a cemetery can raise its burial rates and blame it on the cost of living.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 58
Reputation: bpacheco1227 is an unknown quantity at this point 
Solved Threads: 0
bpacheco1227 bpacheco1227 is offline Offline
Junior Poster in Training

Re: adding item from text box to list box

 
0
  #6
Nov 28th, 2008
no still didn't work, I'm stumped I've tried everything. Cannot add items to lstdisplay (listbox). Maybe because the listbox is being populated by a file?
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 10
Reputation: markd220 is an unknown quantity at this point 
Solved Threads: 2
markd220 markd220 is offline Offline
Newbie Poster

Re: adding item from text box to list box

 
1
  #7
Nov 30th, 2008
The reason the code doesn't work is because adding an item to a listbox using the .items.add is different than setting the datasource. They don't work together.

If you want to continue to use the datasource (which I personally don't recommend) you can try this out. Use a variable for the datasource and append the txtInput to it and reset the datasource of the list.

  1.  
  2. Private path As String = "D:\Mark\"
  3. Dim datasource As New List(Of String)
  4.  
  5.  
  6. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  7. lstFile.Items.Clear()
  8. My.Computer.FileSystem.OpenTextFileReader(path & "ITS_Stuff.txt")
  9.  
  10. Dim a() As String = IO.File.ReadAllLines(path & "ITS_Stuff.txt")
  11. For Each content As String In a
  12. datasource.Add(content)
  13. Next
  14. lstFile.DataSource = datasource
  15. End Sub
  16.  
  17.  
  18. Private Sub exitBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
  19. Me.Close()
  20. End Sub
  21. 'Private Sub txtinput_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtinput.Enter
  22. ' txtinput.SelectAll()
  23. 'End Sub
  24. Private Sub addBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
  25. 'Dim newLineIndex As Integer = 0
  26.  
  27. 'If My.Computer.FileSystem.FileExists(path & "stocks.txt") Then
  28. 'Do Until newLineIndex = -1
  29. datasource.Add(txtInput.Text())
  30. lstFile.DataSource = Nothing
  31. lstFile.DataSource = datasource
  32. lstFile.Refresh()
  33.  
  34. txtinput.Focus()
  35. ' Loop
  36. 'End If
  37. End Sub


A simpler way to do it is just to use the .items.add for the contents of the file and the txtInput

  1.  
  2. Private path As String = "D:\Mark\"
  3.  
  4. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  5. lstFile.Items.Clear()
  6. My.Computer.FileSystem.OpenTextFileReader(path & "ITS_Stuff.txt")
  7.  
  8. Dim a() As String = IO.File.ReadAllLines(path & "ITS_Stuff.txt")
  9. For Each content As String In a
  10. lstFile.Items.Add(content)
  11. Next
  12.  
  13. End Sub
  14.  
  15.  
  16. Private Sub exitBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
  17. Me.Close()
  18. End Sub
  19. 'Private Sub txtinput_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtinput.Enter
  20. ' txtinput.SelectAll()
  21. 'End Sub
  22. Private Sub addBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
  23. 'Dim newLineIndex As Integer = 0
  24.  
  25. 'If My.Computer.FileSystem.FileExists(path & "stocks.txt") Then
  26. 'Do Until newLineIndex = -1
  27. lstFile.Items.Add(txtInput.Text)
  28.  
  29. txtinput.Focus()
  30. ' Loop
  31. 'End If
  32. End Sub
Last edited by markd220; Nov 30th, 2008 at 11:35 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 58
Reputation: bpacheco1227 is an unknown quantity at this point 
Solved Threads: 0
bpacheco1227 bpacheco1227 is offline Offline
Junior Poster in Training

Re: adding item from text box to list box

 
0
  #8
Dec 1st, 2008
thanks, now I can add items in the listbox. I also need to remove items when selected this is the code I have now but, of course it's not working, this is the code that I have so far
  1. Private Sub removeBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles removeBtn.Click
  2. If lstdisplay.SelectedIndex > -1 Then
  3. lstdisplay.Items.RemoveAt(lstdisplay.SelectedIndex)
  4.  
  5. End If
  6. End Sub

would I have to change the lstdisplay.Items.RemoveAt(lstdisplay.SelectedIndex) line to something like lstdisplay.datasource.????
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 10
Reputation: markd220 is an unknown quantity at this point 
Solved Threads: 2
markd220 markd220 is offline Offline
Newbie Poster

Re: adding item from text box to list box

 
0
  #9
Dec 1st, 2008
If you are going to use the datasource approach, you'll have to modify the datasource variable and reset the datasource of the list.

  1.  
  2.  
  3. Private path As String = "c:\"
  4. Dim datasource As New List(Of String)
  5.  
  6. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  7. lstFile.Items.Clear()
  8. Dim a() As String = IO.File.ReadAllLines(path & "dschat.log")
  9.  
  10. 'I prefer to work with collections rather than arrays in .net so I'll move the array to a strongly typed List(Of ) collection
  11. For Each content As String In a
  12. datasource.Add(content)
  13. Next
  14. lstFile.DataSource = datasource
  15. End Sub
  16.  
  17. ''' <summary>
  18. ''' Use this to refresh the list datasource.
  19. ''' </summary>
  20. ''' <remarks></remarks>
  21. Private Sub SetDatasource()
  22. lstFile.DataSource = Nothing
  23. lstFile.DataSource = datasource
  24. lstFile.Refresh()
  25. End Sub
  26.  
  27. Private Sub removeBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles removeBtn.Click
  28.  
  29. If lstFile.SelectedIndex > -1 Then
  30. datasource.RemoveAt(lstFile.SelectedIndex)
  31. SetDatasource()
  32. End If
  33.  
  34. End Sub
  35.  
  36. Private Sub addBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
  37. datasource.Add(txtInput.Text())
  38. SetDatasource()
  39. txtInput.Focus()
  40. End Sub
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 58
Reputation: bpacheco1227 is an unknown quantity at this point 
Solved Threads: 0
bpacheco1227 bpacheco1227 is offline Offline
Junior Poster in Training

Re: adding item from text box to list box

 
0
  #10
Dec 1st, 2008
Yes, thank you everything seems to be working beautifully. I added this to the exit button to write added content back to file.
  1. Private Sub exitBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitBtn.Click
  2. If My.Computer.FileSystem.FileExists(path & "stocks.txt") Then
  3. My.Computer.FileSystem.WriteAllText(path & "stocks.txt", String.Empty, False)
  4. End If
  5. 'loop to enter all data in list box into the file
  6. For Each item As String In lstdisplay.Items
  7. My.Computer.FileSystem.WriteAllText(path & "stocks.txt", item & ControlChars.NewLine, True)
  8. Next
  9. Me.Close()
  10. End Sub
and it actually works !!!

Thanks again!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC