w0lfshad3

I was going over an example of creating a custom control made up of a label and a textbox.

After going through it i am doing an exercise on it. The exercise sais i must create a new MaxLenght property that stores the maximum number of characters that can be entered into the textbox. I did that. The rest was adding an event for when MaxLenght is changed. The problem arose when i tried to override MaxLenght property; the compiler sais: "no suitable method found to override". The MaxLenght property is virtual though but with a twist, it has [LocalizableAttribute(true)] attached to it too, is that the reason i can't override The info on the attribute sais: "Specifies whether a property should be localized. This class cannot be inherited."

I tried even inheriting from the TextBox class separately rather than just inheriting from the custom control. I cannot override MaxLenght from that or the custom control. What is the solution for my problem please

What i need to do is replace the functionality of MaxLenght accessing the relevant variable that is linked to the maxLenght functionality just like in the base class so i can attach MaxLengthChanged event to the set part of the property. I just dont' know how and i don't understand why i cannot override MaxLenght and how to work around that.



Re: Visual C# General overriding MaxLength

Matthew Watson

Are you spelling it correctly in the code MaxLength is the correct spelling (but you typed MaxLenght in your message above).




Re: Visual C# General overriding MaxLength

w0lfshad3

Woot, finally broke through the exercise:

the override i used:

Code Snippet

public override int MaxLength

{

get

{

return base.MaxLength;

}

set

{

base.MaxLength = value;

if (MaxLengthChanged != null) // Make sure there are subscribers

{

// Get the list of methods to call

System.Delegate[] subscribers = MaxLengthChanged.GetInvocationList();

// Loop through the methods

foreach (System.EventHandler target in subscribers)

{

target(this, new EventArgs()); // Call the method

}

}

}

}

and for accessing childs:

Code Snippet

MyTextBox mtb = (MyTextBox)this.Controls["ctlLabelTextbox1"].Controls["txtLabelText"];

this thread helped me : http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=359972&SiteID=1