自定义控件变量问题

发布于 2024-11-09 07:41:09 字数 1903 浏览 0 评论 0 原文

我第一次创建自定义控件,并将其作为代码的一部分:

    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));
        }
    }

但由于某种原因,onPaint 函数中 xMin、xMax、yMin 和 yMax 始终为 0。

编辑:

这是 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();
        }
    }

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();
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情何以堪。 2024-11-16 07:41:10

jimmy_keen 的答案是正确的。如果控件上有任何公共属性,设计器将提供用于配置它们的 UI。如果您不配置它们,那么设计器会将所有属性设置为其默认值 - 在本例中为 default(int) (即 0),而不是您最初在代码中设置的值。

但解决方案并不是直接编辑 *.Designer.cs 文件中的代码,因为 Visual Studio 会将其放回去。相反,请转到设计器中自定义控件的属性并设置一些有用的值。

或者,如果您确实不想使用设计器配置控件,则可以使用如下代码:

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

请参阅 DesignerSerializationVisibilityAttribute 了解详细信息。

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.

可可 2024-11-16 07:41:10

如果设计者将它们设置回零,请在初始化组件后设置这些值。例如

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

,因为它将在初始化组件内将它们设置为 0,这应该位于构造函数外部的组件之后。

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.

执笏见 2024-11-16 07:41:09

设计器很可能将您的属性设置为默认值,即 0。

尝试使用 DefaultValue 属性,只需确保两个值(初始化属性支持字段的值和默认值相同):

private double xMin = -10;

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

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();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文