Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CheckedListBox or ListView w/CheckBoxes 1

Status
Not open for further replies.

dragonwell

Programmer
Oct 21, 2002
863
US
This is two questions.
1. What is the difference between a CheckedListBox and a ListView in List mode with CheckBoxes set to true?

2. I need to create my own control that is like a CheckedListBox, but each item has a LinkLabel to the right of the text. What would be the best way to go about doing this? At first I though, "Oh - I'll just create a new class that inherits from CheckedListBoxItem and add in the LinkLabel." Unfortunately, there is no CheckListBoxItem class - you just add objects to it's itemcollection and indicate if you want them checked.

Thanks for any help.

dragonwell

[pipe]
Share your knowledge! -
 
I've also used treeviews with checkboxes - just to throw more options out at ya! ;>)

I find the treeview works very nice indeed.

Dale
 
You can write the control from scratch, using a panel on your control to hold the various lines. The Dock property of this panel is Fill, so that it resizes when the control resizes.

Now whenever you want to add an item, write a method or function Add and make it add a new panel to the main panel with Dock property set to Top. Then add a checkbox to this panel with Dock set to Left. Add a LinkLabel to the same panel with Dock set to Fill.

Here's some example code:

Code:
Public Sub Add(ByVal labelText As String, ByVal checked As Boolean)
  Dim P As Panel
  Dim L As LinkLabel
  Dim C As CheckBox

  C = New CheckBox
  C.Checked = checked
  C.Text = ""
  C.Size = New Size(20, 24)
  C.FlatStyle = FlatStyle.Flat
  C.Dock = DockStyle.Left

  L = New LinkLabel
  L.Text = labelText
  L.Size = New Size(24, 24)
  L.Dock = DockStyle.Fill
  AddHandler L.LinkClicked, AddressOf LinkLabelClicked

  P = New Panel
  P.Dock = DockStyle.Top
  P.Size = New Size(24, 24)
  P.Controls.Add(L)
  P.Controls.Add(C)

  pnlMain.Controls.Add(P)
End Sub

Public Event LinkClicked(ByVal sender As Object, ByVal e As LinkLabelLinkClickedEventArgs)

Private Sub LinkLabelClicked(ByVal sender As Object, ByVal e As LinkLabelLinkClickedEventArgs)
  RaiseEvent LinkClicked(sender, e)
End Sub

As you can see, I added a handler to raise an event whenever one of the linklabels is clicked. You may also want to do this for other important events, like checking/unchecking the checkboxes.

You can set any other property if you like when creating the new item. You may want to fiddle around with settings to properly align the controls and such, but this is just to get you started.

Regards, Ruffnekk
---
Is it my imagination or do buffalo wings taste just like chicken?
 
Hey - thanks. Using a panel for each item - hadn't thought of that. And setting thier dock to Top so they just "stack down", neat.
 
I forgot to tell you to set the AutoScroll property of the main panel to true, so that it automatically adds a scrollbar when the number of items are too many to display ;)

Regards, Ruffnekk
---
Is it my imagination or do buffalo wings taste just like chicken?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top