Нет горизонтальной полосы прокрутки при установке ListBox DrawMode в OwnerDrawFixed - PullRequest
0 голосов
/ 29 апреля 2020

Приведенный ниже код отлично работает для рисования текстовых элементов в моем списке (articleLB) определенного цвета в зависимости от некоторых условий. Но когда текст длиннее, чем размер списка, горизонтальная полоса прокрутки не отображается. Я реализовал MeasureItemEventHandler и DrawItemEventHandler. Кто-нибудь может понять, почему это не работает?

articlesLB.DrawMode = DrawMode.OwnerDrawFixed;
articlesLB.MeasureItem += new MeasureItemEventHandler(articlesLB_MeasureItem);
articlesLB.DrawItem += new DrawItemEventHandler(articlesLB_SetColor);

private void articlesLB_MeasureItem(object sender, MeasureItemEventArgs e)
{
     int textwidth = TextRenderer.MeasureText(articlesLB.Items[e.Index].ToString(), articlesLB.Font).Width;
     articlesLB.HorizontalExtent = textwidth;
}

private void articlesLB_SetColor(object sender, DrawItemEventArgs e)
{
    if (e.Index < 0) return;

    //if the item state is selected them change the back color 
    if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
        e = new DrawItemEventArgs(e.Graphics,
                                  e.Font,
                                  e.Bounds,
                                  e.Index,
                                  e.State ^ DrawItemState.Selected,
                                  e.ForeColor,
                                  Color.Yellow); //Choose the color

    // Draw the background of the ListBox control for each item.
    e.DrawBackground();

    Brush myBrush = Brushes.Black;

    // check some boolean flags to see if either is checked

    if (EditAOSideRB.Checked == true || EditAPinSideRB.Checked == true)
    {
        string articleTitle = ((ListBox)sender).Items[e.Index].ToString();

        if (articleTitle.Contains("* ") == true)
        {
            // set the brush to green
            myBrush = Brushes.DarkGreen;
        }
        else
        {
            // set the brush to red
            myBrush = Brushes.Red;
        }
    }

    // Draw the current item text
    e.Graphics.DrawString(articlesLB.Items[e.Index].ToString(), e.Font, myBrush, e.Bounds, StringFormat.GenericDefault);

    // If the ListBox has focus, draw a focus rectangle around the selected item.
    e.DrawFocusRectangle();
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...