How to you set the default properties for custom controls, i.e. when they are first dragged onto the form in the Designer?
Can't find an answer here or via Google; all I get is how to constrain the values.
Using Width & Height as examples, if I set them in the constructor they get applied to the control everytime the Designer is opened. How do I set them to a default which is never applied again after the user changes the properties?
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.
C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.
Try using the DefaultValue attribute.
private int height;
[DefaultValue(50)]
public int Height
{
get
{
return height;
}
set
{
height=value;
}
}
What worked for me for properties that I can't override is using the new
operator. For example, the MultiSelect
property on a ListView
control. I want MultiSelect
to default to false
, but I still want to be able to change it.
If I just set it to false
in the constructor, or in InitializeComponent
, the problem (I think) is that the default value is still true
, so if I set the value to true
in the designer, the designer notices that true
is the default, and so just doesn't set the property at all rather than explicitly setting it to what it thinks is already the default. But then the value ends up being false
instead, because that is what is set in the constructor.
To get around this issue I used the following code:
/// <summary>Custom ListView.</summary>
public sealed partial class DetailsListView : ListView
{
...
[DefaultValue(false)]
public new bool MultiSelect {
get { return base.MultiSelect; }
set { base.MultiSelect = value; }
}
This allows the control to still have a functioning MultiSelect
property that defaults to false
rather than true
, and the property can still be toggled on the new control.
EDIT: I encountered an issue having to do with using abstract forms. I've been using abstract form classes, with a concrete implementation that I switch to when I need to use the designer. I found that when I switched the class that I was inheriting from that the properties on my custom control would reset to the old defaults. I seem to have corrected this behaviour by setting properties to their defaults in the constructor of the custom control.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With