Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
Hello Guys! i am facing a little issue in my project how can i change selected listbox items forecolor i can select all items of listbox but i dont know how to change forecolor of selected items
This coding am usinh in my project for select listbox items
        for (int i = 0; i < lbProductsToBuy.Items.Count; i++)
                    {
                        lbProductsToBuy.SetSelected(i,true);
                    }
                    printreceiptToken1();
                    dataGridView67.Rows.Clear();

                }



What I have tried:

i dont know how to do it so i dont try any thing please help me
Posted
Updated 16-Jul-16 19:19pm

C#
private void listBox1_DrawItem(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();
    // Draw the current item text
    e.Graphics.DrawString(listBox1.Items[e.Index].ToString(),e.Font, Brushes.Black, e.Bounds, StringFormat.GenericDefault);
    // If the ListBox has focus, draw a focus rectangle around the selected item.
    e.DrawFocusRectangle();
}


use above logic.
 
Share this answer
 
You can't do that without changing the DrawMode Property to
DrawMode.OwnerDrawFixed<br />
but that will remove some functionality, you then have to add all items programatically by your own.
Here i googled a C# sample;
Make an owner-drawn ListBox in C# - C# HelperC# Helper

I have made such a ComboBox control in VB.NET with many extended features like; BorderColor, DisplayText, ItemsForeColor, ItemsBackColor, ItemsFont etc..
If you want it i can send it to your Email, it's in VB.NET.

Please accept this answer if it helped you.
 
Share this answer
 
Comments
Member 9983063 16-Jul-16 16:20pm    
No sir i dont want this i think maybe my explain way is not right so better for me and you can you please come on skype so i can share screen with you and show you actual problem my skype id is : lakhasajid081@hotmail.com
[no name] 16-Jul-16 17:51pm    
I can't do your homework, but if you know what you are trying to achieve then edit your question, add some more code, including the block where you stuck and let's see.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900