Results 1 to 7 of 7

Thread: Custom Control

  1. #1

    Thread Starter
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861

    Custom Control

    Okay, here is the dilemma.
    I have created a custom control that consists of a textbox and an optional required field validator.
    Everything works fine except for one minor problem.

    I have overridden some of the properties, such as Font, BackColor, BorderStyle, etc.. and have made these properties invisible in the properties window, using <Browsable(False)> _.

    The problem is, in the code-behind (using VS.NET) Intellisense still displays the hidden properties.
    All the examples I have seen say to use the EditorBrowsable(EditorBrowsableState.Never) attribute to prevent Intellisense from displaying it, but this isn't working....

    Here's a sample control I created to test this.
    Code:
    Imports System
    Imports System.Web
    Imports System.Web.UI
    Imports System.Web.UI.WebControls
    Imports System.ComponentModel
    Imports System.Drawing
    
    
    Public Class MyTextBox
        Inherits TextBox
    
        Private txtBox As TextBox
    
        Public Sub New()
            txtBox = New TextBox
        End Sub
    
        Protected Overrides Sub render(ByVal writer As HtmlTextWriter)
            MyBase.Render(writer)
        End Sub
     
        Browsable - prevents the property from being seen in the properties window...This works
    
        EditorBrowsable - prevents the property from being referenced in intellisense 
        in the code-behind...This doesn't work!
    
        Obsolete - Prevents a developer from using this property. 
        If the developer tries to use this property in the code-behind
        and compile it. The message is supposed to display in the 
        task list as an error.
        "This property isn't supported."...This doesn't work!  
    
        <Browsable(False), _
        EditorBrowsable(EditorBrowsableState.Never), _
        Obsolete("This property isn't supported.", True)> _
        Public Overrides Property BackColor() As Color
            Get
                Return Nothing
            End Get
            Set(ByVal Value As Color)
                txtBox.BackColor = Nothing
            End Set
        End Property
    
    End Class
    Any ideas why???
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  2. #2
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Well, I noticed if you use a non-existing property name, the intended effect occurs.

    If you inherit from a control, and try to suppress what is declared publicly browsable in the parent, the intended effect does not occur.

    In the case of textbox, (and I also tried Label), even though the property is declared Overridable, it doesn't seem you can suppress the MetaTag (EditorBrowsable) associated with that property in the parent.


    Strangely, of the three you presented, this 'glitch' only happens with the EditorBrowsable attribute. I'm not sure if its a bug, or its has to do with how the EditorBrowsable attribute is defined in the server controls that implement it, that prevent it from working.

    So after trying all that, and using the control from a dll instead of within the project, i googled and found this:

    http://www.dotnet247.com/247referenc...29/148631.aspx


    Bottom line: I'm guessing there are some undocumented attributes (or perhaps just reasons) that prevent inheriting children from hiding properties declared as browseable in the parent.

    As far as a workaround, well, you could make your inherited control a composite control (webcontrol) that internally encompasses a textbox, and only expose the properties that are needed. I'm pretty sure for example, the WebControl class does not expose a BackColor property.
    Last edited by nemaroller; Apr 23rd, 2004 at 06:24 PM.

  3. #3
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    public class lite : System.Web.UI.Control
    Magiaus

    If I helped give me some points.

  4. #4

    Thread Starter
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    Yes, WebControl exposes a BackColor Property among others.
    My assumption/test is you can override the inherited properties for the WebControl base class, but you can't suppress them from showing up in intellisense.

    I did a test and created a base control (myTextBox) which inherited from the WebControl class. Then I inherited my base control to create a custom control (myTextBox2).

    I did an override of the WebControls BackColor property in my base control (myTextBox), and I was able to suppress the BackColor property in my custom Control (myTextBox2) and prevent the BackColor property from showing up in intellisense.

    Example:
    VB Code:
    1. Public Class myTextBox
    2.    Inherits WebControl
    3.  
    4.    [color=red]Override the WebControls BackColor Property[/color]
    5.    Public Overrides Property BackColor() As Color
    6.       Get
    7.          'code here
    8.       End Get
    9.       Set(ByVal Value As Integer)
    10.          'code here
    11.       End Set
    12.    End
    13. End Class
    Then I inherit myTextBox
    VB Code:
    1. Public Class myTextBox2
    2.    Inherits myTextBox
    3.    
    4.    'I was able to suppress the BackColor property from
    5.    'showing up in intellisense by doing this.
    6.  
    7.    [color=red]Override myTextBoxes BackColor property[/color]
    8.    <Browsable(False), _
    9.     EditorBrowsable(EditorBrowsableState.Never), _
    10.     Obsolete("This control does not support this property.", True)> _
    11.    Public Overrides Property BackColor() As Color
    12.       Get
    13.          'code here
    14.       End Get
    15.       Set(ByVal Value As Integer)
    16.          'code here
    17.       End Set
    18.    End Property
    19. End Class
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  5. #5
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    new internal System.Drawing.Color BackColor{get{;}}


    you the assembly dev could see backcolor byt no one else should be able to. But then again I don't think that really works either. Your best be is to either inherit from control or from the interfaces IParssorAccesory, IStateManager, IComponet, IDataBind. Of cour the you have to handle state and whast not
    Magiaus

    If I helped give me some points.

  6. #6
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Originally posted by Memnoch1207
    ......

    So you were able to solve this then by simply not inheriting your custom 'textbox' from the System.Web.UI.WebControls.TextBox, but rather the WebControl class. Which is how you create a composite control btw.

    So perhaps then, all server controls, including Label, Checkbox, etc, have these limiting attributes in their decleration, that disallow suppression of the EditorBrowseable attribute.

    I find that pretty strange the .Net framework was made this way.

  7. #7

    Thread Starter
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    It didn't matter if I inherited from WebControl or WebControl.TextBox.

    Apparently you can't suppress the webcontrols properties directly. I was able to solve it by creating a class that inherits webcontrol and overrides the webcontrols properties.
    Then creating another class that inherits from the first class I created, then overriding it's properties.
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width