继承文本框控件

编程入门 行业动态 更新时间:2024-10-26 12:33:40
本文介绍了继承文本框控件-c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想创建一个继承c#中textBox控件的类。新类包含一个具有3个可能值的新属性(大小)(最大化,最小化,居中) 我该怎么做? 非常感谢! Nas

Hi, I want to create a class which inherits textBox control in c# . the new class contains a new property (size)with 3 possible values (maximize ,minimize,center ) How I can do that? Thanks a lot! N.a.s

推荐答案

这篇详细的文章 Visual C#.NET中的自定义控件 [ ^ ]应该让您走上正确的道路。之后,您可以将其作为家庭作业继承 Textbox 控件并创建新属性/枚举。 This detailed article Custom Controls in Visual C# .NET[^] should put you on the right path. After that, you can take it as an homework task to inherit the Textbox control and create a new property / enumeration.

请参阅我在对这个问题的评论中给出的建议。并遵循它 - 没有别的办法。 你需要的东西会是这样的: Please see my advice given in my comment to the question. And follow it — there is no other way. What you need will look something like this: public class MyTextBox : System.Windows.Forms.TextBox { public enum SizeBehaviorOption { Maximize, Minimize, Center, } public SizeBehaviorOption SizeBehavior { get { return this.sizeBehavior; } set { if (this.sizeBehavior == value) return; this.sizeBehavior = value; // now, do something to adjust your control size/location/something, as the behavior has been changed } } SizeBehaviorOption sizeBehavior; } //class MyTextBox

注意属性的 setter 的作用。这是属性背后的主要思想:使用setter(有时是getter,很少)为属性值的读/写操作添加一些副作用。 但是,我怀疑所有这些活动都是多余的。您通常可以使用对接容器,对接和填充属性(在某些情况下为锚点)实现所需的行为。更多细节取决于您要使用的UI库。下次提问时,请务必标记这个重要的细节。

-SA

Note the role of the setter of the property. This is the main idea behind property: a setter (sometime getter, rarely) is used to add some side effect to read/write operation for the property values. However, I suspect all this activity is redundant. You can usually implement desired behavior using docking containers, docking and padding properties, in some cases anchors. The further detail depend on UI library you want to use. Next time you ask a question, always tag this important detail.

—SA

你不能 - 或者至少你不应该。 名称Size已经是从Control派生的所有类的属性,如果你将它覆盖到不同的类型,那么它就变得很难了使用。 但是,实际上这很容易: You can''t - or at least you shouldn''t. The name Size is already a property of all classes derived from Control and if you override it to a different type, then it becomes difficult to work with. But, to actually do it is easy: myTextbox myt = new myTextbox(); myt.Size = Sizes.Maximize; Controls.Add(myt); ... public enum Sizes { Maximize, Minimize, Centre, } public class myTextbox : TextBox { public new Sizes Size { get; set; } }

更多推荐

继承文本框控件

本文发布于:2023-11-11 23:48:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1579888.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控件   文本框

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!