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 3 2368
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Nadav |
last post by:
I have a strange problem with inherited controls:
I have created two user controls called UserLoginA & UserLoginB.
One of the properties of these controls is of type UserGroup ( a class I...
|
by: Spam Trap |
last post by:
I am getting strange resizing problems when using an inherited form.
Controls are moving themselves seemingly randomly, but reproducibly.
"frmBase" is my base class (a windows form), and...
|
by: Chris |
last post by:
Hey,
I have a DataGrid that allows for a combobox in a column through a inherited
DataGridColumnStyle and a inherited ComboBox. I have 3 of these columns in
a row. The overall problem is that...
|
by: Ron |
last post by:
Hi,
I have a client side srcipt control that is giving me some very strange
problems when my Master.Page is layed out using <DIV> tags for the UI layout.
When it is layedout in a <TABLE> style...
|
by: ray well |
last post by:
in my app i need to make a RichTextbox control transparent.
i need it to be a like a pane of glass lying on a sheet of paper, where u
can see everything on the sheet of paper not covered by text...
|
by: semedao |
last post by:
Hi All,
I had working code that made custom serialization on objects that inherit from queue
in the inherited queue I create my own GetObjectData:
public void GetObjectData(SerializationInfo info,...
|
by: rn5a |
last post by:
A ASP.NET page has only a DataGrid control whose ID is "DGrid1". The
Page_Load sub looks like this::
Sub Page_Load(.....)
'binding the DataGrid
'to some data source
Dim child As Control
For...
|
by: =?Utf-8?B?Um9iZXJ0IFNtaXRo?= |
last post by:
Hi,
I have a behavior file called ScrollPos.htc, which is supposed to
maintain the scroll position of a datagrid within a div tag.
In my html I have
<%@ Page Language="C#"...
|
by: sirdavethebrave |
last post by:
Hi guys -
I have written a form, and a stored procedure to update the said form.
It really is as simple as that. A user can go into the form, update some fields and hit the update button to...
|
by: Rina0 |
last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: linyimin |
last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
|
by: erikbower65 |
last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA:
1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
|
by: kcodez |
last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
|
by: Taofi |
last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same
This are my field names
ID, Budgeted, Actual, Status and Differences
...
|
by: DJRhino1175 |
last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this -
If...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: lllomh |
last post by:
How does React native implement an English player?
| |