如何将checkedlistbox中的选中项目添加到combobox [英] How to adding checked item from checkedlistbox to combobox
问题描述
我想将checkedlistbox中的选中项目添加到我的组合框,但我有一个小问题。 Combobox仅显示上次选中的1项。
这是我的示例代码。
如果CheckedListBox1.CheckedItems.Count& 0 Then
For i As Integer = 0到CheckedListBox1.CheckedItems.Count - 1
cbCheckedItem.Text = CheckedListBox1.CheckedItems(i).ToString
Next i
结束如果
任何人都可以帮助我显示所有选中的项目?
谢谢您的帮助...
奇怪的是,CheckedListBox有一个CheckedItems属性,它是一个集合。因此,你可以循环通过它像任何其他集合,使用For或For Each循环。
然后,每个项目需要添加到Items集合
Public Class frmCheckedListBox
Private Sub frmCheckedListBox_Load(ByVal sender As System.Object,ByVal e As System.EventArgs)句柄MyBase.Load
Me.CheckedListBox1.Items.Clear()
Me.CheckedListBox1。
Me.CheckedListBox1.Items.Add(One)
Me.CheckedListBox1.Items.Add(Two)
Me.CheckedListBox1.Items.Add(Three )
Me.CheckedListBox1.Items.Add(Four)
Me.CheckedListBox1.Items.Add(Five)
Me.CheckedListBox1.EndUpdate()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)句柄Button1.Click
每个项目作为字符串在Me.CheckedListBox1.CheckedItems
Me .ComboBox1.Items.Add(Item)
Me.ComboBox1.SelectedIndex = 0
Next
End Sub
结束类
如示例代码所示,CheckedItems集合包含被检查的项目,正如名称所暗示的。它不包含每个项目的布尔值,以指示它是否被选中。如果一个项目被选中,那么该项目在CheckedItems中,如果它不是,那么它不是。你只需要循环遍历集合,并获取其中的每个项目,因为它包含所有被检查的项目,没有不是。
可以:
Me.Combobox1.items.clear()
因为当它用示例代码点击时,会有一个点击然后在下一次点击将返回上一个点击,然后新的一个所有编译在组合框选择菜单
也许我的答案可以帮助你解决你的问题
I want to adding checked item from checkedlistbox to my combobox, but i have a little problem here. Combobox only show 1 item last checked. This is my sample code.
If CheckedListBox1.CheckedItems.Count <> 0 Then
For i As Integer = 0 To CheckedListBox1.CheckedItems.Count - 1
cbCheckedItem.Text = CheckedListBox1.CheckedItems(i).ToString
Next i
End If
anyone can help me show all checked item?? thank's for your help...
Oddly enough the CheckedListBox has a CheckedItems property, which is a collection. As such you can loop through it like you can any other collection, using a For or For Each loop.
then, Each item needs to be added to the Items collection of the ComboBox.
like this sample:
Public Class frmCheckedListBox
Private Sub frmCheckedListBox_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.CheckedListBox1.Items.Clear()
Me.CheckedListBox1.BeginUpdate()
Me.CheckedListBox1.Items.Add("One")
Me.CheckedListBox1.Items.Add("Two")
Me.CheckedListBox1.Items.Add("Three")
Me.CheckedListBox1.Items.Add("Four")
Me.CheckedListBox1.Items.Add("Five")
Me.CheckedListBox1.EndUpdate()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For Each Item As String In Me.CheckedListBox1.CheckedItems
Me.ComboBox1.Items.Add(Item)
Me.ComboBox1.SelectedIndex = 0
Next
End Sub
End Class
As sample code shows, the CheckedItems collection contains the items that are checked, just as the name suggests. It doesn't contain a Boolean value for each an every item to indicate whether it is checked or not. If an item is checked then that item is in the CheckedItems, and if it isn't then it isn't. You simply need to loop through the collection and get every item in it, because it contains all the items that are checked and none that aren't.
in the end you can put :
Me.Combobox1.items.clear()
because when it would click with sample code it would have the one that clicked then on the next click would return the previous one it had clicked and then the new one all compiled in the combobox selection menu
perhaps my answer can help you solve your problems
这篇关于如何将checkedlistbox中的选中项目添加到combobox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!