Resolved This code use a button to run, How can a make run without the button.

manny cash

Member
Joined
Oct 19, 2024
Messages
20
Programming Experience
Beginner
Hi guys Greeting!
The code formats a different textbox in a data entry form, but it runs from a button click. How can I make it run without the button? I found this code on the Internet.

1745018615849.png
 
Solution
As for the issue, Windows Forms is event driven, so any code you want executed generally starts with an event handler. In the case of what you have, that's a handler for the Click event of a Button. The user clicks a Button, the Button raises its Click event, the form handles that event and executes the specified code. It's up you to decide when you want your code executed and what event will be raised at that time. You haven't told us anything about that so we can't really help with specifics. You have two examples in that code already though. The form raises its Load event when the form loads, so you can handle that event and execute code at that time. The KeyPress event of a...
Please do no post pictures of code. If we want to copy the code for any reason, e.g. to run it ourselves or edit it to show a solution, we can't do that easily from a picture. ALWAYS post code, error messages, etc, as text, formatted appropriately.
 
As for the issue, Windows Forms is event driven, so any code you want executed generally starts with an event handler. In the case of what you have, that's a handler for the Click event of a Button. The user clicks a Button, the Button raises its Click event, the form handles that event and executes the specified code. It's up you to decide when you want your code executed and what event will be raised at that time. You haven't told us anything about that so we can't really help with specifics. You have two examples in that code already though. The form raises its Load event when the form loads, so you can handle that event and execute code at that time. The KeyPress event of a TextBox is raised when the user presses a keyboard key while that TextBox has focus, so you can handle that event and execute code at that time. The form, all its controls and other object that are not controls raise lots of different events, so you need to think about exactly you want to prompt your code to be executed, find the event that is raised at that time and handle it.

Looking at the code, it seems to be validating the contents of a TextBox, so the obvious choice seems to be the Validating event of that TextBox. Make sure that you read the documentation for that event so that you understand exactly how it works. ALWAYS read the relevant documentation for new types and members. You can simply click the name in code and press F1 to navigate directly to the appropriate page, but you should also have the home page favourited/bookmarked in your browser.

Note also that you can call the form's Validate method to raise the Validating event on all controls. That's a great way to make sure that even controls that never received focus get validated before the user tries to save data or whatever.
 
Solution
For future reference, there is a Resolved button at the top-right of the thread that you should click to mark the thread Resolved when your problem has been resolved. You should also select the post that provided the resolution, if there is one, on the righthand side of that post. All this helps others see the state of the thread and whether it can help them without having to open the thread and read the whole thing. I've done those things for you this time. Also, please select the Question prefix when posting a new question. That prefix is what gets changed when you click Resolved.
 
Back
Top