简体   繁体   English

在设计时添加带有文本框的标签

[英]Add Label with Textbox at design time

I am creating a project using VS.NET (C#) with many forms that contain textboxes and associated labels. 我正在使用VS.NET(C#)创建一个包含许多形式的项目,这些形式包含文本框和关联的标签。 I have created the association through an exposed property I created for the textbox which contains the label name. 我已经通过为包含标签名称的文本框创建的暴露属性创建了关联。 The problem is that each time I add a textbox at design-time I have to add a label and then enter the label name into the property of the textbox. 问题在于,每次我在设计时添加文本框时,都必须添加标签,然后在文本框的属性中输入标签名称。 I would much rather do this dynamically at design-time when I create the textbox, much like the old VB textbox add. 我宁愿在设计时在创建文本框时动态地执行此操作,就像旧的VB文本框添加一样。 I have been scouring the net for a way to dynamically add a label whenever I add a textbox at design-time without finding any acceptable solutions. 我一直在寻找一种在设计时每当添加文本框时都动态添加标签而找不到任何可接受的解决方案的方法。 I found an answer on this site that suggested adding a user control containing a textbox and label, and though it is probably the best solution I have found, I think it restricts me more than I would like. 我在此站点上找到了一个建议添加一个包含文本框和标签的用户控件的答案,尽管这可能是我找到的最佳解决方案,但我认为它对我的限制超出了我的期望。 Do I have to go through some full-blown custom designer to do this hopefully simple task? 我是否需要经过成熟的定制设计师来完成这项希望简单的任务?

TIA TIA

Although I like the solution that uses UserControl better (simpler and easier to handle), but there may be some cases where not creating one more thing that will eat the resources is preferable (for example if you need a lot of such label-textbox pairs on one form). 虽然我喜欢更好地使用UserControl的解决方案(更简单,更易于处理),但是在某些情况下,最好不要再创建一个会占用资源的东西(例如,如果您需要大量这样的label-textbox对)一种形式)。

The simplest solution I came up with is as follows (the label shows in the designer after you build the project): 我想到的最简单的解决方案如下(构建项目后,标签会显示在设计器中):

public class CustomTextBox : TextBox
    {
        public Label AssociatedLabel { get; set; }

        public CustomTextBox():base()
        {
            this.ParentChanged += new EventHandler(CustomTextBox_ParentChanged);
        }

        void CustomTextBox_ParentChanged(object sender, EventArgs e)
        {
            this.AutoAddAssociatedLabel();
        }

        private void AutoAddAssociatedLabel()
        {
            if (this.Parent == null) return;

            AssociatedLabel = new Label();
            AssociatedLabel.Text = "Associated Label";
            AssociatedLabel.Padding = new System.Windows.Forms.Padding(3);

            Size s = TextRenderer.MeasureText(AssociatedLabel.Text, AssociatedLabel.Font);
            AssociatedLabel.Location = new Point(this.Location.X - s.Width - AssociatedLabel.Padding.Right, this.Location.Y);

            this.Parent.Controls.Add(AssociatedLabel);
        }
    }

Although it isn't a complete solution, you need to code the additional behaviour such as moving the label with the textbox, changing the location of the label when its text changes, removing the label when the textbox is removed, and so on. 尽管这不是一个完整的解决方案,但是您需要对其他行为进行编码,例如使用文本框移动标签,在文本更改时更改标签的位置,在删除文本框时删除标签,等等。

Another solution would be to not use the label at all, and just draw the text beside the textbox manually. 另一个解决方案是根本不使用标签,而只是手动在文本框旁边绘制文本。

恐怕不是,您将不得不使用UserControlCustomControl因为无法同时添加TextBox和关联的Label

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

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