Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Override TextBox property??? 3

Status
Not open for further replies.

sp76

Programmer
Jul 1, 2003
59
AU
am creating a custom TextBox control. I want to restrict the user from changing the value of Maxlength property. Is there any way I can override the Maxlength property and make it ReadOnly. Or is there any other way to do it?
 
Instead of Overrides use Shadows - this hides the base implementation of the property and allows the signature to be changed (in this case to read only)
Code:
Private iMaxLength As Int32 = 5
   
Public Shadows ReadOnly Property MaxLength() As Integer

    Get

        Return iMaxLength

    End Get

End Property
 
Don't forget that your inherited control will also need to set the base MaxLength to the required value e.g.
Code:
'Add in the constructor
MyBase.MaxLength = iMaxLength
 
works like a treat. thanks a ton. actually i am new to object based programming, creating custom control. Could you please suggest me some good articles or books or website to get hold of object oriented technologies used in .net, creating custon control, overriding base class events, properties, overloading methods, shadows, protected etc etc... Google and other search engine throws heaps of links, but what i am looking for is some fundamental stuff. I want to learn the principles and fundamentals, rather than just playing around with articles are learing through hit & trial methods. I want to get my basics right.

One more question: I trying to work out how i can embed a custom property of a custom control in Properties window at design time???? or How can i remove a property from Properties window for a control???
 
About this
One more question: I trying to work out how i can embed a custom property of a custom control in Properties window at design time???? or How can i remove a property from Properties window for a control???

.NET will automatically put available properties of an object to the Properties window but there are several ways to customize this,

First of all you need to import following library to your project

Code:
Imports System.ComponentModel

then while declaring a property, if you do not want to show that in the Property window, use the Browsable as follows

Code:
<Description("My Description: This is what you see in property window Description"), Browsable(False)> _
Public ReadOnly Property myProperty() As boolean
   Get

      Return bTest
   End Get
End Property

In this case since I have set the browsable to false, other developers won't even see this in Property window at design time. If you omit Browsable(False) then that will show up in the property window. In either cases while coding the intellisense will show the description that you have given here, when they are coding with your object.

Hope this answered your question.

-Kris


 
thanks kris11 and Shelton just what I was looking for.

Christiaan Baes
Belgium
&quot;What a wonderfull world&quot; - Louis armstrong
 
Hi Kris11

i did use Browsable(False) while declaring property, but it still shows up in Property Windows? what am i doing wrong here's my code

Imports System.ComponentModel

<Browsable(False)> _
Private iMaxLength As Int32 = 10
Public Shadows ReadOnly Property MaxLength() As Integer
Get
Return iMaxLength
End Get
End Property
 
<Browsable(False), Description("Read only MaxLength.")> _
Public Overrides Property MaxLength() As Integer
Get
Return MaxLength
End Get
Set(ByVal Value As Integer)
MaxLength = iMaxLength
End Set
End Property
 
Problem here is you are attaching Browsable property to the variable declaration, not to the property you want, So move the variable declaration to the line above Browsable and eveyrthing should work fine.

Code:
Private iMaxLength As Int32 = 10

<Browsable(False)> _
    Public Shadows ReadOnly Property MaxLength() As Integer
        Get
            Return iMaxLength
        End Get
End Property

-Kris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top