470,641 Members | 1,578 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,641 developers. It's quick & easy.

Strange Behavior With BackColor In Inherited Control

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,

--
Sean
Sep 8 '06 #1
3 2257
All Designer-configured properties are set in the control's
InitializeComponent method. Make sure you save the background colour *after*
the call to InitializeComponent.

--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#
"senfo" wrote:
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,

--
Sean
Sep 8 '06 #2
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....

senfo wrote:
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,

--
Sean
Sep 9 '06 #3
Bruce Wood wrote:
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!

--
Sean
Sep 11 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Nadav | last post: by
1 post views Thread by Chris | last post: by
reply views Thread by Ron | last post: by
6 posts views Thread by semedao | last post: by
reply views Thread by rn5a | last post: by
1 post views Thread by =?Utf-8?B?Um9iZXJ0IFNtaXRo?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.