【问题标题】:How can I set the font size to 20 so the items in the listBox will fit?如何将字体大小设置为 20,以便 listBox 中的项目适合?
【发布时间】:2013-03-06 17:33:24
【问题描述】:

这是我在列表框中绘制和着色项目的类。函数是ColorListBox。如果我使用 8 号字体,它看起来还可以,但如果我使用 20 号字体,则 listBox 中的项目相互重叠;它们之间没有空格。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace GatherLinks
{
    class ColorText
    {   
        public static void Texts(RichTextBox box, string text, Color color)
        {
            box.SelectionStart = box.TextLength;
            box.SelectionLength = 0;

            box.SelectionColor = color;
            box.AppendText(text);
            box.SelectionColor = box.ForeColor;
        }

        public static void ColorListBox(List<string> data, DrawItemEventArgs e)
        {
            string strLeft = null;
            string strMid = "---";
            string strRight = null;

            if (data[e.Index].Contains(strMid))
            {
                int index = data[e.Index].IndexOf(strMid);
                strLeft = data[e.Index].Substring(0, index);
                strRight = data[e.Index].Substring(index + strMid.Length);
            }

            using (Font f = new Font(FontFamily.GenericSansSerif, 20, FontStyle.Regular))
            {
                float startPos;
                e.Graphics.DrawString(strLeft, f, Brushes.Red, e.Bounds.X, e.Bounds.Y);
                startPos = e.Graphics.MeasureString(strLeft, f).Width;
                e.Graphics.DrawString(strMid, f, Brushes.Black, e.Bounds.X + startPos, e.Bounds.Y);
                startPos = e.Graphics.MeasureString(strLeft + strMid, f).Width;
                e.Graphics.DrawString(strRight, f, Brushes.Green, e.Bounds.X + startPos, e.Bounds.Y);

            }
        }
    }
}

这是 20 码时的样子:

【问题讨论】:

  • 那么,您想知道如何更改文本上的填充,以便在字体大小为 20 时,它们不会重叠?这是正确的吗?

标签: c# winforms listbox


【解决方案1】:

尝试自己在 ListBox 中绘制项目。

将 ListBox 的 DrawMode 属性设置为 OwnerDrawVariable。通过 Designer 或代码执行此操作:

myListBox.DrawMode = DrawMode.OwnerDrawVariable;

为 DrawItem 和 MeasureItem 设置 ListBox 事件。通过 Designer 或代码执行此操作:

myListBox.DrawItem += new DrawItemEventHandler(DrawItem);
myListBox.MeasureItem += new MeasureItemEventHandler(MeasureItem);

这将允许您在为 ListBox 中的每个项目触发 DrawItem 和 MeasureItem 事件时收到通知。

为您正在收听的事件添加事件处理程序。如果您通过设计器添加它们,这些将自动填充。

private void DrawItem(object sender, DrawItemEventArgs e)
{
    e.DrawBackground();
    e.DrawFocusRectangle();

    // You'll change the font size here. Notice the 20
    e.Graphics.DrawString(data[e.Index],new Font(FontFamily.GenericSansSerif, 20, FontStyle.Bold), new SolidBrush(color[e.Index]),e.Bounds);
}

private void MeasureItem(object sender, MeasureItemEventArgs e)
{
    // You may need to experiment with the ItemHeight here..
    e.ItemHeight = 25;
}

【讨论】:

  • moncadad 我需要的只是将设计器中的模式更改为 OwnerDrawVariable 然后添加:listBox1_MeasureItem 事件,然后:e.ItemHeight = 25;我不需要再画它或任何东西,因为我是在上面的课堂上做的。现在工作完美,谢谢。
  • @user2065612 不客气。这很好,你只需要听一个事件。尝试使用 ItemHeight 进行试验。我只是随机选择了 25 个,认为它会很合适。
【解决方案2】:

试试,

listbox1.IntegralHeight=false; // where listbox1 is your listbox's ID
listbox1.Height=some_int_number;

【讨论】:

    【解决方案3】:

    我遇到了同样的问题。

    增加字体大小后增加 ListBox.ItemHeight 属性对我有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-05
      • 1970-01-01
      • 2011-06-29
      • 2017-04-15
      • 2015-09-07
      • 1970-01-01
      • 2011-03-10
      • 1970-01-01
      相关资源
      最近更新 更多