自定义控件变量问题
我第一次创建自定义控件,并将其作为代码的一部分:
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
jimmy_keen 的答案是正确的。如果控件上有任何公共属性,设计器将提供用于配置它们的 UI。如果您不配置它们,那么设计器会将所有属性设置为其默认值 - 在本例中为
default(int)
(即0
),而不是您最初在代码中设置的值。但解决方案并不是直接编辑
*.Designer.cs
文件中的代码,因为 Visual Studio 会将其放回去。相反,请转到设计器中自定义控件的属性并设置一些有用的值。或者,如果您确实不想使用设计器配置控件,则可以使用如下代码:
请参阅
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:
See the documentation on
DesignerSerializationVisibilityAttribute
for details.如果设计者将它们设置回零,请在初始化组件后设置这些值。例如
,因为它将在初始化组件内将它们设置为 0,这应该位于构造函数外部的组件之后。
If the designer is setting them back to zero, after Initialize components set the values. For instance
because it would be setting them to 0 inside the initialize components, which should be after the ones outside the constructor.
设计器很可能将您的属性设置为默认值,即 0。
尝试使用
DefaultValue
属性,只需确保两个值(初始化属性支持字段的值和默认值相同):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):