The painful way is to make it manually with formatting and parsing. The elegant way is to bind your form to an object containing your properties. When using object-data binding you can specify formats. The conversions in both ways are carried out automatically.
Besides the advantage of having an automatic conversion to and from strings, you have the advantage that you can separate the business logic from your form. You can apply any logic to a property called guest.DateOfBirth
(or just DateOfBirth
if the logic is inside the Guest
class), for instance, instead of applying it to a construct like DateTime.Parse(textBox13.Text)
, which makes it a lot easier to understand and maintain the code.
See:
Data Binding for Windows Forms (on developerfusion)
Data binding concepts in .NET windows forms (on CodeProject)