C#如何给控件添加文本
我有一个来自控件的子类,我想为其设置文本。 当我将父级更改为标签时,可以完成此操作,但该类的同级应该是按钮。 从逻辑上讲,标签和按钮可以从 Control 扩展,因此我从 Control 扩展我的父类,但现在我无法在其上写入文本。 当然,它上面存在文本属性,但我看不到我的文本。 请帮我 .. 多谢
I have a subclass from control and I want set text for that.
When I change parent to label this can be done but this class has sibling should be buttons.
Logically label and button can extends from Control therefore I extend my parent class from Control but now I can't write text on it.
Of course text property exists on it but i can't see my text.
Please help me ..
Thanks a lot
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据您的描述(想要文本标签和按钮)...
听起来您应该考虑使用
UserControl
,而不是子类化Control
。这允许您创建一个通过组合其他控件构建的“自定义控件”,这意味着您可以拥有一个包含标签(您的文本)以及您的标签的UserControl
。按钮,并将其视为一个统一的对象。Given your description (wanting a label for Text as well as a button)...
Instead of subclassing
Control
, it sounds like you should consider using aUserControl
. This allows you to make a "custom control" that's built by composing other controls, which means you can have a singleUserControl
containing a label (your Text) as well as your buttons, and treat it as a unified object.重写
Text
属性来执行您需要的操作:(假设您的自定义控件中有一个按钮)。
Override the
Text
property to do what you need:(assuming you have a button in your custom control).