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 6214485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:53:05+00:00 2026-05-24T06:53:05+00:00

i am working on program on windows forms I have a listbox and I

  • 0

i am working on program on windows forms I have a listbox and I am validating data I want the correct data be added to the listbox with color green while the invalid data added with red color and I also want from the listbox to auto scroll down when an item is added and thanks

code :

try
{
    validatedata;
    listBox1.Items.Add("Successfully validated the data  : "+validateddata);
}
catch()
{
    listBox1.Items.Add("Failed to validate data: " +validateddata);
}
  • 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-24T06:53:06+00:00Added an answer on May 24, 2026 at 6:53 am

    Assuming WinForms, this is what I would do:

    Start by making a class to contain the item to add to the listbox.

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

    Add items to your listbox using this code:

    listBox1.Items.Add(new MyListBoxItem(Colors.Green, "Validated data successfully"));
    listBox1.Items.Add(new MyListBoxItem(Colors.Red, "Failed to validate data"));
    

    In the properties of the ListBox, set DrawMode to OwnerDrawFixed, and create an event handler for the DrawItem event. This allows you to draw each item however you wish.

    In the DrawItem Event:

    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
        }
    }
    

    There are a few limitations – the main one being that you’d need to write your own click handler and redraw appropriate items to make them appear selected, since Windows won’t do that in OwnerDraw mode. However, if this is just intended to be a log of things happening in your application, you may not care about items appearing selectable.

    To scroll to the last item try

    listBox1.TopIndex = listBox1.Items.Count - 1;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a working program in C++ that generates data for a Mandelbrot Set.
I have a normal windows forms program (Not VSTO) which deploys using click once.
I have a HelpButton on a Windows Forms application. When clicked, I just want
I am using C# and Windows Forms. I have a normal progress bar working
I was recently working on a windows program that would sometimes become unresponsive when
I'm working on a C# windows program with Visual Studio 2008. Usually, I work
I'm currently working on a C++ program in Windows XP that processes large sets
I'm working on a english only C++ program for Windows where we were told
I am working on a Windows utility program which communicates with some custom hardware
is there any small working program for recieving from and sending data to client

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.