Click here to Skip to main content
15,894,291 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
So I have this code

VB
Dim value As String = My.Computer.FileSystem.ReadAllText(Directory).Skip(4).ToArray()

ListBox1.Items.AddRange(value)


But i get this error

Overload resolution failed because no accessible 'Add Range' can be called with these arguments:<br />
'Public Sub AddRange(items() As Object)": Value of type 'string' cannot be converted to '1-dimensional array of object'.<br />
'Public Sub AddRange(value As<br />
System.Windows.Forms.ListBox.ObjectCollection)': Value of type<br />
'string' cannot be converted to<br />
'System.Windows.Forms.ListBox.ObjectCollection'.


Any ideas how i can do this above please.

i am wanting the directory which is a textfile to read the lines and then import them into the listbox please
Posted
Updated 22-May-13 5:56am
v2

1 solution

Hi,

Try:
VB
Dim value As String() = My.Computer.FileSystem.ReadAllText(Directory).Skip(4).ToArray()


[EDIT]

FileSystem.ReadAllText returns a string, and you skip some chars. If you call the ToArray method, you get an array of Chars.
So, try this:
VB
Dim value As String = New String(My.Computer.FileSystem.ReadAllText(Directory).Skip(4).ToArray())
 
ListBox1.Items.Add(value)

The Add method adds a single value to the ListBox.

[EDIT #2]
sampleme90 wrote:
it only imports in to the listbox only one line and that is the first line i got a feeling it is bacause of the readalltext and i am wanting something like readalllines

Instead of the My.Computer.FileSystem.ReadAllText method, you can use the System.IO.File.ReadAllLines[^] method:
VB
Dim value As String() = System.IO.File.ReadAllLines(Directory)

ListBox1.Items.AddRange(value)

Hope this helps.
 
Share this answer
 
v6
Comments
[no name] 22-May-13 11:57am    
ok now it says

Value of type '1-dimensional array of Char' cannot be converted to '1-dimensional array of string' because 'Char" is not derived from string'

for the

My.Computer.FileSystem.ReadAllText(Directory).Skip(4).ToArray()


any ideas please :) thanks for the help so far
Thomas Daniels 22-May-13 12:08pm    
I updated my answer.
[no name] 22-May-13 12:21pm    
afraid this still doesnt work. It doesn't import all my lines only one line. Any ideas else please
Thomas Daniels 22-May-13 12:29pm    
What do you mean exactly?
[no name] 22-May-13 12:31pm    
it only imports in to the listbox only one line and that is the first line i got a feeling it is bacause of the readalltext and i am wanting something like readalllines :(

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900