vb.net 如何将列表框项目保存为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24600079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:48:52  来源:igfitidea点击:

How can I save listbox items into a string

vb.netstringvisual-studio-2012listboxitems

提问by user3329318

I want to save all the items I have in my listbox into a string in this kind of format.

我想将列表框中的所有项目保存为这种格式的字符串。

String = listboxitem1,listboxitem2,listboxitem3,listboxitem4,listboxitem5....

字符串 = listboxitem1,listboxitem2,listboxitem3,listboxitem4,listboxitem5....

So then later once I want to pull them back up I can use a breaker and break it up, then load them into listbox1 again. I have a rough idea how to do this but not sure. I was thinking save 1 item in listbox1 at a time then separate them with "," then put it in the string. I have no idea how to put that in code though.

所以稍后一旦我想将它们拉回来,我可以使用断路器将其分解,然后再次将它们加载到 listbox1 中。我有一个粗略的想法如何做到这一点,但不确定。我想一次在 listbox1 中保存 1 个项目,然后用“,”将它们分开,然后将其放入字符串中。我不知道如何将其放入代码中。

SOLUTION!

解决方案!

Found that the solution was I load it into a listbox, then I added this code

发现解决办法是我把它加载到一个列表框中,然后我添加了这个代码

For Each Item As Object In ListBox1.Items
                [StringNameHere!] &= (Item & ",")
Next

Then I load the string by splitting the string between every ","

然后我通过在每个“,”之间拆分字符串来加载字符串

采纳答案by HengChin

I understand you have solved your own question. Just an additional suggestion for you if it is not a must for you to store your data as string. What if the value in your ListBox contains a "," ? It will give you another row since you are spliting it with a "," in the later part.

我知道你已经解决了你自己的问题。如果您不必将数据存储为字符串,那只是给您的额外建议。如果 ListBox 中的值包含 "," 怎么办?它会给你另一行,因为你在后面的部分用“,”分割它。

Try using the below:

尝试使用以下方法:

To Store the value from ListBox:

要存储 ListBox 中的值:

 Dim itemListToStore As New List(Of ListItem)
 For Each item As ListItem In ListBox1.Items
     itemListToStore.Add(item)
 Next

To populate the ListBox with stored value:

要使用存储的值填充 ListBox:

 For Each pullOutItem As ListItem In itemListToStore
     ListBox1.Items.Add(pullOutItem.Text)
 Next

This will overcome the problem of delimiter.

这将克服分隔符的问题。

回答by Suji

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click    
dim itm_count as integer
dim s as string
s=""
itm_count= list1.items.count
For k As Integer = 0 To list1.Items.Count
  s = list1.Items(k).ToString & ","
Next
MsgBox(s) 'it will shows the item separated by comas in message box
end sub