首页 > 解决方案 > 错误非静态字段方法或属性“System.Windows.Forms.Control.Width.get”需要对象引用

问题描述

我正在尝试弯曲登录表单的边框,当我将鼠标悬停在“Form2.Width”上时,它显示错误“非静态字段方法或属性'System.Windows.Forms.Control. Width.get'",当我运行程序时它当然不会弯曲。

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }


        private void Form2_Load(object sender, EventArgs e)
        {
GraphicsPath newgraph = new GraphicsPath();
            newgraph.StartFigure();
            newgraph.AddArc(new Rectangle(0, 0, 10, 10), 180, 90);
            newgraph.AddLine(10, 0, Form.Width - 20, 0);
            newgraph.AddArc(new Rectangle(Form2.Width - 10, 0, 10, 10), -90, 90);
            newgraph.AddLine(Form2.Width, 20, Form2.Width, Form2.Height - 10);
            newgraph.AddArc(new Rectangle(Form2.Width - 10, Form2.Height - 10, 10, 10), 0, 90);
            newgraph.AddLine(Form2.Width - 10, Form2.Height, 20, Form2.Height);
            newgraph.AddArc(new Rectangle(0, Form2.Height - 10, 10, 10), 90, 90);
            newgraph.CloseAllFigures();
            Form2.Region = new Region(newgraph);
              }
        }

标签: c#

解决方案


不要使用类名。您必须使用已创建类的实例。使用关键字this.Width,您可以访问表单本身

private void Form2_Load(object sender, EventArgs e)
{
   GraphicsPath newgraph = new GraphicsPath();
   newgraph.StartFigure();
   newgraph.AddArc(new Rectangle(0, 0, 10, 10), 180, 90);
   newgraph.AddLine(10, 0, this.Width - 20, 0);
   newgraph.AddArc(new Rectangle(this.Width - 10, 0, 10, 10), -90, 90);
   newgraph.AddLine(this.Width, 20, this.Width, this.Height - 10);
   newgraph.AddArc(new Rectangle(this.Width - 10, this.Height - 10, 10, 10), 0, 90);
   newgraph.AddLine(this.Width - 10, this.Height, 20, this.Height);
   newgraph.AddArc(new Rectangle(0, this.Height - 10, 10, 10), 90, 90);
   newgraph.CloseAllFigures();
   this.Region = new Region(newgraph);
}

推荐阅读