Question common control validations for multiple forms?

afrozzeenat34

New member
Joined
Jun 7, 2021
Messages
1
Programming Experience
Beginner
I have more than 1 forms and i have validations while triggering button clickevent and i want to call that one button click event handler to other forms so that idont have to write the code again and again please helpi am a beginner in c#
 
That's the wrong way to look at it. If you want reusable code then you put it in its own method and then call that method wherever you need to use it. Where you put that method depends on the specific circumstances. For instance, if you want the same code in multiple forms then you might put it in a single base class and then inherit that in all your forms. Alternatively, you might put it in a module or class and then use that in all your forms.
 
Also, although, UserControls are typically used to package together controls that are often used in conjunction with each other, you could also have a UserControl that contains just one control, but have the validation code live within the UserControl.
 
Also, although, UserControls are typically used to package together controls that are often used in conjunction with each other, you could also have a UserControl that contains just one control, but have the validation code live within the UserControl.
Rather than a user control with one child control, it would probably be better to simply inherit that child control and add the validation directly to that.
 
I agree. That's what I would do. But for all these folks who seem to be addicted to the WinForms Designer, inheritance is not always the first thing that comes to mind for them.
 
Back
Top Bottom