winforms C#使特定的列表框项文本加粗并删除一些项

hpcdzsge  于 2023-06-06  发布在  C#
关注(0)|答案(1)|浏览(168)

如何删除列表框中的“红色标记”区域并使“蓝色标记”区域加粗?我想在项目MAT和LBL1之间有两个空项目。我该如何让它成为可能?

I tryed:No idea how..但试着大胆一点

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        ListBox1.DrawMode = DrawMode.OwnerDrawFixed;
    }

    private void ListBox1_DrawItem(object sender, DrawItemEventArgs e)
    {
        ListBox lb1 = (ListBox)sender;
        var itemText = lb1.Items[e.Index].ToString();
        var textBold = FontStyle.Bold;

        // Make index bold from Client to MAT(or Client to first empty item)

        e.DrawBackground();
        e.Graphics.DrawString(ListBox1.Items[e.Index].ToString(), new Font("Arial", 10, FontStyle.Bold), Brushes.Black, e.Bounds);
        e.DrawFocusRectangle();
    }
}
u91tlkcl

u91tlkcl1#

我其实找到了解决办法。删除一些项目,并作出一些大胆的。它工作正常!

void document_PrintPage(object sender, PrintPageEventArgs e)
    {
        e.Graphics.PageUnit = GraphicsUnit.Millimeter;
        int leading = 1;
        int leftMargin = 25;
        int topMargin = 25;

        // a few simple formatting options..
        StringFormat FmtRight = new StringFormat() { Alignment = StringAlignment.Far };
        StringFormat FmtLeft = new StringFormat() { Alignment = StringAlignment.Near };
        StringFormat FmtCenter = new StringFormat() { Alignment = StringAlignment.Near };

        StringFormat fmt = FmtLeft;

        //set bold for the first 6 lines
        using (Font font = new Font("Arial", 12f, FontStyle.Bold))
        {
            SizeF sz = e.Graphics.MeasureString("_|", Font);
            float h = sz.Height + leading;

            for (int i = 0; i < 6; i++)
                e.Graphics.DrawString(listBox9.Items[i].ToString(), font,
                Brushes.Black, leftMargin, topMargin + h * i, fmt);
        }

        //make the rest regular
        using (Font font = new Font("Arial", 12f))
        {
            SizeF sz = e.Graphics.MeasureString("_|", Font);
            float h = sz.Height + leading;

            for (int i2 = 6; i2 < listBox9.Items.Count; i2++)
                e.Graphics.DrawString(listBox9.Items[i2].ToString(), font,
                Brushes.Black, leftMargin, topMargin + h * i2, fmt);
        }
    }

        listBox9.BeginUpdate();            

        string MAT = "MAT:";
        string KNR = "KNR";
        string SPANNUNG = "SPANNUNG";

        //select the item, from where the deletion should start
        for (int i = 0; i < listBox9.Items.Count; i++)
        {
            if (listBox9.Items[i].ToString().Contains(KNR))
            {
                listBox9.SetSelected(i + 1, true);
            }
            else if (listBox9.Items[i].ToString().Contains(MAT))
            {
                listBox9.ClearSelected();
                listBox9.SetSelected(i + 1, true);
            }
            else if (listBox9.Items[i].ToString().Contains(SPANNUNG))
            {
                listBox9.ClearSelected();
                listBox9.SetSelected(i + 1, true);
            }
        }

        //select all items between "SPANNUNG" and "LBL1"
        for (int i2 = listBox9.SelectedIndex; i2 < listBox9.Items.Count; i2++)
        {
            listBox9.SetSelected(i2, true);
            if (listBox9.Items[i2 + 1].ToString().Contains("LBL1"))
            {
                break;
            }
        }

        // delete all selected items between "SPANNUNG" and "LBL1", that I don't need
        var selected = listBox9.SelectedItems.Cast<Object>().ToArray();
        var selectedIndices = new List<int>(listBox9.SelectedIndices.Cast<int>());
        selectedIndices.Reverse();
        selectedIndices.ForEach(index => listBox9.Items.RemoveAt(index));

        // insert to spaces between "SPANNUNG" and "LBL1"
        for (int i3 = 0; i3 < listBox9.Items.Count; i3++)
        {
            if (listBox9.Items[i3].ToString().Contains("LBL1"))
            {
                listBox9.SetSelected(i3, true);
                listBox9.Items.Insert(listBox9.SelectedIndex, "");
                listBox9.Items.Insert(listBox9.SelectedIndex, "");
                break;
            }
        }

        listBox9.ClearSelected();
        listBox9.EndUpdate();

相关问题