Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6763697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:30:26+00:00 2026-05-26T14:30:26+00:00

In my WinForms application I have a timer which ‘ticks’ every second. In the

  • 0

In my WinForms application I have a timer which ‘ticks’ every second. In the tick-method of this timer, I do various things, including adding items to a listbox (called lbxLogText).

I need to have the option to colour the text of some of the items (the colour is decided before the item is added). Because of this, I have set the DrawMode property to OwnerDrawVariable, and the lbxLogText_DrawItem method looks like this:

private void lbxLogText_DrawItem(object sender, DrawItemEventArgs e)
{
    e.DrawBackground();
    e.DrawFocusRectangle();
    e.Graphics.DrawString(logStringToAdd, 
        new Font(FontFamily.GenericSansSerif, 8),
        new SolidBrush(logStringToAddColor), e.Bounds);

    testCounter++;
    label29.Text = testCounter.ToString();
}

logStringToAdd: a global string.

logStringToAddColor: a global Color – either red or black.

testCounter: a global int, initiated to 0.

I have a method called Log(). This is called in the tick-method of the timer mentioned above.

This is how it looks like:

private void Log(string status)
{
    // |red, |black
    if (status != null)
    {
        if (status.Contains("|red"))
        {
            status = status.Replace("|red", "");
            logStringToAddColor = Color.Red;
        }
        else if (status.Contains("|black"))
        {
            status = status.Replace("|black", "");
            logStringToAddColor = Color.Black;
        }
        logStringToAdd = status;
        lbxLogText.Items.Add(new object());
    }

    // scroll to bottom
    lbxLogText.SetSelected(lbxLogText.Items.Count - 1, true);
    lbxLogText.SetSelected(lbxLogText.Items.Count - 1, false);
}

status: Could be “System is working correctly.|black”, or “System is NOT working correctly.|red” (for instance). This parameter is updated before the Log-call (of course).

This code works OK, to some extent. I have the following issues:

  • when running the program, I can see on label29 that the testCounter variable is not just incremented every second, it’s like it starts at 1, then becomes 3, then 6, 10, 15, 21, 28, 36 (I think you got the pattern now). This means that the DrawItem event is called more than just every second.

  • let’s say status goes from “asd123|black” to “qwe456|red”. This means the next item added to the listbox should be red. Well, it does become red, but all items in the listbox become red. And the text of all items is changed to the newest as well.

  • when debugging, I can see that when the SetSelected method is called, it goes right down to the DrawItem event method. But I don’t see how I can avoid this, since I need the listbox to scroll to the bottom when a new item is added, so the newest item is visible.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:30:27+00:00Added an answer on May 26, 2026 at 2:30 pm

    As Otiel said, DrawItem is called whenever any item needs to be redrawn, and you’re using a global state to store the text and colour for all of the items.

    To make it specific to each item, you can add an object/type instance that returns the text as .ToString() but you can query for the colour in your DrawItem event:

    struct LogItem {
      public string Text;
      public Colour ItemColour
    }
    
    private void Log(string status) { 
      LogItem item = new LogItem();
      item.Text = "Wibble";
      item.ItemColour = Colours.Red;
      lbxLogText.Items.Add(item); 
    }
    
    private void lbxLogText_DrawItem(object sender, DrawItemEventArgs e) {   
      LogItem item = lbxLogText.Items[e.Index];
      e.DrawBackground();   
      e.DrawFocusRectangle();   
      e.Graphics.DrawString(item.Text,    
        new Font(FontFamily.GenericSansSerif, 8),   
        new SolidBrush(item.Color), e.Bounds);   
    }  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a WinForms application which runs on a touch-screen on a bit of
I have a Winforms application which is working fine.. using a BackgroundWorkerThread to manage
We have a WinForms desktop application, which is heavily multithreaded. 3 threads run with
I got a .net WinForms application. I have a UserControl which gets instantiated based
I have a WinForms application that was written in C# .NET 3.5. This application
I have C# winforms application that needs to start an external exe from time
I have an application (winform exe) that I run several times. Does this mean
I have a WinForms application with a DataGridView control and a column of DataGridViewButtonCell
I have a winforms application that normally is at about 2-4% CPU. We are
We have a WinForms application written in C# that uses the AxAcroPDFLib.AxAcroPDF component to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.