c# - 错误非静态字段方法或属性“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);
}
}
解决方案
不要使用类名。您必须使用已创建类的实例。使用关键字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);
}
推荐阅读
- angular - ESLint prefer-const 规则声明变量应该是 Const,即使它被重新分配
- java - 如何在像facebook这样的网格中显示多个图像并链接
- java - 使用休眠/弹簧引导连接 3 个表
- html - 为什么没有填满,但宽度和高度都为 100%
- excel - 在宏上输入月份
- java - 不了解病情
- javascript - 我在 asp.net 核心中使用数据表,它工作正常,但有些事情没有正常进行。例如我的分页只是一个链接
- amazon-web-services - AWS 中使用的服务列表
- javascript - 登录react-redux后如何重定向到家?
- cloud-init - 自定义用户执行云初始化