Search code examples
c#winformsextension-methodscheckedlistbox

Getting error on extension method for CheckedListBox - CS0201


I have an extension method for a CheckedListBox which adjusts the column width depending on content. The following method is placed in a separate ExtensionMethods class:

public static void AdjustCheckedListWidthToContent(this CheckedListBox checkedListBox)
{
    int maxWidth = 0;
    foreach (string text in checkedListBox.Items)
    {
        int width = TextRenderer.MeasureText(text, checkedListBox.Font).Width;
        if (width > maxWidth)
            maxWidth = width;
    }
    checkedListBox.ColumnWidth = maxWidth + 20;
}

This way the code works fine: ExtensionMethods.AdjustCheckedListWidthToContent(checkedListBoxProductList);

But in my preferred way, I'm getting error - CS0201 C# Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement: checkedListBoxProductList.AdjustCheckedListWidthToContent;

I can't get my head around. :(


Solution

  • Add a pair of parentheses before semicolon. It is required even there's no parameter in a function call statememnt.