我有一个从两点创建LineShape的代码。
class MyMenu
{
public static void AddLine()
{
ShapeContainer canvas = new ShapeContainer();
LineShape theLine = new LineShape();
canvas.Parent = this;
theLine.Parent = canvas;
theLine.BorderColor = SystemColors.ControlDarkDark;
theLine.StartPoint = new System.Drawing.Point(-3, 154);
theLine.EndPoint = new System.Drawing.Point(212, 154);
}
}
。我想创建一个类,并从那里使用,但我最终有一个错误。
Keyword 'this' is not valid in a static property, static method, or static field initializer
我试着像这样修理它,但没有!
Form1 MyForm = new Form1();
canvas.Parent = MyForm;
谢谢!
答案 0 :(得分:1)
我不确定,您可以尝试将表单引用传递给方法(如果这是您的意思)。
class MyMenu
{
public static void AddLine(Form f)
{
ShapeContainer canvas = new ShapeContainer();
LineShape theLine = new LineShape();
canvas.Parent = f;
theLine.Parent = canvas;
theLine.BorderColor = SystemColors.ControlDarkDark;
theLine.StartPoint = new System.Drawing.Point(-3, 154);
theLine.EndPoint = new System.Drawing.Point(212, 154);
}
}
从表格:
MyMenu.AddLine(this);