Creating required form textboxes?
One of my forms has a few textboxes that are required. If I try to use an if/then statement, the form shows a message box stating the error but then continues and eventually crashes because the textbox wasn't filled out. How to I make the program stop what it's doing if the box isn't filled out?
Re: Creating required form textboxes?
Obviously your If statement is not correct. As you haven't shown us what you're doing, we can't tell you what you're doing wrong. Generally speaking though, code that is only to be executed IF a condition is met should be place INSIDE the If block. If you have your code inside the If block and it still doesn't work then you probably have your condition inverted.
That said, the "proper" way to validate is to use the Validating event of your controls. You should handle the Validating event of each control you want to validate and, in the event handler, you set e.Cancel to True if it fails. The user will then not be able to leave the control until they enter valid data. When you click your Save button or whatever, you call ValidateChildren on the form and every control will be validated. This ensures even controls that never received focus will raise a Validating event. ValidateChildren will return False if one or more controls fail validation, allowing you to decide whether to proceed with the operation or not using an If statement.