Strange Behavior With BackColor In Inherited Control

S

senfo

I developed a Windows control in VS 2005 that inherits from the
PictureBox Control that adds the ability to select images in a Windows
application. It is, however, experiencing a strange issue that I can't
explain. One of the desired affects was to provide the ability to
change the background color on images that were selected, as well as
setting the background color back to its original color after the image
had been deselected.

To accomplish this task, I added a private member variable
(_originalBackColor) to remember the background color of the control in
its original, unselected state. The idea was that I could expose a
public property named SelectedBackColor that I could set BackColor to in
its selected state, then switch back to the unselected color by
assigning the value of _originalBackColor back to BackColor for when the
image is unselected.

Sounds simple enough! I determined, however, that the value of
BackColor at the time the constructor is run (which is where I assign
the value of BackColor to _originalBackColor), is not the value I
expected. For example, in the designer, I have the background color of
the SelectablePictureBox set to ControlLightLight, which is the same
color I use for the FlowLayoutPanel that hosts the SelectablePictureBox
control. However, when the constructor is executed, I have determined
that the value of BackColor is *not* ControlLightLight, so my
_originalBackColor variable is set incorrectly. The visual *appearance*
when the form first loads, however, would lead you to believe that the
SelectablePictureBox's BackColor were in fact set to ControlLightLight,
because it is displayed as white when the form first loads.

The behavior I am experiencing is as follows:

1) Form loads and SelectablePictureBox appears with desired color
2) Clicking SelectablePictureBox to select it causes desired selected
background color affect to be applied.
3) Clicking SelectablePictureBox to unselect it causes the background
color to turn a dark gray instead of the white color that
ControlLightLight implies.

The Click event handler is extremely simple:

void pic_Click(object sender, EventArgs e)
{
SelectablePictureBox pic = (SelectablePictureBox)sender;

if (!pic.IsSelected)
{
pic.SelectImage();
}
else
{
pic.UnSelectImage();
}
}

The code for the control is also pretty simple:

using System;
using System.Windows.Forms;
using System.Drawing;

namespace Foo
{
class SelectablePictureBox : PictureBox
{
private bool _isSelected;
private BorderStyle _selectedBorderStyle;
private BorderStyle _originalBorderStyle; // Original border
private Color _selectedBackColor;
private Color _originalBackColor; // Original background color
public delegate void SelectedChanged(object sender, EventArgs e);
public event SelectedChanged OnSelectedChanged;

/// <summary>
/// Gets or sets a value indicating whether control is selected
/// </summary>
public bool IsSelected
{
get
{
return _isSelected;
}
set
{
_isSelected = value;

// Fire OnSelectedChanged event
if (OnSelectedChanged != null)
OnSelectedChanged(this, new EventArgs());
}
}

/// <summary>
/// Indicates border style for the control when it is selected
/// </summary>
public BorderStyle SelectedBorderStyle
{
get { return _selectedBorderStyle; }
set { _selectedBorderStyle = value; }
}

/// <summary>
/// Gets or sets the background color for control when selected
/// </summary>
public Color SelectedBackColor
{
get { return _selectedBackColor; }
set { _selectedBackColor = value; }
}

/// <summary>
/// Creates an instance of a SelectablePictureBox
/// </summary>
public SelectablePictureBox()
{
this._originalBackColor = BackColor;
this._originalBorderStyle = BorderStyle;
}

/// <summary>
/// Selects the SelectablePictureBox
/// </summary>
public void SelectImage()
{
IsSelected = true;
BackColor = SelectedBackColor;
BorderStyle = SelectedBorderStyle;
}

/// <summary>
/// Unselects the SelectablePictureBox
/// </summary>
public void UnSelectImage()
{
IsSelected = false;
BackColor = _originalBackColor;
BorderStyle = _originalBorderStyle;
}
}
}


Any ideas why I'm experiencing this behavior?

Thank you in advance,
 
G

Guest

All Designer-configured properties are set in the control's
InitializeComponent method. Make sure you save the background colour *after*
the call to InitializeComponent.
 
B

Bruce Wood

You might consider capturing the original background colour in the
OnLoad method, rather in the constructor. I've found that some things
about controls don't quite "settle down" until the control is loaded
and gets a window handle.

That said, you would then have to figure out how to handle the case in
which the caller calls "Select" or "UnSelect" before the control is
first loaded....
 
S

senfo

Bruce said:
You might consider capturing the original background colour in the
OnLoad method, rather in the constructor. I've found that some things
about controls don't quite "settle down" until the control is loaded
and gets a window handle.

That said, you would then have to figure out how to handle the case in
which the caller calls "Select" or "UnSelect" before the control is
first loaded....

That was it, exactly. Thank you very much!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top