自定义控件构造函数

发布于 2024-11-09 09:18:33 字数 1167 浏览 0 评论 0原文

请原谅我的故事很长,但我认为这个问题值得提出。

我制作了一个自定义控件,它有自己的重写 OnPaintBackground 方法,该方法使用成员 BrushPen。我是这样创建的:

CustomControl c = new CustomControl();
c.Parent = someParent;

构造函数只是默认的构造函数,添加了一些用于创建画笔和钢笔以及背景内容的内容。

但是,我认为最好有一个采用父面板的构造函数(因为没有父面板就无法使用我的控件),因此我向构造函数添加了一个参数:

public CustomControl(Control parent) {
    InitializeComponent();
    Parent = parent;
    // do a bunch of stuff like getting brushes, pens, etc for drawing the background
}

然后,当我运行程序时,第一个控件是创建和绘制精美。然后,对于第一个控件之后的所有控件,我在 System.Drawing.dll 中遇到了第一次机会异常,并且我的控件内的所有面板的背景都是红色的< code>X,Winforms 在找不到图像或其他内容时显示的内容。请记住,要创建的控件的第一个实例运行良好。

因此,我将 Visual Studio 设置为在引发任何异常时中断,而不是仅仅记录它,并且它在我的重写 OnPaintBackground 内的一行上中断,如下所示:

e.Graphics.DrawLine(bPen, x, y, Width, Height);

有了信息 Argument can be空。因此,我查看了调试器窗口,发现不仅 bPen 为 null,而且我所有的画笔和笔以及所有内容都为 null,即使正在调用构造函数(使用 MessageBox 进行验证)!

我最终通过从默认构造函数中删除参数并添加另一个带有参数的单独构造函数并从该构造函数调用默认构造函数来解决了该问题。但是,我想知道,C# 对于没有默认构造函数的控件有什么限制,导致它们无法创建像 Pen 这样的东西?

Pardon me for this long story, but I think the question merits it.

I have a custom control that I made, which had it's own overridden OnPaintBackground method which used a member Brush and Pen. I was creating it like this:

CustomControl c = new CustomControl();
c.Parent = someParent;

and the constructor was just the default one with some added stuff for creating brushes and pens and stuff for the background.

However, I thought it would be nicer to have a constructor that took the parent panel (since my control can't be used without a parent) so I added one parameter to the constructor:

public CustomControl(Control parent) {
    InitializeComponent();
    Parent = parent;
    // do a bunch of stuff like getting brushes, pens, etc for drawing the background
}

Then when I ran my program, the first control was created and drawn fine. Then for all the controls after the first one, I was getting first chance exceptions in System.Drawing.dll and the backgrounds of all the panels inside my control were that red X that Winforms shows when it can't find an image or something. Remember though that the first instance of the control to be created was working perfectly.

So I set my Visual Studio to break when any exceptions were thrown instead of just logging it, and it broke on a line inside my overriden OnPaintBackground that looked like this :

e.Graphics.DrawLine(bPen, x, y, Width, Height);

With the information Argument cannot be null. So I looked in the debugger window and found that not only was bPen null, but all my brushes and pens and everything was null even though the constructor was being called (verified with a MessageBox)!

I eventually solved the problem by removing the parameter from the default constructor and adding another seperate constructor that took an argument, and calling the default ctor from that one. However, I would like to know, what does C# have against controls not having a default constructor that makes them not able to create stuff like Pens?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

時窥 2024-11-16 09:18:33

这个问题有一些有用的答案:C# 中带有参数的'UserControl'构造函数

There's some useful answers to this question: 'UserControl' constructor with parameters in C#

兔小萌 2024-11-16 09:18:33

C# 不反对控件,它不在乎。这只是一种语言。通常您会需要一个默认构造函数,因为设计器依赖它们。事实上,这样的事情对我来说效果很好:

public class MyControl : Control
{
    private Control _parent;
    public MyControl()
    {
        InitializeComponent();
    }

    public MyControl(Control parent) : this()
    {
        _parent = parent;
    }
}

C# has nothing against controls, it doesn't care. It's just a language. Usually you will want a default constructor because the designer relies on them. In fact, something like this works just fine for me:

public class MyControl : Control
{
    private Control _parent;
    public MyControl()
    {
        InitializeComponent();
    }

    public MyControl(Control parent) : this()
    {
        _parent = parent;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文