简体   繁体   English

在WinForm中向textbox custom usercontrol添加标签

[英]Add label to textbox custom usercontrol in WinForm

I am writing a windows forms application that has a lot of textboxes. 我正在编写一个包含大量文本框的Windows窗体应用程序。 I want to add a label or a caption to the textbox, so that I don't have to drag a lot labels onto the form and deal with positioning etc. So far I have found 2 possible ways to do this. 我想在文本框中添加标签或标题,这样我就不必将很多标签拖到表单上并处理定位等。到目前为止,我已经找到了两种可行的方法。

  1. Create a user control with the label and textbox. 使用标签和文本框创建用户控件。 How do I get the control, label and textbox to size appropriately depending on the text entered since the control will be reusable and different text sizes will be entered. 如何根据输入的文本使控件,标签和文本框适当调整大小,因为控件可以重复使用,并且将输入不同的文本大小。 How to get all the properties and events of the textbox to remain the same. 如何使文本框的所有属性和事件保持不变。

  2. Extend a normal textbox and add a string property called label or caption, and show this property at the left of the textbox. 扩展普通文本框并添加名为label或caption的字符串属性,并在文本框的左侧显示此属性。 I know this can be done in Web.UI with CSS but is it possible in a winform and how? 我知道这可以在带有CSS的Web.UI中完成但是它有可能在winform中如何实现?

Any suggestions on how to do either of these? 有关如何做其中任何一项的任何建议? Thanks. 谢谢。

You can create a UserControl that contains a label and a textbox. 您可以创建包含标签和文本框的UserControl When you add the user control to your form, both the label and the textbox within will be added simultaneously. 将用户控件添加到表单时,将同时添加标签和文本框。 You can expose properties of the label and textbox to assign values at design or run time. 您可以公开标签和文本框的属性,以便在设计或运行时分配值。

Using this method, you can add multiples of the user control to standardize the layout. 使用此方法,您可以添加多个用户控件以标准化布局。 As far as resizing the controls based on the text, you'll have to subscribe to events and change the sizing manually. 至于根据文本调整控件大小,您必须订阅事件并手动更改大小。

For example, you can subscribe to the TextChanged event of the label and the textbox. 例如,您可以订阅标签和文本框的TextChanged事件。 When the event fires, you calculate the size of the string and then adjust the width and position of the controls accordingly. 触发事件时,您计算字符串的大小,然后相应地调整控件的宽度和位置。

If you get to the point where you have too many textboxes, I would suggest switching to a DataGridView . 如果你到达了太多文本框的地步,我建议你切换到DataGridView The GridView component is very well suited for what you're describing, but of course it requires you to accept a grid layout. GridView组件非常适合您所描述的内容,但当然它要求您接受网格布局。

One of the bonuses involved in using a GridView is hard to appreciate until you see it in action: it creates only one HWINDOW at a time (two if you're in editing mode). 使用GridView所涉及的奖励之一很难被欣赏,直到你看到它的实际效果:它一次只创建一个HWINDOW(如果你处于编辑模式,则只有两个)。 If you create Labels and TextBoxes all over your form, each one is registered with the operating system as an HWINDOW object. 如果在表单上创建标签和文本框,则每个表单都在操作系统中注册为HWINDOW对象。 All those HWINDOW objects take time to display! 所有这些HWINDOW对象都需要时间来显示! In .NET 1.0, WinForms was so slow that dialogs with more than about two dozen controls were unusable. 在.NET 1.0中,WinForms非常慢,超过大约24个控件的对话框无法使用。 Even though .NET 2.0 is much better in this regard, but you'll still get significantly better performance by using a single control that manages lots of data, as opposed to lots of controls that each manage one piece of data. 尽管.NET 2.0在这方面要好得多,但是通过使用管理大量数据的单个控件仍然可以获得明显更好的性能,而不是每个管理一个数据的大量控件。

Oh, and another option, if you like: you can also try a PropertyGrid . 哦,另外一个选择,如果你喜欢:你也可以尝试一下PropertyGrid It has the advantage that it will also show help and allow you to create complex editing controls for each element. 它的优点是它还可以显示帮助,并允许您为每个元素创建复杂的编辑控件。

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

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