How can i color an item in listBox when clicking the mouse right button ?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
This is the code im using now. The coloring section in the DrawItem event is working if im not using the flag isColor.

<pre class="prettyprint private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
item = listBox1.SelectedItem.ToString();
this.f1.PlayLightnings();
f1.pdftoolsmenu();
if (item != null && !pdf1.Lightnings.Contains(item.ToString()))
{
pdf1.Lightnings.Add(item.ToString());
}
}

private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
listBox1.SelectedIndex = listBox1.IndexFromPoint(e.X, e.Y);

if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
isColor = true;
}
}

private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
if (isColor == true)
{
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.Red);//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();
}
}[/code]
What i need it to do is:

1. When clicking on an item with the right button mouse click it will color the item in Red.
2. When clicking on colored Red item with the left mouse button it will return to its marked blue original color.
3. If i click on an item wich is not colored in Red with left mouse button it wont change its color.
4. To enable multiple selections only on Red(when making right click button on the mouse on an item).
The first problem now with the code as it is is that when i click the button that show/open the listBox its first going to the DrawItem event and since the flag is false there is nothing in the listBox then when i click left or right i see the item i click
on but its not working good.
This is the Full code of my new Form with the listBox:

<pre class="prettyprint using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using unfreez_wrapper;
using WindowsFormsApplication1;
using DannyGeneral;
using System.Text.RegularExpressions;
using Extracting_Frames;

namespace Lightnings_Extractor
{
public partial class Lightnings_Mode : Form
{
Lightnings_Extractor.PDF pdf1;
public static string item;
Form1 f1 = null;
List<string> recentItems = new List<string>();
bool isColor = false;

public Lightnings_Mode(Form1 f1)
{
InitializeComponent();

pdf1 = new Lightnings_Extractor.PDF();
this.f1 = f1;
}

private void Lightnings_Mode_Load(object sender, EventArgs e)
{
this.Size = new Size(416, 506);
this.Location = new Point(23, 258);
listBoxIndexs();
this.listBox1.SelectedIndex = 0;
}

private void listBoxIndexs()
{
for (int i = 0; i < Form1.lightningsRegions.Count; i++)
{

listBox1.Items.Add(Form1.lightningsRegions);

}
}

private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
item = listBox1.SelectedItem.ToString();
this.f1.PlayLightnings();
f1.pdftoolsmenu();
if (item != null && !pdf1.Lightnings.Contains(item.ToString()))
{
pdf1.Lightnings.Add(item.ToString());
}
}

private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
listBox1.SelectedIndex = listBox1.IndexFromPoint(e.X, e.Y);

if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
isColor = true;
}
}

private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
if (isColor == true)
{
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.Red);//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();
}
}

}
}[/code]
<br/>
I need to make all sections from 1 to 4 and first to solve this flag problem i used a falg since i thought it will help me to color the item in Red only when i click the right mouse button.<hr class="sig danieli

View the full article
 
Back
Top