How to set new text in CheckListBox after first line

29 Views Asked by At
private void ZzCleaner_Load(object sender, EventArgs e) 
    {

        Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 25, 25));
        richTextBox1_Load();
        if (!string.IsNullOrEmpty(Properties.Settings.Default.FoldersToClear))  
        {
            Properties.Settings.Default.FoldersToClear.Split(',') 
                .ToList()
                .ForEach(item =>
                {

                    bool checkbox = false;
                    if (!FoldersToClear.Items.Contains(item))
                    {
                        FoldersToClear.Items.Add(item); //So here I add it and in here I also need to add path to that specific file. Path that I will be using is below "folderPath".
                        //richTextBox2.SelectionFont = new Font("Arial", 8.00f, FontStyle.Italic);
                        //richTextBox2.AppendText(folderPath + item + Environment.NewLine);
                    }

                    if (Properties.Settings.Default.CheckedFolders.Contains(item))
                    {
                        checkbox = true;
                        folderOrganiser = new FolderOrganiser(item, richTextBox1);
                    }
                    var index = this.FoldersToClear.Items.IndexOf(item);

                    FoldersToClear.SetItemChecked(index, checkbox);
                });
        }
    }

I've been working on CheckboxList to change text and font size after first word or item in my case. My question is how would I set this ChecklistBox to have different font size and font family after first line or item.

1

There are 1 best solutions below

0
hiddenUser On

Have you tried creating your own component? I created a custom component CustomCheckedListBox which extends the CheckedListBox.

Then I created an instance of the CustomCheckedListBox and added it to the form in the code behind. Now I have to admit that this is not the most elegant solution but it was the quickest I could come up with.

Here is an answer which I used as template

Here is the code of the CustomCheckedBoxList:

public class CustomCheckedListBox : CheckedListBox
{
    //Override the OnDrawItem event to add some logic
    protected override void OnDrawItem(DrawItemEventArgs e)
    {
        Color foreColour;

        //Change the foreColour (font colour) based on the index of the item
        if (e.Index > 0)
        {
            foreColour = Color.Green;
        }
        else
        {
            foreColour = Color.Black;
        }

        //Copy the original event args, just tweaking the fore color.
        var tweakedEventArgs = new DrawItemEventArgs(
            e.Graphics,
            e.Font,
            e.Bounds,
            e.Index,
            e.State,
            foreColour,
            e.BackColor);

        //Call the original OnDrawItem, but supply the tweaked color.
        base.OnDrawItem(tweakedEventArgs);
    }
}

Here is the code of the Form:

public partial class Form1 : Form
{
    private CustomCheckedListBox _customCheckedListBox;

    public Form1()
    {
        _customCheckedListBox = new CustomCheckedListBox();
        //Add the control to the form
        this.Controls.Add(_customCheckedListBox);

        InitializeComponent();
        InitializeCheckedListBox();
    }

    private void InitializeCheckedListBox()
    {
        _customCheckedListBox.Items.Add("Item 1");
        _customCheckedListBox.Items.Add("Item 2");
        _customCheckedListBox.Items.Add("Item 3");
    }
}