Xyro
Xyro

Reputation: 227

Custom control problem with variables

I am creating a custom control for the first time and I have this as a part of the code :

    protected Pen pen;
    protected Color lineColor = Color.Green;
    protected double xMin = -10, xMax = 10, yMin = -10, yMax = 10;
    protected double[] data;

    public Graph()
    {
        InitializeComponent();
        pen = new Pen(new SolidBrush(lineColor), 1);
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        if(yMin <= 0 || yMax >= 0)
        {
            double yDifference = Math.Abs(yMin) + Math.Abs(yMax);
            double xAxisHeight = (Math.Abs(yMin) / yDifference) * Height;

            e.Graphics.DrawLine(pen, new Point(0, (int)xAxisHeight), new Point(Width, (int)xAxisHeight));
        }
    }

But for some reason xMin, xMax, yMin and yMax are always 0 in the onPaint function.

Edit :

Here is the rest of the code from Graph.cs

   The only this I wasn't showing was this : 

        public double XMin
    {
        get { return xMin; }
        set
        {
            xMin = value;
            this.Invalidate();
        }
    }
    public double XMax
    {
        get { return xMax; }
        set
        {
            xMax = value;
            this.Invalidate();
        }
    }
    public double YMin
    {
        get { return yMin; }
        set
        {
            yMin = value;
            this.Invalidate();
        }
    }
    public double YMax
    {
        get { return yMax; }
        set
        {
            yMax = value;
            this.Invalidate();
        }
    }

    public double[] Data
    {
        get { return data; }
        set
        {
            data = value;
            this.Invalidate();
        }
    }

    public Color LineColor
    {
        get { return lineColor; }
        set
        {
            lineColor = value;
            pen = new Pen(new SolidBrush(lineColor), 1);
            this.Invalidate();
        }
    }

Upvotes: 1

Views: 166

Answers (3)

Daniel Pryden
Daniel Pryden

Reputation: 60947

jimmy_keen's answer is correct. If you have any public properties on your control, the designer will provide a UI for configuring them. If you don't configure them, then the designer will set all the properties to their default values -- which in this case would be default(int) (that is, 0), not the value that you initially set it to in your code.

But the solution isn't to just edit the code in the *.Designer.cs files directly, because Visual Studio will just put it back. Instead, go to the properties for your custom control in the Designer and set some useful values.

Or, if you really don't want to be able to configure your control using the designer, you can use code like the following:

[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[Browsable(false)]
public double XMin
{
    get { return xMin; }
    set
    {
        xMin = value;
        this.Invalidate();
    }
}

See the documentation on DesignerSerializationVisibilityAttribute for details.

Upvotes: 0

k.m
k.m

Reputation: 31454

Designer most likely sets your properties to default value, which is 0.

Try using DefaultValue attribute, just make sure both values (the one you initialize property backing field with and default one are the same):

private double xMin = -10;

[DefaultValue(-10)]
public double XMin
{
    get { return xMin; }
    set
    {
        xMin = value;
        this.Invalidate();
    }
}

Upvotes: 1

Joshua
Joshua

Reputation: 552

If the designer is setting them back to zero, after Initialize components set the values. For instance

InitializeComponent();
xMin = -10;
xMax = 10; 
yMin = -10;
yMax = 10;

because it would be setting them to 0 inside the initialize components, which should be after the ones outside the constructor.

Upvotes: 0

Related Questions