Click here to Skip to main content
15,892,643 members
Articles / Programming Languages / C#

CheckListBox Based on ListBox that Supports ReadOnly

Rate me:
Please Sign up or sign in to vote.
3.22/5 (3 votes)
8 Nov 2007CPOL 45.8K   894   23   1
Extended ListBox which supports CheckBox in Normal and ReadOnly mode
Screenshot - CheckListBox_demo.jpg

Introduction

This is based on this article where I extended the code provided by Zhi-Xin Ye in the post to create a CheckListBox that supports ReadOnly unlike CheckedListBox control which can only be disabled.

Using the Code

The demo project contains the code as follows:

C#
private void Form1_Load(object sender, EventArgs e)
{
    //ReadOnly CheckListBox
    checkListBox1.BackColor = Color.FromKnownColor(KnownColor.ControlLight);
    checkListBox1.SelectionMode = SelectionMode.None;
    CheckBox[] boxes = new CheckBox[100];
    for (int i = 0; i < 100; i++)
    {
        CheckBox box = new CheckBox();
        box.Checked = true;
        box.Enabled = false;
        box.Text = "box" + i.ToString();
        boxes[i] = box;
    }
    checkListBox1.AddCheckBoxes(boxes);
    //Normal CheckListBox
    boxes = new CheckBox[100];
    for (int i = 0; i < 100; i++)
    {
        CheckBox box = new CheckBox();
        box.Text = "box" + i.ToString();
        boxes[i] = box;
    }
    checkListBox2.AddCheckBoxes(boxes);
} 

Points of Interest

Like I mentioned, mouse wheel scroll is quite slow and there's flickering when I use scrollbar. This is just a start. Hopefully I will be able to make it better in future.

History

  • 1.0 Created

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralThe real solution Pin
H. Tony28-Jan-08 5:40
H. Tony28-Jan-08 5:40 
Learned from the following that I only need to set the SelectionMode of the CheckedListBox to none, then it behaves very close to ReadOnly. Now I only need to set the background, checked status.

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1361235&SiteID=1[^]

No man is an island, entire of itself
every man is a piece of the continent, a part of the main
if a clod be washed away by the sea,
Europe is the less, as well as if a promontory were,
as well as if a manor of thy friends or of thine own were
any man's death diminishes me, because I am involved in mankind
and therefore never send to know for whom the bell tolls
it tolls for thee.


-- John Donne

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.