Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: (untagged)
Hello
I am trying to make listbox, the items must be colored and whenever one of its items selected that item changed its color
And if I select it again its color returns back (toggle), but surprisingly that happened when I press the vertical scroll (against my desire)
I want it to become toggle when I select that item using mouse.
I think the index of the wanted item will not be available in mouse click event(mouseclick(object sender, MouseEventArgs e)) .
I will accept the answer even if you add another button or by any other way
thanks

What I have tried:

C#
int[] MARK = new int[10];
ListBox lb = new ListBox();
private void FormLoad(object sender, EventArgs e)
{
    for (i = 0; i < 10; i++) str3[i] = "Hello_"+i.ToString();
    lb.Items.AddRange(str3);//new object[] { str3 });
    lb.Location = new System.Drawing.Point(80, 40);
    lb.Size = new System.Drawing.Size(160, 80);
    lb.DrawMode = DrawMode.OwnerDrawFixed;
    lb.DrawItem += new DrawItemEventHandler(MyListBox_DrawItem);
    Controls.Add(lb);
}
private void MyListBox_DrawItem(object sender, DrawItemEventArgs e)
{
    ListBox lbSender = (ListBox)sender;
    if (lbSender == lb && (e.State & DrawItemState.Selected) != DrawItemState.Selected)
    {
        e.DrawBackground();
        string s = lb.Items[e.Index].ToString();
        Brush brush = Brushes.Green;

        if (MARK[e.Index] == 0) {brush = Brushes.Blue;}
        else if (MARK[e.Index] == -1) {brush = Brushes.Red;}
        e.Graphics.DrawString(lb.Items[e.Index].ToString(), e.Font, brush, e.Bounds.Location, StringFormat.GenericDefault);
    }
    else if (lbSender == lb && (e.State & DrawItemState.Selected) == DrawItemState.Selected)
    {
        e.DrawBackground();
        string s;
        Brush brush = Brushes.Green;

        if (MARK[e.Index] == 0) {brush = Brushes.Red;MARK[e.Index] = -1;}
        else if (MARK[e.Index] == -1){brush = Brushes.Blue;MARK[e.Index] = 0;}
        s = lb.Items[e.Index].ToString();
        e = new DrawItemEventArgs(e.Graphics,
                                 e.Font,
                                 e.Bounds,
                                 e.Index,
                                 e.State ^ DrawItemState.Selected,
                                 e.ForeColor,
                                 Color.Black);
        e.Graphics.DrawString(lb.Items[e.Index].ToString(), e.Font, brush, e.Bounds.Location, StringFormat.GenericDefault);
        e.DrawFocusRectangle();
    }
}
Posted
Updated 1-May-19 3:24am
v2
Comments
[no name] 1-May-19 2:26am    
Use WPF if you want "fancy" and less / no pain.

1 solution

What you are trying to achieve is not very easy with a ListBox, here is an example with a ListView:
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;

namespace ListboxToggle
{
    public partial class Form1 : Form
    {
        int[] MARK = new int[10];
        ListView lv = new ListView();

        public Form1()
        {
            InitializeComponent();
            lv.View = View.Details;
            lv.MultiSelect = false;
            lv.FullRowSelect = true;
            lv.Columns.Add("Header", 160);
            lv.HeaderStyle = ColumnHeaderStyle.None;
            lv.Location = new Point(80, 40);
            lv.Size = new Size(160, 80);

            for (int i = 0; i < 10; i++)
            {
                lv.Items.Add("Hello_" + i);
            }

            Controls.Add(lv);
            lv.SelectedIndexChanged += listview_SelectedIndexChanged;
        }

        private void listview_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            ListView lbSender = (ListView)sender;

            foreach (ListViewItem item in lbSender.SelectedItems)
            {
                Debug.Print(item.Text);

                if (item.BackColor == Color.Red)
                {
                    item.BackColor = Color.White;
                }
                else
                {
                    item.BackColor = Color.Red;
                }
            }
            
        }
    }
}
 
Share this answer
 
Comments
Engineer khalid 1-May-19 12:36pm    
Good alternative... my appreciation

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