سؤال

I have a ListBox and it has a DrawItem event defined as follows. The ListBox's following properties are set as such - DrawMode = OwnerDrawFixed and FormattingEnabled = ture.

When I run the program and add more than one item or object to the ListBox, it flickers really bad. I am not sure what exactly is the problem. I have ListBoxes with a very similar setup on other winforms and they don't flicker. I try to capture an image of the winform with the flickering ListBox, but the captured image didn't show the ListBox flickering every time.

method HTrendFrm.AGroupList_DrawItem(sender: System.Object; e: System.Windows.Forms.DrawItemEventArgs);
    var
      lb:ListBox;
      tg:TTrendGroup;
    begin
      if e.Index = -1 then exit;
      lb := (sender as ListBox);
      tg := TTrendGroup(LoggingGroup.Item[e.Index]);
      if tg.Enabled then
      begin
        if ((e.State and DrawItemState.Selected) = DrawItemState.Selected) then
        begin
          lb.ForeColor:=Color.White;
          e.Graphics.FillRectangle(SystemBrushes.Highlight, e.Bounds);
        end
        else
         lb.ForeColor := Color.Black;
      end
      else
         lb.ForeColor := Color.LightGray;

      lb.CreateGraphics.DrawString(tg.name,new Font('Arial',9,FontStyle.Bold),new SolidBrush(lb.ForeColor),e.Bounds.Left+5,e.Bounds.Top);

        if ((e.State and DrawItemState.Focus) <> DrawItemState.Checked) then
            e.DrawFocusRectangle();
    end;

So, what is causing my ListBox Flicker?

Thanks in advance,

هل كانت مفيدة؟

المحلول

I followed LarsTech suggestion and it works as expected.

I totally removed lb listbox and replaced it with e.Graphics. Now, it doesn't flicker anymore.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top