Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How to add the same _TextChanged to all?
Message
De
17/01/2010 16:17:30
 
 
Information générale
Forum:
ASP.NET
Catégorie:
Formulaires
Versions des environnements
Environment:
C# 2.0
Divers
Thread ID:
01444429
Message ID:
01444461
Vues:
59
>Bonnie,
>
>Thank you very much for the sample code. I will have to hit the books to learn how to "use an Interface on a form" (absolutely new concept for me).
>
>Looking down below at your code, do I understand that what you are doing is:
>1. Adding the OnTextChanged code to all my base textbox and other classes.
>2. At run time the OnTextChanged code will only fire (call method EnableDisableButtons) only if the form "implements this interface". And if the form is plain old form the textboxes do not call this method. Right?
>Do I understand it correctly?


Yes, you understand it correctly. Let me only clarify that the OnTextChanged method is an existing method for every control that has a TextChanged event. You are simply overriding the method and adding more code to it (and do NOT forget to put the base.OnTextChanged(e) statement at the end). I think you know that, but I just wanted to be sure.

~~Bonnie





>
>>I would probably do it by using an Interface on your Form, something like this (untested, off the top of my head code):
>>
>>
>>public interface IEnableDisableButtons
>>{
>>    void EnableDisableButtons(bool enable);
>>}
>>
>>
>>So, for all the Forms that you want to implement this functionality, you simply have that Form implement this Interface (meaning, it will have the EnableDisableButtons() method defined in it).
>>
>>Then, in your TextBox classes, in its OnTextChanged event, you do the following:
>>
>>
>>protected override void OnTextChanged(EventArgs e)
>>{
>>	Control o = this.Parent;
>>	while (o != null && (o is Form) == false)
>>	{
>>		o = o.Parent;
>>	}
>>	if (o != null && o is IEnableDisableButtons)
>>		((IEnableDisableButtons)o).EnableDisableButtons(false);
>>
>>	base.OnTextChanged(e);
>>}
>>
>>
>>As I said, this if off the top of my head, but it should work for you, or at least point you in the right direction.
>>
>>~~Bonnie
>>
>>
>>
>>>Hi,
>>>
>>>I need to add a code to the event _TextChanged of all controls on the form (e.g. textbox, checkbox, etc.). The code in _TextChanged will call a form method to disable all buttons (as soon as user makes a change to any one of textboxes/checkbox on the form). I know that I can add this method to the base Textbox class which is the parent of all textboxes in the toolbox. But then other forms that do not need the _TextChanged code will have it. And I can "manually" add this method to every textbox on the form but this is a lot of work and maintenance.
>>>
>>>Any suggestions of other ways to accomplish this?
>>>
>>>TIA.
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform