473,385 Members | 1,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Problems with properties of derived controls

Hello,
I currently fight with a problem during the derivative of WinForm controls.

In Visual Studio I created a new User Control. This control is derived from
the DataGridView of the System.Windows.Forms namespace. I want to use this
control as a template for futher controls. Within this control i want to set
some property values to be pre-defined for further derived controls.

My base class looks like this:

public partial class BaseDataGrid : System.Windows.Forms.DataGridView
{
private bool _Enabled;

public BaseDataGrid() : base()
{
InitializeComponent();
this.Enabled = false;
}

[DefaultValue(false)]
public new bool Enabled
{
get
{
return this._Enabled;
}
set
{
this._Enabled = value;
}
}
}

For testing this control I created a WinForm and put it on the form. Looking
at the property window shows the Enabled-Property as set to "true". and not
as expected to "false". The entry "true" is bold, so the Designer recognized
that the default value for this property is "false".

Further, I derived a new User Control from my BaseDataGridView control, put
it on my form. As the BaseDataGridView the property window shows the
Enabled-Property as set to "true".

It seems that the Designer uses the Property-Value of the next-higher level
control in a control hierarchy as the default value for this property. But,
for pre-defining the property it uses the property default value of the
highest class in the object hierarchy.

My second approch was not to "override" the Enabled-Property. I only set the
Enabled-property derived from the DataGridView to "false".

My BaseDataGrid-class now looks like this:

public partial class BaseDataGrid : System.Windows.Forms.DataGridView
{
public BaseDataGrid() : base()
{
this.Enabled = false;
}
}

One again, putting this control on a WinForm is suprising me. Within the
property window the Enabled-property is set to false. Everything seems to be
fine. But when changing it to "true" there will be not code is written into
the InitializeComponent-method of the form. When starting the form the
control will be disabled. Debugging the InitializeComponent-method shows that
the control has its Enabled-property set to "false".

Part of the InitializeComponent-method when putting the control on the form:

this.baseDataGrid1.ColumnHeadersHeightSizeMode =
System.Windows.Forms.DataGridViewColumnHeadersHeig htSizeMode.AutoSize;
this.baseDataGrid1.Enabled = false;
this.baseDataGrid1.Location = new System.Drawing.Point(358, 88);
this.baseDataGrid1.Name = "baseDataGrid1";
this.baseDataGrid1.Size = new System.Drawing.Size(240, 150);
this.baseDataGrid1.TabIndex = 0;

Part of the InitializeComponent-method after setting the property to true:

this.baseDataGrid1.ColumnHeadersHeightSizeMode =
System.Windows.Forms.DataGridViewColumnHeadersHeig htSizeMode.AutoSize;
this.baseDataGrid1.Location = new System.Drawing.Point(358, 88);
this.baseDataGrid1.Name = "baseDataGrid1";
this.baseDataGrid1.Size = new System.Drawing.Size(240, 150);
this.baseDataGrid1.TabIndex = 0;

Hope, anyone of you has an idea how to solve this problem.

Thanks and best regrads
Chris
Jun 27 '08 #1
1 2204
Hello Ignacio,
thanks for Your reply. I was enjoying the good weather this weekend, so
sorry for my late delay.

First of, thanks for the important information about the fact, that a
pre-definition of a control property doesn't be used by the designer.

Your solution, to assign the desired values within the constructor was the
point where my problems began. I have changed my example to make my problem a
litte bit more "visual".

Lets take a textbox and build a new textbox control to be the base for all
further textboxes. Lets definie that the default back color of the textbox
should be blue.

The source code of the text box:

public partial class BaseTextBox : TextBox
{
public BaseTextBox()
:base()
{
InitializeComponent();
this.BackColor = Color.Blue;
}
}

I put this control on a WinForm. The back color of the control is blue.
Within the property window, the back color property is set to blue.
Everythink seems to be fine. But the setting is bold, so when saving the
form, it will be written to the InitializeComponent method.

The InitializeComponent method of the form:

private void InitializeComponent()
{
this.baseTextBox1 = new ControlInheritance.BaseTextBox();
this.SuspendLayout();
//
// baseTextBox1
//
this.baseTextBox1.BackColor = System.Drawing.Color.Blue;
this.baseTextBox1.Location = new System.Drawing.Point(12, 25);
this.baseTextBox1.Name = "baseTextBox1";
this.baseTextBox1.Size = new System.Drawing.Size(100, 20);
this.baseTextBox1.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292,266);
this.Controls.Add(this.baseTextBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}

When understanding the use polymorphism right, changes at a class will take
affect everywhere, where it has been used. So lets try it. Lets say, the
default back color of our textbox should be red. First I will change the base
class code as follows:

public partial class BaseTextBox : TextBox
{
public BaseTextBox()
:base()
{
InitializeComponent();
this.BackColor = Color.Red;
}
}

When I open the form I already put the "former" base textbox on it, the
changes will not take affect: The old base text box is still blue, a new one
will be red...

This all happens because of the designer didn't recognize that we have
changed the "default" back color. The designer notice that the back color has
been changed from its default (as far as I know: SystemColor.Window) to the
new value blue. So You have to replace every used base textbox with its new
version :-(

A side effect I notice: Take the base textbox and change the bakc color to
the default (SystemColor.Window). The designer will never accepts any color
as default value...

BUT: this code will work fine...

public partial class BaseTextBox : TextBox, ISupportInitialize
{
public BaseTextBox()
: base()
{
InitializeComponent();
this.BackColor = Color.Blue ;
}

[DefaultValue(typeof(Color), "Blue")]
public new Color BackColor
{
get
{
return base.BackColor;
}
set
{
base.BackColor = value;
}
}

public void BeginInit()
{
}

public void EndInit()
{
Color c = this.BackColor;
}
}

The textbox on the form is blue. Replace Blue with Red and rebuild Your
projekt. Now the textbox will be red... But this is quit a dirty way to do
this...
Perhaps, I have a big error within my thinking, but hope anyone can help me...

Thanks
Chris

Jun 29 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: William Bradley | last post by:
First of all I have been working with Access 97 and this morning the owner of the business phoned me to inform me that he had updated to Access 2000 and parts of my forms would not work anymore. ...
6
by: bryanhobson | last post by:
I'm fairly new to c#, and I am just trying to work out how a 'properties' dialog works. Currently in my code, I have an object represented by the class 'Dog'. The dog object has several...
2
by: Brian | last post by:
NOTE ALSO POSTED IN microsoft.public.dotnet.framework.aspnet.buildingcontrols I have solved most of my Server Control Collection property issues. I wrote an HTML page that describes all of the...
3
by: One Handed Man | last post by:
How can one fire an event programatically for a given control. In another post someone wanted to cause an event in order to make a RichTextBox scroll down. However, the onVscroll method is a...
4
by: Dennis | last post by:
I am trying to set the default design proerties in a control I have derived from the Panel Class. I thought I'd found how to do it from the MSDN but the following line doesn't work: Inherits...
6
by: Yehia A.Salam | last post by:
Hello, I'm trying to create my own control derived from ScrollableControl public partial class qViewer : ScrollableControl{ public qViewer() { VScroll = true; AutoScroll = true; } ... }
6
by: | last post by:
I have made some user controls with custom properties. I can set those properties on instances of my user controls, and I have programmed my user control to do useful visual things in response to...
1
by: =?Utf-8?B?SmltIFdhbHNo?= | last post by:
I have an VC++ MFC Win32 application that isn't working correctly when I build it with VS2005. The problem seems to be in some ADO ActiveX controls that came with VS6 and are now out of support....
3
by: =?Utf-8?B?R3JlZw==?= | last post by:
I am used to using third party controls when it comes to setting up appearences. But, now I am using Visual Basic.Net controls that come standard with the product. I've come across a frustration...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...

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.