Applying the same function to ALL textboxes in a project

aguamenti

New member
Joined
Mar 21, 2015
Messages
2
Programming Experience
1-3
Is there a way to apply the same function to ALL textboxes in a project? I have about 50+ textboxes on a form (and 100+ more on other forms) that must be completed by the user and then uploaded to a MYSQL server upon saving. I want to add a "handleApostrophes" function to each textbox's leave event so that MYSQL can handle an apostrophe (by adding ''). What I do not want to do is to have to go into each and every textbox to call this function ... is there a quicker way to add a function to multiple textboxes in a project?
 
What you're trying to do is completely misguided. There is no issue whatsoever saving text containing apostrophes into MySQL or any other database. If you're having trouble with that then it's because you're doing it wrong. If you do it right then the issue goes away. To learn how to do it right and why, which includes your current "issue" but is not limited to it, then follow the Blog link in my signature below and read my post on Parameters In ADO.NET.
 
If you ever do find yourself in a situation where you do need common custom functionality in multiple controls - which definitely is not the case here - then what you would do is define your own class that inherits the standard class, add the extra functionality and then use that class on your forms in place of the standard class. For instance, if you wanted TextBoxes that all did the same thing on their Leave event then you would inherit the TextBox class and override the OnLeave method, e.g.
Public Class TextBoxEx
    Inherits TextBox

    Protected Overrides Sub OnLeave(ByVal e As EventArgs)
        'Put code here to be executed BEFORE the Leave event handler(s).

        MyBase.OnLeave(e)

        'Put code here to be executed AFTER the Leave event handler(s).
    End Sub

End Class
Once you build the project, that new control will be added to the Toolbox automatically, so you can add it to your forms just as you would any other control. If you already have standard TextBoxes on your forms then you can simply open the designer code file and change all references to the TextBox class to the TextBoxEx class and all your controls will become that new type.
 
To learn how to do it right and why, which includes your current "issue" but is not limited to it, then follow the Blog link in my signature below and read my post on Parameters In ADO.NET.

John - thanks for this. I found the above post on your blog and this solves exactly what I was tying to accomplish - thank you. I will convert all SQL queries to use parameters.

Many thanks
Mark
 
Back
Top