I am trying to set a custom C# control property.
Here's my code:
/* Cancel's button text */
[Category("ComboTouch"),
Description("Text to display in cancel button"),
DefaultValue("Cancel")]
public String ct_cancelButtonText { get; set; }
I can get the property when I use the customized control in other projects (as you can see in the image); but configuration parameter DefaultValue seems not to work.
Could anybody help me? Thank you very much.
01/10/13 Update. Thank you very much for your answers, you solved my problem.
I would like to share how I finally could set the default value automatically:
private String m_cancelButtonText="Cancel";
/* Cancel's button text */
[Category("ComboTouch"),
Description("Text to display in cancel button"),
DefaultValue("Cancel")]
public String ct_cancelButtonText
{
get
{
return m_cancelButtonText;
}
set
{
m_cancelButtonText = value;
}
}
One curiosity: please check the format of 'Cancel' text. If I set DefaultValue type; it looks like normal text. But if I don't, it looks like bold text. I know it's silly; but I would like to know why it is that way. Thank you.
As noted in documentation:
A DefaultValueAttribute will not cause a member to be automatically initialized with the attribute's value. You must set the initial value in your code.
Related
I am creating several .NET User Controls and I am trying to figure out the best way to go about setting properties. I have an address control and I am trying to create a property called ShowCountry which will either hide or show the control's country ddl.
I have been trying to set most of my properties similar to the below code:
public bool ShowCountry
{
get { return (bool)ViewState["ShowCountry"]; }
set
{
ViewState["ShowCountry"] = value;
pnlCountry.Visible = value;
}
}
How would I set a default value for this property? When I run my page with the control on it, it instantly errors out in the "get{}" when ShowCountry is used in one of my functions because I never set ShowCountry="false" in the control's tag. If I set this property when declaring the control everything works fine.
Also is what I am doing with the ViewState a good way to keep property values across postbacks?
Could someone show me how they would write this property?
The specs are:
Must keep value across postbacks, Must default to false
you can try this in order to avoiding error..
public bool ShowCountry
{
get {
if(ViewState["ShowCountry"] != null ){
return (bool)ViewState["ShowCountry"];
}
else {
//return the default value
return false;
}
}
set
{
ViewState["ShowCountry"] = value;
pnlCountry.Visible = value;
}
}
i think view state is best approach alternatively you can use hidden field in order to save value against post back.
I'm using DevExpress.
In my project i have control (textEdit), which EditValue is binded to the property of "int" type.
Problem is that control allow to enter only numbers.
My task is: while form is in edit mode, the textEdit should display word "Automatic", and only after safe button press there should be generated number.
Now in edit mode textbox shows "0", is it possible to make it show "Automatic" in case of "0".
there is the property, to which the textBox is binded:
int fEventNr;
public int EventNr {
get { return fEventNr; }
set { SetPropertyValue<int>("EventNr", ref fEventNr, value); }
}
everything works except that it shows "0" and I don't know how to make him show "automatic"
maybe someone has any ideas?
This is a solution to your problem:
textEdit1.Properties.CustomDisplayText += new Properties_CustomDisplayText;
void Properties_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
{
if (yourCondition)
e.DisplayText = "Automatic";
}
txtEdit.Properties.NullText = "Automatic";
txtEdit.EditValue = null;
Consider changing public int EventNr to public int? EventNr so that you can be sure that the user HAS NOT supplied any value if the EditValue is null and you should generate it "Automatic"aly :)
I believe it's a bad practice to consider 0 as [value not set]. That is the reason why they invented the null.
On the properties panel go to Properties -> Mask . Set "MaskType" to RegEx and set "EditMask" to \d*. If you don't want integers to begin with zero(s) then set "EditMask" to [1-9]+\d* instead.
Alternatively you can do it by code :
this.textEditIntegersOnly.Properties.Mask.EditMask = "[1-9]+\\d*";
this.textEditIntegersOnly.Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.RegEx;
I am wondering why this code works fine and comiler doesnt generate any errors or warnings?
class Program
{
static int _value;
static int MyValue
{
get { return _value; }
set { _value = 5; }
}
static void Main()
{
Console.WriteLine(Program.MyValue); //This line print 0 (defoult value of int variables)and its normal
Program.MyValue = 10; //after calling the Set accssor we will see that
Console.WriteLine(Program.MyValue); //The result is 5
Console.ReadLine();
}
is this any usefull or special thing? or could it be a technic in Property initialization? thanks in advance.
EDIT:
It seems what we have here is a Readonly Property with defoult value am I right?
The set-accessor is nothing other than a method with a parameter, the "value"-parameter. It's up to the method what it does and what it does not.
void Set__MyValue(int value){
_value=5;
}
The code is valid C#, so wouldn't cause errors or warnings.
Code in getters and setters can be anything, so can't really be statically checked - even if you do something strange like ignore the value passed into the setter, that might be exactly the right thing for your code.
Since the set statement is actually just a convenient way of specifying a full method (i.e. set_MyValue(int value)) all the normal rules of functions apply. In particular, there is no obligation to use the parameters if you don't want to.
While your example is contrived, there are plenty of situations where it makes sense to ignore the value passed in. For example, suppose you wanted to make sure a particular string property was never null. You might do something like this:
private string _myProp = string.Empty;
public string MyProp
{
get
{
return _myProp;
}
set
{
if (value == null)
{
_myProp = string.Empty;
return;
}
_myProp = value;
}
}
We ignore value if we want to, and don't otherwise.
This code is pretty valid and also can be usefull in certain scenarious . If you immagine that you want raise an event/WPF command on _value changing, log that stuff, do any other meaningful for you stuff there. Having this kind of management setting the _value directly will cause "silent" assignment, setting this _value via property can cause notifications.
I will never do something like this honestly, cause it's confusional and not clear for someone who doesn't really understand your code, but by the way it's kind of "solution" too.
Regards.
The property will be converted as 2 different methods in IL(one for set the value and the other is to return the value) and ignoring a parameter is valid in a method!
You can check this by using the tool ildasm.exe(which comes with Visual Studio).
I'm new in C# but not new to coding --being doing it for almost two decades--, and have a problem with properties in a custom control I'm building, which inherits from a Panel. When I put my properties, I can see them in the Designer properties list and can even set them, but when running my little application, it seems these properties values are not used. The same if I change a property programatically: no error but my control does nothing, it is like they are not properly set. However, if I do it programatically whithin the class, they do work. My guess is that something in my properties set/get stuff is not right. Please see the following code chunk of how I'm doing it:
public class ColorStrip : Panel
{
// properties
// ------------------------------------------
// size of color clusters (boxes)
private int _clusterSize = 20;
// controls if show the buttons panel
private Boolean _showButtons;
// property setters/getters
// ------------------------------------------
// clusterSize...
public int clusterSize
{
get { return _clusterSize; }
set { _clusterSize = value; }
}
// showButtons...
public Boolean showButtons
{
get { return _showButtons; }
set { Console.Write(_showButtons); _showButtons = value; }
}
....
So in my form, for instance in the load or even in a click event somewhere, if I put colorStrip1.showButtons = false; or colorStrip1.showButtons = true; whatever (colorStrip1 would be the instance name after placing the control in the form in design mode)... console.write says always 'false'; Even if I set it in the design properties list as 'true' it will not reflect the settled value, even if I default it to true, it will never change externally. Any ideas? Non of the methods get the new and externally settled property value neither, obviously the getter/setter thing is not working. Seems to me I'm not doing right the way I set or get my properties outside the class. It works only inside it, as a charm...Any help...very appreciate!
Cheers
lithium
p.s. TO CLARIFY SOLUTION:
Setting the property in this case didn't work because I was trying to use a new set value within the constructor, which seems can't get the new values since it is, well, building the thing. If I change the property value in Design mode > Property editor or in code externally to the object, say in it's parent form's load event, it will change it but readable for all methods except the constructor, of course :)
It's likely an issue of the order of execution. Your property setter just sets a variable, but doesn't actually trigger anything on the control to update the state related to this variable (e.g. adding or showing the buttons I assume).
When you set the property befre the rest of the initialization is done, the value is being used, otherwise it isn't because during the initial go the default value is still the property value.
You need to act on the setter, here's some pseudocode to illustrate:
set {
_showButtons = value;
if (alreadyInitialized) {
UpdateButtons();
}
}
Note: make sure to first set the value, then act - otherwise you end up using the old value (just like your Console.Write() is doing).
The quoted code doesn't look problematic. Are you sure you're referencing the same instance of ColorStrip? Also, check your .Designer.cs file to ensure that the code setting the property is there.
In fact, try simplifying your code by using auto-implementing properties:
public int clusterSize { get;set;}
public Boolean showButtons {get;set;}
public ColorStrip() { ... clusterSize = 20; ... }
I've got a Windows Form User Control with a string property for setting the text of a textbox. This string can be multi-line.
I've noticed that on some controls with a text property, and instead of being forced to type in the single line property textbox, you get a little pop up where you can type multiple lines. (As a matter of fact, a Windows Forms Textbox control allows this on the Text property.)
How do I enable this functionality in the properties window for the property I have designed?
The following is not real code in my app, but an example of how such a property might be defined
public string Instructions
{
get
{
return TextBox1.Text;
}
set
{
TextBox1.Text = value;
}
}
You can use the EditorAttribute with a MultilineStringEditor:
[EditorAttribute(typeof(MultilineStringEditor),
typeof(System.Drawing.Design.UITypeEditor))]
public string Instructions
{
get
{
return TextBox1.Text;
}
set
{
TextBox1.Text = value;
}
}
To avoid adding a reference to System.Design and thus requiring the Full framework, you can also write the attribute like this:
[EditorAttribute(
"System.ComponentModel.Design.MultilineStringEditor, System.Design",
"System.Drawing.Design.UITypeEditor")]
Although this is less of a problem now that they've stopped splitting the framework into a Client Profile and a Full one.