473,626 Members | 3,183 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
(_originalBackC olor) to remember the background color of the control in
its original, unselected state. The idea was that I could expose a
public property named SelectedBackCol or that I could set BackColor to in
its selected state, then switch back to the unselected color by
assigning the value of _originalBackCo lor 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 _originalBackCo lor), is not the value I
expected. For example, in the designer, I have the background color of
the SelectablePictu reBox set to ControlLightLig ht, which is the same
color I use for the FlowLayoutPanel that hosts the SelectablePictu reBox
control. However, when the constructor is executed, I have determined
that the value of BackColor is *not* ControlLightLig ht, so my
_originalBackCo lor variable is set incorrectly. The visual *appearance*
when the form first loads, however, would lead you to believe that the
SelectablePictu reBox's BackColor were in fact set to ControlLightLig ht,
because it is displayed as white when the form first loads.

The behavior I am experiencing is as follows:

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

The Click event handler is extremely simple:

void pic_Click(objec t sender, EventArgs e)
{
SelectablePictu reBox pic = (SelectablePict ureBox)sender;

if (!pic.IsSelecte d)
{
pic.SelectImage ();
}
else
{
pic.UnSelectIma ge();
}
}

The code for the control is also pretty simple:

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

namespace Foo
{
class SelectablePictu reBox : PictureBox
{
private bool _isSelected;
private BorderStyle _selectedBorder Style;
private BorderStyle _originalBorder Style; // Original border
private Color _selectedBackCo lor;
private Color _originalBackCo lor; // Original background color
public delegate void SelectedChanged (object sender, EventArgs e);
public event SelectedChanged OnSelectedChang ed;

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

// Fire OnSelectedChang ed event
if (OnSelectedChan ged != null)
OnSelectedChang ed(this, new EventArgs());
}
}

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

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

/// <summary>
/// Creates an instance of a SelectablePictu reBox
/// </summary>
public SelectablePictu reBox()
{
this._originalB ackColor = BackColor;
this._originalB orderStyle = BorderStyle;
}

/// <summary>
/// Selects the SelectablePictu reBox
/// </summary>
public void SelectImage()
{
IsSelected = true;
BackColor = SelectedBackCol or;
BorderStyle = SelectedBorderS tyle;
}

/// <summary>
/// Unselects the SelectablePictu reBox
/// </summary>
public void UnSelectImage()
{
IsSelected = false;
BackColor = _originalBackCo lor;
BorderStyle = _originalBorder Style;
}
}
}
Any ideas why I'm experiencing this behavior?

Thank you in advance,

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

--
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
(_originalBackC olor) to remember the background color of the control in
its original, unselected state. The idea was that I could expose a
public property named SelectedBackCol or that I could set BackColor to in
its selected state, then switch back to the unselected color by
assigning the value of _originalBackCo lor 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 _originalBackCo lor), is not the value I
expected. For example, in the designer, I have the background color of
the SelectablePictu reBox set to ControlLightLig ht, which is the same
color I use for the FlowLayoutPanel that hosts the SelectablePictu reBox
control. However, when the constructor is executed, I have determined
that the value of BackColor is *not* ControlLightLig ht, so my
_originalBackCo lor variable is set incorrectly. The visual *appearance*
when the form first loads, however, would lead you to believe that the
SelectablePictu reBox's BackColor were in fact set to ControlLightLig ht,
because it is displayed as white when the form first loads.

The behavior I am experiencing is as follows:

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

The Click event handler is extremely simple:

void pic_Click(objec t sender, EventArgs e)
{
SelectablePictu reBox pic = (SelectablePict ureBox)sender;

if (!pic.IsSelecte d)
{
pic.SelectImage ();
}
else
{
pic.UnSelectIma ge();
}
}

The code for the control is also pretty simple:

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

namespace Foo
{
class SelectablePictu reBox : PictureBox
{
private bool _isSelected;
private BorderStyle _selectedBorder Style;
private BorderStyle _originalBorder Style; // Original border
private Color _selectedBackCo lor;
private Color _originalBackCo lor; // Original background color
public delegate void SelectedChanged (object sender, EventArgs e);
public event SelectedChanged OnSelectedChang ed;

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

// Fire OnSelectedChang ed event
if (OnSelectedChan ged != null)
OnSelectedChang ed(this, new EventArgs());
}
}

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

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

/// <summary>
/// Creates an instance of a SelectablePictu reBox
/// </summary>
public SelectablePictu reBox()
{
this._originalB ackColor = BackColor;
this._originalB orderStyle = BorderStyle;
}

/// <summary>
/// Selects the SelectablePictu reBox
/// </summary>
public void SelectImage()
{
IsSelected = true;
BackColor = SelectedBackCol or;
BorderStyle = SelectedBorderS tyle;
}

/// <summary>
/// Unselects the SelectablePictu reBox
/// </summary>
public void UnSelectImage()
{
IsSelected = false;
BackColor = _originalBackCo lor;
BorderStyle = _originalBorder Style;
}
}
}
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
(_originalBackC olor) to remember the background color of the control in
its original, unselected state. The idea was that I could expose a
public property named SelectedBackCol or that I could set BackColor to in
its selected state, then switch back to the unselected color by
assigning the value of _originalBackCo lor 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 _originalBackCo lor), is not the value I
expected. For example, in the designer, I have the background color of
the SelectablePictu reBox set to ControlLightLig ht, which is the same
color I use for the FlowLayoutPanel that hosts the SelectablePictu reBox
control. However, when the constructor is executed, I have determined
that the value of BackColor is *not* ControlLightLig ht, so my
_originalBackCo lor variable is set incorrectly. The visual *appearance*
when the form first loads, however, would lead you to believe that the
SelectablePictu reBox's BackColor were in fact set to ControlLightLig ht,
because it is displayed as white when the form first loads.

The behavior I am experiencing is as follows:

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

The Click event handler is extremely simple:

void pic_Click(objec t sender, EventArgs e)
{
SelectablePictu reBox pic = (SelectablePict ureBox)sender;

if (!pic.IsSelecte d)
{
pic.SelectImage ();
}
else
{
pic.UnSelectIma ge();
}
}

The code for the control is also pretty simple:

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

namespace Foo
{
class SelectablePictu reBox : PictureBox
{
private bool _isSelected;
private BorderStyle _selectedBorder Style;
private BorderStyle _originalBorder Style; // Original border
private Color _selectedBackCo lor;
private Color _originalBackCo lor; // Original background color
public delegate void SelectedChanged (object sender, EventArgs e);
public event SelectedChanged OnSelectedChang ed;

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

// Fire OnSelectedChang ed event
if (OnSelectedChan ged != null)
OnSelectedChang ed(this, new EventArgs());
}
}

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

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

/// <summary>
/// Creates an instance of a SelectablePictu reBox
/// </summary>
public SelectablePictu reBox()
{
this._originalB ackColor = BackColor;
this._originalB orderStyle = BorderStyle;
}

/// <summary>
/// Selects the SelectablePictu reBox
/// </summary>
public void SelectImage()
{
IsSelected = true;
BackColor = SelectedBackCol or;
BorderStyle = SelectedBorderS tyle;
}

/// <summary>
/// Unselects the SelectablePictu reBox
/// </summary>
public void UnSelectImage()
{
IsSelected = false;
BackColor = _originalBackCo lor;
BorderStyle = _originalBorder Style;
}
}
}
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1778
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 defined in another assembly). Then I show that UserLoginA & UserLoginB have a lot of common code, so I decided to inherit UserLoginB from UserLoginA. So I Created a new user control UserControlC that inherits from UserLoginA, added the code needed to...
8
6692
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 contains a few controls anchored to the sides of the form. "frmChild" inherits from "frmBase"... and the controls appear on the inherited form as expected. However things start going wrong when I place addition controls on the "frmChild" form. When I...
1
1124
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 when I hit TAB on the first of 3 of these columns the cursor moves into the second and then immideantly leaves and moves to the third box. if you leave the second one it moves to a new line (through the 3rd one). I'm trying to figure out why...
0
1336
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 fashion i have no issues. I do a RegisterClientScriptInclude for the .js files in the code behind of the master. This has no problems my scripts are loaded fine.
4
9347
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 written on the glass pane. i need to be able to see the control or form that is underneath the RichTextbox, and at the same time to be able to write and erase text to the RichTextbox. i'm assuming that it is the backcolor that hides what is...
6
1892
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, StreamingContext ctxt) { lock (this.SyncRoot) {
0
1054
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 Each child In DGrid1.Controls 'Response.Write(child.GetType())
1
1291
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#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="MaintainScrollDemo" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
2629
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 update the information which is stored in a SQL database. In testing we noticed that the form was updating correctly but the update mechanism was also updating the first record of the table in the sql database every time. No error messages are on...
0
8265
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8196
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8705
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8637
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6125
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5574
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4197
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2625
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1808
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.