Why the variable bool is all the time false ?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I have two codes. The first one is a User Control of a listBox where i check if i click on an item with the right mouse click or the left mouse click.
If its the right mouse click it will color the clicked item in Red if its the left click it will color it to the original color back.
The variable in this code is: isColoring i also created a property for this variable.

<pre class="prettyprint /*----------------------------------------------------------------
* Module Name : ListBoxControl
* Description : Change listBox items color
* Author : Danny
* Date : 30/12/2012
* Revision : 1.00
* --------------------------------------------------------------*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

/*
* Introduction :
*
* By default the color is red.
* Added a property to change the color.
* Right mouse click on item to change item color.
* Left mouse click on item to change the item color back.
* If the listBox is empty the control will be filled with 10 "Test" items.
* */

namespace Lightnings_Extractor // to check how and change the namespace to listBoxControl
{
public partial class ListBoxControl : UserControl
{
private Color m_MyListColor;
private List<int> m_itemIndexes = new List<int>();
private bool coloring;

public ListBoxControl()
{
InitializeComponent();

if (listBox1.Items.Count == 0)
{
for (int i = 0; i < 10; i++)
{
listBox1.Items.Add("Test " + i);
}
}
}

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

if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
if (m_itemIndexes.Contains(index))
return;

m_itemIndexes.Add(index);
DrawItem(index);
}
else if (e.Button == MouseButtons.Left)
{
if (!m_itemIndexes.Contains(index))
return;

m_itemIndexes.Remove(index);
DrawItem(index);
}
}

private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
m_MyListColor = MyListColor;
if (m_MyListColor.IsEmpty == true)
{
m_MyListColor = Color.Red;
}
coloring = m_itemIndexes.Contains(e.Index);
bool selected = (e.State & DrawItemState.Selected) == DrawItemState.Selected;

if (coloring)
{
using (var brush = new SolidBrush(m_MyListColor))
{
e.Graphics.FillRectangle(brush, e.Bounds);
}
}
else
{
e.DrawBackground();
}

string item = listBox1.Items[e.Index].ToString();
e.Graphics.DrawString(item, e.Font, selected || coloring ? Brushes.White : Brushes.Black, e.Bounds, StringFormat.GenericDefault);

if (selected)
e.DrawFocusRectangle();
}

private void DrawItem(int index)
{
Rectangle rectItem = listBox1.GetItemRectangle(index);
listBox1.Invalidate(rectItem);
}

[Browsable(true)]
public Color MyListColor
{
get { return m_MyListColor; }
set
{
m_MyListColor = value;
Refresh();
}
}

[Browsable(true)]
public ListBox MyListBox
{
get { return listBox1; }
set
{
listBox1 = value;
Refresh();
}
}

[Browsable(true)]
public bool isColoring
{
get { return coloring; }
set
{
coloring = value;
Refresh();
}
}

private void ListBoxControl_Load(object sender, EventArgs e)
{
this.listBox1.SelectedIndex = 0;
}
}
}
[/code]
This is the propert part:

<pre class="prettyprint [Browsable(true)]
public bool isColoring
{
get { return coloring; }
set
{
coloring = value;
Refresh();
}
}
[/code]
Now im using this property in a new Form there im adding items to the listBox of the User Control. And also using the listBox event: SelectedIndexChanged
All this is connected to the listBox in the user control after dragged the control to the designer of the new Form.

<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
{
private List<int> m_itemIndexes = new List<int>();
Lightnings_Extractor.PDF pdf1;
public static string item;
Form1 f1 = null;
List<string> recentItems = new List<string>();

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();
listBoxControl1.MyListBox.SelectedIndex = 0; listBoxControl1.MyListBox.SelectedIndexChanged += new EventHandler(listBox1_SelectedIndexChanged);
}

private void listBoxIndexs()
{
listBoxControl1.MyListBox.Items.Clear();
listBoxControl1.Dock = DockStyle.Fill;
for (int i = 0; i < Form1.lightningsRegions.Count; i++)
{

listBoxControl1.MyListBox.Items.Add(Form1.lightningsRegions);

}
}

private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
item = listBoxControl1.MyListBox.SelectedItem.ToString();
this.f1.PlayLightnings();
f1.pdftoolsmenu();
if (listBoxControl1.isColoring == true)
{
if (item != null && !pdf1.Lightnings.Contains(item.ToString()))
{
pdf1.Lightnings.Add(item.ToString()); }
}
}
}
}
[/code]
The problem is in the SelectedIndexChanged event in the new Form. I want to check that only if i did a right click and if it colored then do the rest of the code in the SelectedIndexChanged event.

<pre class="prettyprint if (listBoxControl1.isColoring == true)
[/code]
The problem is that the variable isColoring all the time false in the new Form. I used a breakpoint and each time i think that each time i click on an item in the listBox its going first to the SelectedIndexChanged event and after it its going to the User
Control code and doing the there the line:

<pre class="prettyprint if (coloring)[/code]
And using a breakpoint after doing this line isColoring is true but thats after it was in the new Form selectedindexchanged event.
Then i click right click button on an item again so its Red and isColoring should be true now but its false in the new Form.
What ma i missing here ?
<hr class="sig danieli

View the full article
 
Back
Top