c# - 自定义控件错误

标签 c# winforms custom-controls

我正在开发这个自定义控件。 (我对这部分编程非常陌生。)我正在开发一个应用程序,当用户在我自己的自定义控件中输入输入时,该应用程序必须能够格式化数学表达式。这就是我想要的控件的样子(此图像是在 Photoshop 中制作的):

enter image description here

我不会解释我希望它具有的行为,因为这对您没有帮助,但其想法是它不基于任何 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();
        }
    }
}

当我尝试将其放入表单中时,出现此错误对话框:

enter image description here

问题出在哪里?或者说为什么会出现这个错误?

最佳答案

我认为问题在于您正在处理系统刷:

// 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/

相关文章:

c# - 当依赖属性具有 RelativeSource 绑定(bind)时,GetTemplateChild 返回 null

c# - ASP.NET WebAPI - 如何扫描注册的操作

c# - 事件处理程序是否保证在 UI 线程上运行

windows - 快速像素绘图 C++ Windows 窗体

delphi - 我如何判断 Delphi 控件当前是否可见?

HTML5 视频 : hide custom controls when native ones are re-activated

c# - 使 WPF ListBox 逗号分隔值

c# - 您如何对发布者-订阅者模式进行单元测试?

c# - 如何使按钮文字变粗?

c# - 自定义控件 - 属性框中的可单击链接