Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to subclass control?
Message
From
22/04/2008 10:37:49
 
 
To
22/04/2008 01:15:23
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
C# 2.0
OS:
Windows XP SP2
Network:
Windows 2000 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
01184867
Message ID:
01312364
Views:
24
Wait a minute!!! ( I'm saying that kindly...) There must be something I still do not understand...

In a previous post, *YOU* said putting all this in the constructor was a no-no (or at least it would have un-desired side-effects):

You said:

That second point is pretty important. Let's use a TextBox as an example, along with it's BackColor property. Say that you've not used the [DefaultValue] attribute in your TextBox, and simply set the value of the BackColor in your TextBox's constructor so that it looks like this:
public class MyTextBox : TextBox
{
	public MyTextBox()
	{
		this.BackColor = Color.DarkGreen;
	}
}
When you drop this TextBox onto a Form, the BackColor will be explicitly set in code to Color.DarkGreen in the InitializeComponent() method of the Form. Consequently, if you later decide to change the color in your MyTextBox class, you have to revisit every single Form that you ever dropped that TextBox on to change it to the new default value. Not good!


Now, I definately do NOT want to have to "revisit every single form...", but maybe that just applies to dealing with the the Default Value re-setting of the property in the IDE. I do not so much care about the "default value" in the IDE part, but I do want to control visual things like color, font, size, from a central class so that they will automatically re-propogate to all forms when I change the base class.

So, it is starting to seem like I CAN use the contstructor do achieve what I want, as long as I don't care about the IDE re-setting of the Default Value thing.

Is that correct?

Your expanded version required adding Get and Set methods and the whole really began to explode way beyond what I was expecting, but the Constructor-only way is much more (in fact exactly) what I was looking for.
public class MyTextBox : TextBox
{
	public MyTextBox()
	{
	}
	
	[DefaultValue(typeof(System.Drawing.Color), "DarkGreen")]
	public override System.Drawing.Color BackColor
	{
		get {return base.BackColor;}
		set {base.BackColor = value;}
	}
}
>This doesn't work. Have you tried dragging that "Component TextBox" from the ToolBox onto a Form? Seems it disappears somewhere ... well, it's not visible anyway. Components are not meant to be used this way.
>
>The preferable way to do this is to create one file that holds most of these simple types of controls (TextBox, ComboBox, ListBox, ListView, CheckBox, etc.), as I have previously mentioned.
>
>As far as the purpose of a Component class, here's what the documentation says about it:
>
>Component is the base class for all components in the common language runtime that marshal by reference. Component is remotable and derives from the MarshalByRefObject class. Component provides an implementation of the IComponent interface. The MarshalByValueComponent provides an implementation of IComponent that marshals by value.
>
>You can host a Component in any object that implements the IContainer interface, and you can query and get services from its container. The container creates an ISite for each Component it contains. The container uses the site to manage the Component and is used by the Component to communicate with its container.

>
>That certainly doesn't sound like something I want to stick a TextBox on. <g>
>
>Your TextBox class would simply look something like this, which seems much simpler to me:
>
>
>    public class MattsTextBox : OakLeaf.MM.Main.Windows.Forms.mmTextBox
>    {
>        public MattsTextBox()
>        {
>            this.BindingSourceMember = null;
>            this.ControlID = new System.Guid("00000000-0000-0000-0000-000000000000");
>            this.Font = new System.Drawing.Font("Arial", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
>            this.ForeColor = System.Drawing.Color.Red;
>            this.Location = new System.Drawing.Point(0, 0);
>            this.Name = "mmTextBox1";
>            this.Size = new System.Drawing.Size(100, 22);
>            this.TabIndex = 0;
>        }
>    }
>}
>
>
>
>~~Bonnie
>
>
>
>
>>In that post, you said you can't do it visually, but I noticed that if I choose "Component Class" when adding a new item to my project, I get a deisnger surface that I can drag a base class onto, then I seem to be off and running visually.
>>
>>I get custom code in the Component.cs file, and generated code in the Component.Designer.cs that seems to handle all my defaults that I set in the properties window. This is certainly more appealing to me (the properties window).
>>
>>If this is not the purpose of a Component Class template, please help me understand what it it.
>>
>>
>>Here is the code-behind of the Component.cs file
>>
>>using System;
>>using System.Collections.Generic;
>>using System.ComponentModel;
>>using System.Diagnostics;
>>using System.Linq;
>>using System.Text;
>>
>>namespace lmforms
>>{
>>    public partial class Component2 : Component
>>    {
>>        public Component2()
>>        {
>>            InitializeComponent();
>>        }
>>
>>        public Component2(IContainer container)
>>        {
>>            container.Add(this);
>>
>>            InitializeComponent();
>>        }
>>    }
>>}
>>
>>
>>
>>And here is the genereted code in the Component.Designer.cs file:
>>
>>namespace lmforms
>>{
>>    partial class Component2
>>    {
>>        /// <summary>
>>        /// Required designer variable.
>>        /// </summary>
>>        private System.ComponentModel.IContainer components = null;
>>
>>        /// <summary>
>>        /// Clean up any resources being used.
>>        /// </summary>
>>        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
>>        protected override void Dispose(bool disposing)
>>        {
>>            if (disposing && (components != null))
>>            {
>>                components.Dispose();
>>            }
>>            base.Dispose(disposing);
>>        }
>>
>>        #region Component Designer generated code
>>
>>        /// <summary>
>>        /// Required method for Designer support - do not modify
>>        /// the contents of this method with the code editor.
>>        /// </summary>
>>        private void InitializeComponent()
>>        {
>>            this.mmTextBox1 = new OakLeaf.MM.Main.Windows.Forms.mmTextBox();
>>            //
>>            // mmTextBox1
>>            //
>>            this.mmTextBox1.BindingSourceMember = null;
>>            this.mmTextBox1.ControlID = new System.Guid("00000000-0000-0000-0000-000000000000");
>>            this.mmTextBox1.Font = new System.Drawing.Font("Arial", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
>>            this.mmTextBox1.ForeColor = System.Drawing.Color.Red;
>>            this.mmTextBox1.Location = new System.Drawing.Point(0, 0);
>>            this.mmTextBox1.Name = "mmTextBox1";
>>            this.mmTextBox1.Size = new System.Drawing.Size(100, 22);
>>            this.mmTextBox1.TabIndex = 0;
>>
>>        }
>>
>>        #endregion
>>
>>        private OakLeaf.MM.Main.Windows.Forms.mmTextBox mmTextBox1;
>>    }
>>}
>>
>>
>>
>>>>Can you also go and read my thread# 1311814?
>>>
>>>I already have ... John pointed you to another post of mine with the same suggestions I would make all over again. Did that not help?
>>>
>>>~~Bonnie
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform