将字符串转换为ListBox [英] Convert String To ListBox
问题描述
所以我有这个代码
Dim value As String = My.Computer.FileSystem.ReadAllText(Directory).Skip( 4 )。ToArray()
ListBox1.Items.AddRange(value)
但我得到了这个错误
重载决策失败,因为没有可访问的''添加范围''可以用这些参数调用:< br />
''Public Sub AddRange(items()As Object):''string''类型的值不能转换为''对象'的1维数组'。< br />
''Public Sub AddRange(value As< br />
System.Windows.Forms.ListBox.ObjectCollection)'':类型的值< br />
''string''无法转换to< br />
''System.Windows.Forms.ListBox.ObjectCollection''。
任何想法如何我可以做到这一点请。
i我想要一个文本文件目录来读取行,然后将它们导入列表框请
尝试:
Dim 值为 字符串() =我的.Computer.FileSystem.ReadAllText(目录).Skip( 4 )。ToArray()
FileSystem.ReadAllText
返回一个字符串,然后你跳过一些字符。如果你调用ToArray
方法,你会得到一个Char
的数组。
所以,试试这个:
Dim 值 As 字符串 = 新 字符串(我的。 Computer.FileSystem.ReadAllText(Directory).Skip( 4 )。ToArray())
ListBox1.Items.Add(value)
添加
方法将单个值添加到ListBox
。
sampleme90写道:它只导入到列表框中只有一行,这是第一行我觉得它是readalltext的原因而且我想要像readalllines这样的东西
而不是My.Computer.FileSystem.ReadAllText
方法,你可以使用 System.IO.File.ReadAllLines [ ^ ]方法:
Dim value As String ()= System.IO.File.ReadAllLines(目录)
ListBox1.Items.AddRange(value)
希望这个帮助
So I have this code
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
Hi,
Try:
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 theToArray
method, you get an array ofChar
s.
So, try this:
Dim value As String = New String(My.Computer.FileSystem.ReadAllText(Directory).Skip(4).ToArray()) ListBox1.Items.Add(value)
TheAdd
method adds a single value to theListBox
.
[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 theMy.Computer.FileSystem.ReadAllText
method, you can use the System.IO.File.ReadAllLines[^] method:
Dim value As String() = System.IO.File.ReadAllLines(Directory) ListBox1.Items.AddRange(value)
Hope this helps.
这篇关于将字符串转换为ListBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!