0

I'm customizing a control which inherits from UserControl, this control has Text property inherits from UserControl. I noticed that when I type, there is no Text entry in the properties list (provided by Intellisense) of my custom control.

Do I have to declare a new Text property (with new keyword) in my custom control to intentionally hide the base Text property? Doing this requires me to invoke OnTextchanged() myself and needs more code while using the base/inherited Text property is enough to me. However I don't like my control Properties to be not present in the Intellisense list.

Any suggestion would be highly appreciated! Thank you!

King King
  • 61,710
  • 16
  • 105
  • 130
  • It is hidden because UserControl has no meaningful way to display text. It is designed to be a container for other controls. Good odds that you should be deriving from Control instead, it is unclear from the question. If not, like you display the text with a Label, then you'll have to override the Text property and undo the base class attributes. – Hans Passant Apr 28 '13 at 18:32
  • @HansPassant The first reason I don't choose Control is UserControl supports designer with drag n drop other controls on to it while to do this with Control, I'm afraid that I have to use some tricks first. The second is I don't know why clicking on Control doesn't give focus to it, I want the control to get focus when user clicking on it. If you know how to achieve this, please let me know. Anyway, drag n drop at design time makes UserControl be my choice in almost cases. Thanks! – King King Apr 28 '13 at 19:08
  • A UserControl won't take the focus, for the same reason. Not a problem with Control. After a UserControl is designed and you drop it on a form, you cannot add controls to it anymore. Not without a custom designer. – Hans Passant Apr 28 '13 at 19:24
  • No, I've tested with UserControl, I customized it as a Focusable Label (focusing by both tab and click). While with Control, it only receives focus when tab, clicking doesn't make it focus. – King King Apr 28 '13 at 19:46
  • Call SetStyle(ControlStyles.UserMouse, true) in the constructor of the control. – Hans Passant Apr 28 '13 at 20:08
  • @HansPassant Thank you, it works. If my custom control doesn't have many children controls on it, I will consider using Control for my next custom controls. Thank you. – King King Apr 28 '13 at 20:23

1 Answers1

3

try with below attributes

[EditorBrowsable(EditorBrowsableState.Always)]
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[Bindable(true)]
public override string Text 
Damith
  • 62,401
  • 13
  • 102
  • 153
  • 1
    Thank you Damith, in fact just overriding Text property (without adding more code) can make it visible, no need more attributes. Maybe all virtual members are hidden from Intellisense by default. Thank you very much! – King King Apr 28 '13 at 19:12
  • @KingKing, actually you need to add the `[Browsable(true)]` attribute to make it viewable in the properties list. Just overriding it would still not show it for me. – Arvo Bowen Mar 08 '23 at 19:07