将字符串转换为ListBox [英] Convert String To ListBox

查看:89
本文介绍了将字符串转换为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 the ToArray method, you get an array of Chars.
So, try this:

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:

Dim value As String() = System.IO.File.ReadAllLines(Directory)

ListBox1.Items.AddRange(value)


Hope this helps.


这篇关于将字符串转换为ListBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆