C# tutorials
19. CheckedListBox with automatic column width

Note: While reading this page, bear in mind that I'm no computer expert and that the text below may be partly inaccurate. If you find errors or have proposals for improvements, please send me a message and help make this a better page for the benefit of future visitors. To the left, there are links to more C# tutorials.


To complete this tutorial, follow these instructions:

1. Open Visual C# 2010 Express.

2. Click on New Project in the File menu.

3. Choose Windows Forms Application if that option isn't already chosen, change the name if you like and click on OK.

4. Point at Toolbox in the left margin and click on the Auto Hide icon at the top of the window (to keep the window to stay open and easier to work with).

5. Drag a CheckedListBox to Form 1.

6. Right-click on checkedListBox1 and click on Properties.

7. Select CheckOnClick in the Properties pane and change the value to True (to make it possible to check a checkbox by clicking on text in the CheckedListBox; you can skip this step if you like).

8. Select Items in the Properties pane and click on the button with three dots to the right of (Collection).

9. Write or paste these Roman numerals to the textbox in the String Collection Editor and click on OK:

i
ii
iii
xi
xii
xiii
xxxi
xxxii
xxxiii

Make sure to keep the linebreaks. If you like, you can write or paste some other text with linebreaks.

10. Select MultiColumn in the Properties pane and change the value to True.

11. Press F5 to start debugging the program. Note that the default column width is used. This width is most likely inappropriate, whether you're using the Roman numerals proposed in step 9 or some other text.

12. Press Shift+F5 to stop debugging the program.

13. Double-click on Form 1. Make sure to click outside checkedListBox1; if you click inside checkedListBox1, you will get code for a SelectedIndexChanged event for checkedListBox1, which is not what you need to complete this tutorial.

14. Write or paste this code where the caret is:

checkedListBox1.ColumnWidth = mkStrWidth(checkedListBox1) + 20;

15. Write or paste this code right above private void Form1_Load(object sender, EventArgs e):

private int mkStrWidth(CheckedListBox clb)
{
  int result = 0;
  using (Font fnt = new Font("Microsoft Sans Serif", 8.25F))
  {
    foreach (string str in clb.Items)
    {
      int width = TextRenderer.MeasureText(str, fnt).Width;
      if (width > result)
      {
      result = width;
      }
    }
  }
  return result;
}

16. Press F5 to start debugging the program. Note that the column width now is appropriate, whether you're using the Roman numerals proposed in step 9 or some other text.

Comment on the first code snippet

Set the column width in checkedListBox1 to the widest item in the checkedListBox1 items collection + 20. + 20 is to account for the width of a checkbox and add some padding between the columns. You can modify this value if you want more or less padding.

Comments on the second code snippet

Line 3: Set the initial value of the int result to 0.

Line 4: Create a new font and set the font to Microsoft Sans Serif and the font-size to 8.25 points, the default settings in the Properties pane. The F after 8.25 makes 8.25 a floating-point number. The F is necessary if you want to use a font-size with decimals. Use a using statement to dispose of the font automatically at the end of the method.

Line 6-13: Check the length of each item in the checkedListBox1 items collection and get the length of the longest item.

Line 8: Get the width of the next item in the checkedListBox1 items collection.

Line 9: Compare the int width with the int result.

Line 11: If the int width is greater than the int result, change the int result to the int width.


Would you like to comment on this page or some other page? Use the contact form. Write the title of the page you want to comment on and your comment in the field Message.