组合多个控件的控件子类 [英] sub-class of Control that combines multiple Controls

查看:46
本文介绍了组合多个控件的控件子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试扩展TextBox,以在其左侧添加一个Label并将其视为一个控件,因此不必跟踪它们的大小,位置等.

I'm trying to extend TextBox to add a Label to the left of it and treat it as one Control so I don't have to keep track of both of their sizes, locations, etc.

我创建了一个TextBoxWithLabel类,该类扩展了Control并具有TextBox和Label字段,但是我不确定如何对onPaint()进行操作-我必须告诉它手动绘制两个项目吗?如果是这样,怎么办?我猜默认的继承行为不会达到检查是否包含任何子控件,如果包含子控件,则绘制它们"……

I've created a TextBoxWithLabel class that extends Control and has TextBox and Label fields, but I'm not really sure what to do for onPaint() - do I have to tell it to manually draw both items? If so, how? I'm guessing the default inherited behaviour doesn't go so far as 'check if I contain any child Controls and if I do, draw them'...

这甚至是最好的方法吗?我以前有我的类扩展TextBox并只是添加了Label字段,但是当然没有将其添加到包含TextBoxWithLabel的面板中,因此没有绘制.

Is this even the best way to do it? I previously had my class extend TextBox and just added the Label field, but of course that didn't get added to the Panel containing the TextBoxWithLabel and so wasn't drawn.

对任何建议或正确方向的建议表示赞赏.

Any suggestions or pokes in the right direction appreciated.

谢谢

亚历克斯

推荐答案

此处的典型方法是在UserControl中放置标签和文本框.但是,这很痛苦,您必须将许多文本框的属性和事件添加到用户控件中,以便它至少类似于文本框.丑陋的样板代码.

The typical approach here is a UserControl in which you put both the label and the text box. It is painful though, you have to add a lot of the properties and events of the text box to the user control so it at least resembles a text box. Ugly boilerplate code.

另一种方法是制作一个自定义文本框,使其潜入父级的标签控件中.这完全像一个TextBox,而无需做任何工作.在您的项目中添加一个新类,并粘贴以下代码.编译.将新控件从工具箱的顶部拖放到您的窗体上.将Description属性设置为您希望看到的文本显示在标签中.

Another way to do it is to make a custom text box that sneaks in a label control on the parent. That completely behaves like a TextBox without having to do any work. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form. Set the Description property to the text you want to see appear in the label.

using System;
using System.Drawing;
using System.Windows.Forms;

class MyTextBox : TextBox {
    private Label label;
    public MyTextBox() {
        label = new Label();
        label.AutoSize = true;
        label.Font = this.Font;
        label.Location = this.Location;
        label.Resize += new EventHandler(label_Resize);
    }
    protected override void OnParentChanged(EventArgs e) {
        // Keeps label on the same parent as the text box
        base.OnParentChanged(e);
        label.Parent = this.Parent;   // NOTE: no dispose necessary
    }

    private void moveLabel() {
        // Keep label right-aligned to the left of the text box
        label.Location = new Point(this.Left - label.Width - 10, this.Top);
    }
    private void label_Resize(object sender, EventArgs e) {
        moveLabel();
    }
    protected override void OnLocationChanged(EventArgs e) {
        base.OnLocationChanged(e);
        moveLabel();
    }

    public string Description {
        get { return label.Text; }
        set { label.Text = value; }
    }

    public override Font Font {
        get { return base.Font; }
        set { base.Font = label.Font = value; }
    }

}

这篇关于组合多个控件的控件子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆