在设计时添加带有文本框的标签 [英] Add Label with Textbox at design time

查看:164
本文介绍了在设计时添加带有文本框的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用VS.NET(C#)创建一个包含许多形式的项目,这些形式包含文本框和相关的标签.我已经通过为包含标签名称的文本框创建的暴露属性创建了关联.问题在于,每次在设计时添加文本框时,都必须添加标签,然后将标签名称输入到文本框的属性中.我宁愿在设计时在创建文本框时动态地执行此操作,就像旧的VB文本框添加一样.我一直在寻找一种在设计时每当添加文本框时就动态添加标签而没有找到任何可接受的解决方案的方法.我在此站点上找到了一个建议添加一个包含文本框和标签的用户控件的答案,尽管这可能是我找到的最佳解决方案,但我认为它对我的限制超出了我的期望.我是否需要经过一些成熟的定制设计师来完成这个希望简单的任务?

I am creating a project using VS.NET (C#) with many forms that contain textboxes and associated labels. 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. 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

推荐答案

尽管我更喜欢使用UserControl的解决方案(更简单,更易于处理),但是在某些情况下,如果不创建又一个东西会吃掉资源是更可取的(例如,如果您在一种形式上需要很多这样的标签-文本框对).

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).

我想到的最简单的解决方案如下(构建项目后,标签会显示在设计器中):

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.

这篇关于在设计时添加带有文本框的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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