CheckListBox adding items

  • Thread starter Thread starter Kevin
  • Start date Start date
K

Kevin

I know that to add and check items the following is used
CheckedListBox1.Items.AddRange(New Object() {"One", "Two"})
CheckedListBox1.SetItemChecked(0, True)

What I would like is to add a method to the Items property that would allow
me to add the display value and the value for checked or unchecked. below I
slapped something together which works off the base control but rather have
my method work on the Items property of the custom checkedlistebox. Any
thoughts?

Public Class ksgCheckedListBox
Inherits Windows.Forms.CheckedListBox

Public Sub AddValueBoolean(ByVal DisplayValue As String, ByVal Checked
As Boolean)
Me.Items.Add(DisplayValue)
Me.SetItemChecked(Me.Items.Count - 1, Checked)
End Sub

End Class
 
Kevin said:
I know that to add and check items the following is used
CheckedListBox1.Items.AddRange(New Object() {"One", "Two"})
CheckedListBox1.SetItemChecked(0, True)

What I would like is to add a method to the Items property that would
allow me to add the display value and the value for checked or unchecked.
below I slapped something together which works off the base control but
rather have my method work on the Items property of the custom
checkedlistebox. Any thoughts?

Public Class ksgCheckedListBox
Inherits Windows.Forms.CheckedListBox

Public Sub AddValueBoolean(ByVal DisplayValue As String, ByVal Checked
As Boolean)
Me.Items.Add(DisplayValue)
Me.SetItemChecked(Me.Items.Count - 1, Checked)
End Sub

End Class


My preference if just adding the one method as above, would be to use an
extension method. This is only available in VS 2008 though. The extension
would look like this:

Module Module1

<System.Runtime.CompilerServices.Extension()> _
Public Sub AddValueBoolean(ByVal clb As CheckedListBox, ByVal obj As
Object, _
ByVal Checked As Boolean)

clb.Items.Add(obj)
clb.SetItemChecked(clb.Items.Count - 1, Checked)
End Sub

End Module

Then you can call like this:

CheckedListBox1.AddValueBoolean("hi!", True)

Note that I would also (and did) change the value to an object rather than a
string, to be more generic.
 
Thanks Mike, I actually did that then thought about having it a method of
Items but realized I can not get to "SetItemsChecked" from
what I have below, hence my original question I didn't have the Display
value as object since I only use it for strings yet not a bad
suggested to make it an object as it work both ways for me.

''' <summary>
''' Add display item and set checked property
''' </summary>
''' <param name="Items"></param>
''' <remarks></remarks>
<Runtime.CompilerServices.Extension()> _
Sub ExtendedAdd(ByVal Items As
Windows.Forms.CheckedListBox.ObjectCollection)
....
End Sub
 
Back
Top