如何从核对清单框创建一个句子? [英] How to create a sentence from a checkedlist box?
问题描述
我正在尝试使用checkedlistbox来创建包含所选项目的句子。
我希望能够从列表框中选择一个或多个项目并用逗号分隔单词,但我还需要在结尾处留下逗号并替换和,然后使用最后选择的项目进行正确的语法。
所以对于checklistbox项目
香蕉
葡萄
苹果
桃子
我想创建一个只包含所选项目的字符串并生成这个句子结构:
我去商店买了香蕉,葡萄和桃子。
或
我去商店买了苹果。
我试过以下代码
每个项目在CheckedListBox1.CheckedItems
sb.Append(item)
sb.Append(,)
下一页
但我无法弄清楚如何取消最后一个逗号并在最后一项之前插入和。
经过数小时的谷歌搜索,我们将非常感谢任何帮助!
看看例子:
昏暗 s As String = String .Empty
Dim j 作为 整数 = CheckedListBox1.CheckedItems.Count - 1
对于 index 作为 整数 = 0 To j
s& = IIf(index = 0 , 字符串 .Empty,IIf(index = CheckedListBox1.CheckedItems.Count - 1 , AND, ,))& CheckedListBox1.CheckedItems(index).ToString
Next
s = 我去商店买了& s
MsgBox(s)
退货:
仅检查香蕉
我去了 商店和购买的香蕉
' 香蕉和苹果
我去 商店和购买香蕉 AND apples
' 香蕉,苹果和葡萄
我去 商店和购买的香蕉,苹果和葡萄
我发现从逗号分隔的字符串中找到非常有趣的方法CheckListBox.CheckedItems
collection: LINQ Cookbook,Recipe 5:连接选定的字符串一个CheckedListBox(Kit George) [ ^ ]
然后你就可以替换最后出现的逗号了使用 String.LastIndexOf [ ^ ]和 String.Substring [ ^ ] functi on。
实施:
昏暗 ci As String ()= CheckedListBox1.CheckedItems.Cast( Of String )。ToArray()
Dim s 作为 字符串 = 字符串 .Join( ,,ci)
Dim j < span class =code-keyword> As Integer = s.LastIndexOf( < span class =code-string>,)
如果 j> 0 然后 s = s.Substring( 0 ,j)& AND& s.Substring(j + 1 ,s.Length - j - 1 )
MsgBox( s,MsgBoxStyle.OkOnly,s.LastIndexOf( ,))
I am trying to use a checkedlistbox to create a sentence with the selected items.
I would like to be able to select one or more items from the listbox and put commas to separate the words, but I also need to leave a comma out at the end and substitute "and" and then use the last selected item for proper grammar.
So for checklistbox items
bananas
grapes
apples
peaches
I would like to create a string with just the selected items and produce this sentence structure:
"I went to the store and purchased bananas, grapes and peaches."
or
"I went to the store and purchased apples."
I tried the following code
For Each item In CheckedListBox1.CheckedItems
sb.Append(item)
sb.Append(", ")
Next
But I can't figure out how to leave off the last comma and insert "and" before the last item.
After many hours of Google searches, any help would be greatly appreciated!
Have a look at example:
[EDIT]
Dim s As String = String.Empty Dim j As Integer = CheckedListBox1.CheckedItems.Count - 1 For index As Integer = 0 To j s &= IIf(index = 0, String.Empty, IIf(index = CheckedListBox1.CheckedItems.Count - 1, " AND ", ", ")) & CheckedListBox1.CheckedItems(index).ToString Next s = "I went to the store and purchased " & s MsgBox(s)
Returns:
'only bananas have been checked I went to the store and purchased bananas 'bananas and apples I went to the store and purchased bananas AND apples 'bananas, apples and grapes I went to the store and purchased bananas, apples AND grapes
I found very interesting way to return comma separated string fromCheckListBox.CheckedItems
collection: LINQ Cookbook, Recipe 5: Concatenating the selected strings from a CheckedListBox (Kit George)[^]
Then you'll be able to replace last occurance of comma via using String.LastIndexOf[^] and String.Substring[^] function.
Implementation:
Dim ci As String() = CheckedListBox1.CheckedItems.Cast(Of String).ToArray() Dim s As String = String.Join(", ", ci) Dim j As Integer = s.LastIndexOf(",") If j > 0 Then s = s.Substring(0, j) & " AND" & s.Substring(j + 1, s.Length - j - 1) MsgBox(s, MsgBoxStyle.OkOnly, s.LastIndexOf(","))
这篇关于如何从核对清单框创建一个句子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!