如何在 CheckListBox 中第一行之后设置新文本

问题描述 投票:0回答:1
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);
                });
        }
    }

我一直在研究 CheckboxList 以更改第一个单词或 item 之后的文本和字体大小。 我的问题是如何将这个 ChecklistBox 设置为在第一行或 item 之后具有不同的字体大小和字体系列。

c# windows-forms-designer checklistbox
1个回答
0
投票

您尝试过创建自己的组件吗?我创建了一个自定义组件

CustomCheckedListBox
,它扩展了
CheckedListBox

然后我创建了

CustomCheckedListBox
的实例并将其添加到后面代码中的表单中。现在我不得不承认这不是最优雅的解决方案,但它是我能想到的最快的解决方案。

这里是我用作模板的答案

这是

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);
    }
}

这是表格的代码:

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");
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.