Click here to Skip to main content
15,886,963 members
Home / Discussions / C#
   

C#

 
GeneralRe: in C#.net Pin
Pete O'Hanlon13-Aug-12 3:14
mvePete O'Hanlon13-Aug-12 3:14 
GeneralRe: in C#.net Pin
Joan M13-Aug-12 3:13
professionalJoan M13-Aug-12 3:13 
Questionusing Same Shortcut keys on Different Tabs of a form Pin
Why so Serious13-Aug-12 2:48
Why so Serious13-Aug-12 2:48 
AnswerRe: using Same Shortcut keys on Different Tabs of a form Pin
Shameel13-Aug-12 4:29
professionalShameel13-Aug-12 4:29 
AnswerRe: using Same Shortcut keys on Different Tabs of a form Pin
Liam Cairns Kelly13-Aug-12 9:30
Liam Cairns Kelly13-Aug-12 9:30 
Questionread and org data in c# Pin
Xarzu12-Aug-12 23:42
Xarzu12-Aug-12 23:42 
AnswerRe: read and org data in c# Pin
Eddy Vluggen13-Aug-12 0:33
professionalEddy Vluggen13-Aug-12 0:33 
QuestionOverriding the DefaultValueAttribute of an inherited property Pin
Bernhard Hiller12-Aug-12 20:51
Bernhard Hiller12-Aug-12 20:51 
Let's look at a simple example: I want to create a Button class with a different BackColor and Size than the "normal" button (.Net 4, Windows Forms). Looks easy, doesnt't it? But I face some problems.
A simple "solution" would be:
C#
public class BernieButton : Button
..
public BernieButton()
{
    this.BackColor = Color.Green;
    this.Size = new Size(150,50);
}
Compile the project, drag a BernieButton onto Form1, and everything looks fine.

But now Sales decides that the BackCOlor must be yellow, and the size is too big. OK, change that in the constructor, test the new button, looks fine. Rebuild the old projects, and - the buttons there look like before. Failure!

How could that fail? When the button was dragged onto the form, the Designer added some lines in the InitializeComponent method of the form:
C#
this.BernieButton1.BackColor = Color.Green;
this.ernieButton1.Size = new Size(150,50);
and that code gets executed after the constructor of the button, thus overriding the recent changes of the default value.

I tried overriding the properties:
C#
[DefaultValue(typeof(Color), "Green")]
public override Color BackColor
{
    get { return base.BackColor; }
    set { base.BackColor = value; }
}
and then set the default value in the constructor (using reflection, thus circumventing double storage of the new default value). That really works as it is intended, the designer does not write a line for BackColor in the InitializeComponent method.

But it does not work with Size. There is an important difference: BackColor is "virtual" in the Button class, Size is not.

I tried with the "new" keyword, but it fails: a line is added to InitializeComponent, and later changes of the default value are ignored.

I thought of setting the DefaultValueAttribute of the properties in the constructor. But that is not possible, attributes are read-only at run-time.

Do you have some ideas on how to overcome these limitations with inherited non-virtual properties?
AnswerRe: Overriding the DefaultValueAttribute of an inherited property Pin
DaveyM6913-Aug-12 0:19
professionalDaveyM6913-Aug-12 0:19 
GeneralRe: Overriding the DefaultValueAttribute of an inherited property Pin
Bernhard Hiller13-Aug-12 1:01
Bernhard Hiller13-Aug-12 1:01 
GeneralRe: Overriding the DefaultValueAttribute of an inherited property Pin
DaveyM6913-Aug-12 1:27
professionalDaveyM6913-Aug-12 1:27 
QuestionA simple proxy server in C# Pin
Prahlad Yeri12-Aug-12 1:41
Prahlad Yeri12-Aug-12 1:41 
AnswerRe: A simple proxy server in C# Pin
Richard Andrew x6412-Aug-12 4:17
professionalRichard Andrew x6412-Aug-12 4:17 
GeneralRRe: A simple proxy server in C# Pin
Prahlad Yeri12-Aug-12 5:07
Prahlad Yeri12-Aug-12 5:07 
GeneralRe: RRe: A simple proxy server in C# Pin
Richard Andrew x6412-Aug-12 5:25
professionalRichard Andrew x6412-Aug-12 5:25 
GeneralRe: RRe: A simple proxy server in C# Pin
Prahlad Yeri12-Aug-12 5:31
Prahlad Yeri12-Aug-12 5:31 
GeneralRe: RRe: A simple proxy server in C# Pin
Richard Andrew x6412-Aug-12 5:36
professionalRichard Andrew x6412-Aug-12 5:36 
GeneralRe: RRe: A simple proxy server in C# Pin
Prahlad Yeri12-Aug-12 6:18
Prahlad Yeri12-Aug-12 6:18 
QuestionTracking system Pin
kipchirchir11-Aug-12 20:52
kipchirchir11-Aug-12 20:52 
AnswerRe: Tracking system Pin
OriginalGriff11-Aug-12 21:17
mveOriginalGriff11-Aug-12 21:17 
AnswerRe: Tracking system Pin
Richard MacCutchan11-Aug-12 21:46
mveRichard MacCutchan11-Aug-12 21:46 
QuestionXDocument.Save() reversing attribute order Pin
Nukeman3311-Aug-12 2:03
Nukeman3311-Aug-12 2:03 
QuestionRe: XDocument.Save() reversing attribute order Pin
Eddy Vluggen11-Aug-12 2:22
professionalEddy Vluggen11-Aug-12 2:22 
AnswerRe: XDocument.Save() reversing attribute order Pin
Dave Kreskowiak11-Aug-12 4:13
mveDave Kreskowiak11-Aug-12 4:13 
AnswerRe: XDocument.Save() reversing attribute order Pin
jschell11-Aug-12 7:35
jschell11-Aug-12 7:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.