| | |
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:
Solved Threads: 0
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.
VB Syntax (Toggle Plain Text)
Private Sub addBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addBtn.Click Dim newLineIndex As Integer = 0 If My.Computer.FileSystem.FileExists(path & "stocks.txt") Then Do Until newLineIndex = -1 lstdisplay.Items.Add(txtinput.Text) Loop End If End Sub
Last edited by bpacheco1227; Nov 25th, 2008 at 4:56 pm.
•
•
Join Date: Jul 2007
Posts: 58
Reputation:
Solved Threads: 0
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
VB Syntax (Toggle Plain Text)
Public Class Form1 Private path As String = "E:\Visual Basic\" Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load lstdisplay.Items.Clear() My.Computer.FileSystem.OpenTextFileReader(path & "stocks.txt") lstdisplay.DataSource = IO.File.ReadAllLines(path & "stocks.txt") End Sub Private Sub exitBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitBtn.Click Me.Close() End Sub 'Private Sub txtinput_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtinput.Enter ' txtinput.SelectAll() 'End Sub Private Sub addBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addBtn.Click 'Dim newLineIndex As Integer = 0 'If My.Computer.FileSystem.FileExists(path & "stocks.txt") Then 'Do Until newLineIndex = -1 lstdisplay.Items.Add(txtinput.Text) txtinput.Focus() ' Loop 'End If End Sub End Class
•
•
Join Date: Dec 2002
Posts: 461
Reputation:
Solved Threads: 56
Will this help?
VB.NET Syntax (Toggle Plain Text)
Dim zz As String = My.Computer.FileSystem.ReadAllText(path & "stocks.txt") Dim a As String() = Split(zz, vbNewLine) 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.
It is hard to understand how a cemetery can raise its burial rates and blame it on the cost of living.
•
•
Join Date: Nov 2008
Posts: 10
Reputation:
Solved Threads: 2
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.
A simpler way to do it is just to use the .items.add for the contents of the file and the txtInput
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.
vb Syntax (Toggle Plain Text)
Private path As String = "D:\Mark\" Dim datasource As New List(Of String) Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load lstFile.Items.Clear() My.Computer.FileSystem.OpenTextFileReader(path & "ITS_Stuff.txt") Dim a() As String = IO.File.ReadAllLines(path & "ITS_Stuff.txt") For Each content As String In a datasource.Add(content) Next lstFile.DataSource = datasource End Sub Private Sub exitBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click Me.Close() End Sub 'Private Sub txtinput_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtinput.Enter ' txtinput.SelectAll() 'End Sub Private Sub addBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click 'Dim newLineIndex As Integer = 0 'If My.Computer.FileSystem.FileExists(path & "stocks.txt") Then 'Do Until newLineIndex = -1 datasource.Add(txtInput.Text()) lstFile.DataSource = Nothing lstFile.DataSource = datasource lstFile.Refresh() txtinput.Focus() ' Loop 'End If End Sub
A simpler way to do it is just to use the .items.add for the contents of the file and the txtInput
vb Syntax (Toggle Plain Text)
Private path As String = "D:\Mark\" Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load lstFile.Items.Clear() My.Computer.FileSystem.OpenTextFileReader(path & "ITS_Stuff.txt") Dim a() As String = IO.File.ReadAllLines(path & "ITS_Stuff.txt") For Each content As String In a lstFile.Items.Add(content) Next End Sub Private Sub exitBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click Me.Close() End Sub 'Private Sub txtinput_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtinput.Enter ' txtinput.SelectAll() 'End Sub Private Sub addBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click 'Dim newLineIndex As Integer = 0 'If My.Computer.FileSystem.FileExists(path & "stocks.txt") Then 'Do Until newLineIndex = -1 lstFile.Items.Add(txtInput.Text) txtinput.Focus() ' Loop 'End If End Sub
Last edited by markd220; Nov 30th, 2008 at 11:35 pm.
•
•
Join Date: Jul 2007
Posts: 58
Reputation:
Solved Threads: 0
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
would I have to change the lstdisplay.Items.RemoveAt(lstdisplay.SelectedIndex) line to something like lstdisplay.datasource.????
VB Syntax (Toggle Plain Text)
Private Sub removeBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles removeBtn.Click If lstdisplay.SelectedIndex > -1 Then lstdisplay.Items.RemoveAt(lstdisplay.SelectedIndex) End If End Sub
would I have to change the lstdisplay.Items.RemoveAt(lstdisplay.SelectedIndex) line to something like lstdisplay.datasource.????
•
•
Join Date: Nov 2008
Posts: 10
Reputation:
Solved Threads: 2
If you are going to use the datasource approach, you'll have to modify the datasource variable and reset the datasource of the list.
vb Syntax (Toggle Plain Text)
Private path As String = "c:\" Dim datasource As New List(Of String) Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load lstFile.Items.Clear() Dim a() As String = IO.File.ReadAllLines(path & "dschat.log") 'I prefer to work with collections rather than arrays in .net so I'll move the array to a strongly typed List(Of ) collection For Each content As String In a datasource.Add(content) Next lstFile.DataSource = datasource End Sub ''' <summary> ''' Use this to refresh the list datasource. ''' </summary> ''' <remarks></remarks> Private Sub SetDatasource() lstFile.DataSource = Nothing lstFile.DataSource = datasource lstFile.Refresh() End Sub Private Sub removeBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles removeBtn.Click If lstFile.SelectedIndex > -1 Then datasource.RemoveAt(lstFile.SelectedIndex) SetDatasource() End If End Sub Private Sub addBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click datasource.Add(txtInput.Text()) SetDatasource() txtInput.Focus() End Sub
•
•
Join Date: Jul 2007
Posts: 58
Reputation:
Solved Threads: 0
Yes, thank you everything seems to be working beautifully. I added this to the exit button to write added content back to file.
and it actually works !!!
Thanks again!
VB Syntax (Toggle Plain Text)
Private Sub exitBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitBtn.Click If My.Computer.FileSystem.FileExists(path & "stocks.txt") Then My.Computer.FileSystem.WriteAllText(path & "stocks.txt", String.Empty, False) End If 'loop to enter all data in list box into the file For Each item As String In lstdisplay.Items My.Computer.FileSystem.WriteAllText(path & "stocks.txt", item & ControlChars.NewLine, True) Next Me.Close() End Sub
Thanks again!
![]() |
Similar Threads
- Adding To a list box from multiple text boxes (Visual Basic 4 / 5 / 6)
- how to show item in list box?? (Visual Basic 4 / 5 / 6)
- selected index (VB.NET)
- Source Code that don't work? (Java)
- Hey check out my prog. It has an error can u find it? (Java)
- Issue related to combobox.... (VB.NET)
- not-a-virusadware (Viruses, Spyware and other Nasties)
- Can anybody please help me with this code? (ASP.NET)
Other Threads in the VB.NET Forum
- Previous Thread: How I can display selected value from combo box in data grid view using VB.Net?
- Next Thread: Program Start Up
| Thread Tools | Search this Thread |
"crystal .net .net2005 30minutes 2008 access add arithmetic array assignment basic binary bing box button buttons c# center code combobox component connectionstring convert cpu data database databasesearch datagrid datagridview design dissertation dissertations dissertationthesis dosconsolevb.net editvb.net excel file-dialog firewall folder google hardcopy image images isnumericfuntioncall listview login math memory mobile ms mssqlbackend mysql navigate net networking opacity output pan peertopeervideostreaming picturebox picturebox1 plugin port print printpreview problemwithinstallation project record reports" reuse save savedialog serial server sorting sql storedprocedure string temp text textbox timer toolbox updown upload useraccounts usercontrol vb vb.net vb.netcode vb.nettoolboxvisualbasic2008sidebar vbnet view vista visual visualbasic visualbasic.net visualstudio web wpf





