Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Overriding properties in subclasses
Message
From
19/07/2006 11:52:33
 
General information
Forum:
ASP.NET
Category:
Class design
Miscellaneous
Thread ID:
01137567
Message ID:
01137657
Views:
24
Chad,

If you want the DropDownStyle to always be DropDownList, then set it in the constructor of the class rather than try to override the property:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace MyCompany.WinUI.MyClasses
{
    public class MyComboBox : System.Windows.Forms.ComboBox
    {
        public MyComboBox()
        {
            // Override settings with new default values in the constructor.
            this.DropDownStyle = ComboBoxStyle.DropDownList;
        }
    }
}
~~Bonnie



>Seems like this should be an easy one. I have the code below (sample code from Bonnie Berent) to create a one-off class. Now, if I want to change the default setting for the DropDownStyle, how do I do that?
>
>using System;
>using System.Drawing;
>using System.Collections;
>using System.ComponentModel;
>using System.Windows.Forms;
>using System.Data;
>
>namespace MyCompany.WinUI.MyClasses
>{
>    public class MyComboBox : System.Windows.Forms.ComboBox
>    {
>        // Override inherited settings with new default values.
>        public new ComboBoxStyle DropDownStyle = ComboBoxStyle.DropDownList;
>    }
>}
>
>Without the new keyword, the compiler tells me I'm hiding an inherited member and if that is the intended behavior, I should use new. So, while this compiles fine, when I try to use this control on a WinForm in another project, that property still defaults to DropDown (not DropDownLlist). I'm sure it has to be something simple I'm missing. By the way, I built this in a class project instead of a component project, does that make a difference?
>
>TIA,
>Chad
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform