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

  • Home
  • SEARCH
  • 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 7671405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:02:50+00:00 2026-05-31T16:02:50+00:00

I use ListBox for inserting text like You add Michael in your database ,

  • 0

I use ListBox for inserting text like You add Michael in your database, You delete Michael, …

 listBox1.Items.Insert(0,"You add " + name + " in your database\n");

It works ok. How can i set color once black (for insert) and once red (for deletion)? I tried with this:

 public class MyListBoxItem
    {
        public MyListBoxItem(Color c, string m)
        {
            ItemColor = c;
            Message = m;
        }
        public Color ItemColor { get; set; }
        public string Message { get; set; }
    }

    private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
    {
        MyListBoxItem item = listBox1.Items[e.Index] as MyListBoxItem; // Get the current item and cast it to MyListBoxItem

        if (item != null)
        {
            e.Graphics.DrawString( // Draw the appropriate text in the ListBox
            item.Message, // The message linked to the item
            listBox1.Font, // Take the font from the listbox
            new SolidBrush(item.ItemColor), // Set the color 
            0, // X pixel coordinate
            e.Index * listBox1.ItemHeight // Y pixel coordinate.  Multiply the index by the ItemHeight defined in the listbox.
            );
        }
        else
        {
            // The item isn't a MyListBoxItem, do something about it
        }
    }

And on insertion:

 listBox1.Items.Insert(0, new MyListBoxItem(Color.Black, "You add " + name + " in your database\n"));
 listBox1.Items.Insert(0, new MyListBoxItem(Color.Red, "You delete " + name + "\n"));

This code works, but when i insert multiple items, scrol doesn’t work correctly – text does not appear. What am i doing wrong? Or is any other way to do this?

  • 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-31T16:02:52+00:00Added an answer on May 31, 2026 at 4:02 pm

    You should use:

    e.Bounds.Top
    

    instead of:

    e.Index * listBox1.ItemHeight
    

    Also, before drawing the text, I recommend drawing the background so you can see which item is selected, if the list supports selection, or support the list’s desired item background color in any case:

    using (Brush fill = new SolidBrush(e.BackColor))
    {
       e.Graphics.FillRectangle(fill, e.Bounds);
    }
    

    And you should properly dispose of the Brush that you’re creating to draw the text.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to use the listbox with a htmlstring? listbox.items.add( <font color='#efefef'
When I use the following code it works because I am using a ListBox
I have a system.collections.generic.list(of ListBox) I would like to use the collection classes built-in
Use case: I've just entered insert mode, and typed some text. Now I want
I use SetItemDataPtr to set listbox items pointers to my data, but i cannot
I use a owner drawn ListBox with a big ItemHeight, what works fine so
People use frequently something like: <ListBox ItemsSource={Binding ElementName=thisControl, Path=ListIndexes}> <ListBox.ItemTemplate> <DataTemplate> <StackPanel> <Label Content={Binding
I sort and group listbox items, I use CollectioView on this purpose. From view
Now I am aware that I probably need to use a ListBox and can
When we will use DrawItem for a listbox? Normally, if the listbox is ownerdraw,

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.