Question

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 :)

Was it helpful?

Solution

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).

OTHER TIPS

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; ... }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top