简体   繁体   English

在C#ToolBox中派生两个扩展控件,一个包含另一个

[英]Derive Two Extended Control One inside Another in C# ToolBox

I have two controls inside my Class Library one named TextBox and one Panel, I am able to derive them differently in my ToolBox but what I want is instead of deriving two different controls I want my textbox inside the panel and derive just panel so that it contains both panel and textbox. 我的类库中有两个控件,一个分别名为TextBox和一个Panel,我可以在ToolBox中以不同的方式派生它们,但是我想要的是代替派生两个不同的控件,我希望我的文本框在面板中并派生仅面板,这样包含面板和文本框。 I am facing an issue while doing it. 我在执行时遇到问题。 Panel is easily available in Toolbox but the textbox object I created inside the Panel is not visible. 面板在工具箱中很容易使用,但是我在面板内创建的文本框对象不可见。 Can anyone tell me is this thing achievable? 谁能告诉我这件事是可以实现的吗?

public class TextBoxPanel :Panel
{
    TextBoxPanel()
    {
        IpTextBox ip = new IpTextBox();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
    }

    public class IPField : TextBox
    {
        protected override void OnClick(EventArgs e)
        {

        }
    }
}

You can't extend from multiple base classes. 您不能从多个基类扩展。 Your intuition to derive from your Panel is correct since you want a TextBox inside a Panel . Panel派生的直觉是正确的,因为您想要在Panel使用TextBox

The big question is WHY do you "need" to derive from TextBox ? 最大的问题是您为什么“需要”从TextBox派生? If you just need a TextBox inside your new control then simply adding an instance of it to your Panel is enough. 如果仅在新TextBox内需要一个TextBox则只需将其实例添加到Panel中就足够了。

If the TextBox provides additional functionality through an API which you want to expose from the new TextBoxPanel then I suggest something along these lines: 如果TextBox通过API提供了其他功能,而您想从新的TextBoxPanel公开该API,那么我建议按照以下方式进行操作:

public interface ICoolStuff
{
   public String HelloWorld();
}

public class TextBox : Control, ICoolStuff
{
    // *snip*
}

public class TextBoxPanel : Panel, ICoolStuff
{
    public string HelloWorld()
    {
        return this.textbox.HelloWorld();
    }
}

IF the functionality your textbox provides is not dependent on the textbox itself then it would be even better to separate it into a completely different class 如果您的文本框提供的功能不依赖于文本框本身,那么最好将其分为一个完全不同的类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM