我正在开发这个自定义控件。 (我对这部分编程非常陌生。)我正在开发一个应用程序,当用户在我自己的自定义控件中输入输入时,该应用程序必须能够格式化数学表达式。这就是我想要的控件的样子(此图像是在 Photoshop 中制作的):
我不会解释我希望它具有的行为,因为这对您没有帮助,但其想法是它不基于任何 Windows 控件。
这是我已有的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Support.Components
{
public partial class PartialExpressionEditor : Control
{
public PartialExpressionEditor()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
Brush background = Brushes.White;
pe.Graphics.FillRectangle(background, ClientRectangle);
background.Dispose();
}
}
}
当我尝试将其放入表单中时,出现此错误对话框:
问题出在哪里?或者说为什么会出现这个错误?
最佳答案
我认为问题在于您正在处理系统刷:
// background.Dispose();
因为你没有创建它:
Brush background = Brushes.White;
要使用您自己处理的刷子:
using (SolidBrush br = new SolidBrush(Color.White)) {
pe.Graphics.FillRectangle(br, this.ClientRectangle);
}
您可能必须退出 Visual Studio 才能恢复 Brushes.White 画笔。
关于c# - 自定义控件错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17751037/