locked
UserControl Events not showing in Property Grid RRS feed

  • Question

  • Hi,

       I have a usercontrol which having a textbox inside, I set a Property called TxtBx(to access the Textbox inside the Usercontrol). When i look at the events it is only showing the events of UserControl, But i need to List out TxtBx events also, VS IDE is not list all but is showing the TxtBx property. Other wise i need to manually coded to each events..? Is it possible to do that..?

    Regards

    Dheeraj

    Tuesday, May 18, 2010 4:39 AM

Answers

  • Hi Dheeraj,

    When creating you own UserControl, it's always derived from the Control class which is the BASE class of all the windows forms controls in C#, so, there are a great number of properties、functions and events in it . But not ALL the properties and events are needed in the child class(user control), because the creators may want to hide some of the base properties or events to avoid customer to call the unwanted ones. 

    In the aspect of OOP, it's not possible to NOT allowed the caller of the child class to access the public member of the base class, in other words, we cannot get rid of the public members of base class, even though the member of the base class is abstract or virtual, the accessibility is unchangeable.

    Since we cannot get rid of the unwanted base member, we can think of another way. Actually PictureBox is a good example. In the IDE, you cannot get the Text property by the intellisense when you coding "PictureBox1.", but if you forcely coding a statement "pictureBox1.Text="aaa";", the compiler will not get an error. Obviously, Text is one of the properties of Control class, PictureBox still inherits this, and also can access this, only not access by intellisense, and cannot be found in the Properties browser window. So you just need to do as follows to allow your user control to show/not show property in the Properties browser window:        

            [Browsable(false)]
            [EditorBrowsable(EditorBrowsableState.Never)]
            public override string Text
            {
                get { return base.Text; }
                set { base.Text = value; }
            }

    When you override the Text property, BrowsableAttribute defines the property not show in the Properties browser window, and EditorBrowsableAttribute defines the property not be accessed by intellisense.      

    For the unrewritable members of base class, we can use "new" to hide the base member. The following code hide the TextChanged event: 

            [Browsable(false)]
            [EditorBrowsable(EditorBrowsableState.Never)]
            public new event EventHandler TextChanged;
            protected override void OnTextChanged(EventArgs e)
            {
                if (TextChanged != null)
                    TextChanged(this, new EventArgs());
            }

    Please look into these links on MSDN:

    Varieties of Custom Controls
    How to: Develop a Simple Windows Forms Control

     

    Best Wishes,
    Helen Zhou


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Helen Zhou Tuesday, May 25, 2010 1:57 AM
    Wednesday, May 19, 2010 7:18 AM

All replies

  • Hi Dheeraj,

    When creating you own UserControl, it's always derived from the Control class which is the BASE class of all the windows forms controls in C#, so, there are a great number of properties、functions and events in it . But not ALL the properties and events are needed in the child class(user control), because the creators may want to hide some of the base properties or events to avoid customer to call the unwanted ones. 

    In the aspect of OOP, it's not possible to NOT allowed the caller of the child class to access the public member of the base class, in other words, we cannot get rid of the public members of base class, even though the member of the base class is abstract or virtual, the accessibility is unchangeable.

    Since we cannot get rid of the unwanted base member, we can think of another way. Actually PictureBox is a good example. In the IDE, you cannot get the Text property by the intellisense when you coding "PictureBox1.", but if you forcely coding a statement "pictureBox1.Text="aaa";", the compiler will not get an error. Obviously, Text is one of the properties of Control class, PictureBox still inherits this, and also can access this, only not access by intellisense, and cannot be found in the Properties browser window. So you just need to do as follows to allow your user control to show/not show property in the Properties browser window:        

            [Browsable(false)]
            [EditorBrowsable(EditorBrowsableState.Never)]
            public override string Text
            {
                get { return base.Text; }
                set { base.Text = value; }
            }

    When you override the Text property, BrowsableAttribute defines the property not show in the Properties browser window, and EditorBrowsableAttribute defines the property not be accessed by intellisense.      

    For the unrewritable members of base class, we can use "new" to hide the base member. The following code hide the TextChanged event: 

            [Browsable(false)]
            [EditorBrowsable(EditorBrowsableState.Never)]
            public new event EventHandler TextChanged;
            protected override void OnTextChanged(EventArgs e)
            {
                if (TextChanged != null)
                    TextChanged(this, new EventArgs());
            }

    Please look into these links on MSDN:

    Varieties of Custom Controls
    How to: Develop a Simple Windows Forms Control

     

    Best Wishes,
    Helen Zhou


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Helen Zhou Tuesday, May 25, 2010 1:57 AM
    Wednesday, May 19, 2010 7:18 AM
  • Thanks to answer my question. I have few  doubts again can you please look at..

    1) Property Grid showing the both properties Usercontrol & Textbox(inside the Usercontrol). If Grid is showing the Property of Texbox then why is not showing the Texbox Events...

    2) Is there any way to show the TextBox Events in Grid...

     

    • Edited by Dheeraj pk Tuesday, May 25, 2010 5:39 AM Spell
    Tuesday, May 25, 2010 5:37 AM
  • Hi Dheeraj,

    About how to create a event of a usercontrol, you can refer to http://msdn.microsoft.com/en-us/library/aa302342.aspx#winformusecon_topic3d, look for the "Create Events " page.

    And also an article on CodeProject: How to Create an Event for a User Control

    Best Wishes,
    Helen Zhou


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Tuesday, May 25, 2010 5:57 AM
  • Ohh sorry you are misunderstand my doubts...

    I'll explain one more please ..

    1) I have UserControl which have TextBox Inside ..
    2) I want to show Texbox Events Also in Property Grid in VS IDE.
    Tuesday, May 25, 2010 6:24 AM
  • Hi Dheeraj,

    Set the Browsable to true.
    [Browsable(true|false)]  is to determine whether the property or event can be seem in the Properties Grid Window in VS IDE.

    Others:
    [Category( "Appearance")] determines its categories in the Properties Grid Window in VS IDE.
    [Description( "Add the description of the property of event")]
    ......

    Sincerely,
    Helen Zhou 


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Tuesday, May 25, 2010 6:45 AM
  • Thanks to answer my doubts, Can you please send your Mail ID so i can send the Screen shots of my controls Property


     

    Tuesday, May 25, 2010 7:16 AM
  • I am facing same issue. i have a user control that have a TextBox.

    In my user control i made a public property of textbox and this is showing in property grid in property panel but the events of that textbox is not showing.

    if your issue is resolved kindly help me to resolve it

    Friday, August 25, 2017 5:51 AM