Exceptions are used to deal with special occurences such as errors. When you run your program in debug mode (that is from within Delphi), a dialog pops up every time an exception occurs. This is not the only way your program can react to exceptions.
To convert a string into an integer you can use the StrToInt(MyString: String) function. If you pass it 'xaslk' as MyString you will get an exception informing you that 'xaslk' is not an integer-value. It would be pretty annoying if your program crashed every time, the user passes an invalid value. To catch the exception yourself use:
procedure Button1Click(Sender: TObject); begin try strtotint('xaslk'); except showmessage('You did not pass a valid integer value. Please try again.'); exit; finally //... end; end;
Everything behind except is only executed if an exception occurs. All the statements behind finally are always executed. You should free resources here etc.
Windows programs are event-oriented. This means that the program waits for the user to do something and then reacts. If the user presses a button, the corresponding event is issued. Most VCL-components have at least one event property. Its name is usually made up of "On" plus a describtion of the event. Thus the event that occurs when the user clicks a button is called OnClick. It is a property of the TButton class.
You can control the events of the components in you form on the event-tab of the object-inspector. Select a button and look at its events. You will find that there are surprisingly many. OnClick is by far most important. Please refer to the Delphi help-file for a describtion of the events.
To assign an action to an event, simply double-click on the empty field to the right of the event's name. Delphi will open a procedure for you with the correct parameters and assign it to the event. The name of the procedure is made up of the component's name and the event (without the "on"). For Button1 and OnClick this would be Button1Click.
To assign a procedure to an event manually simply use:
Component.OnEvent:= MyEventprocedure;
Where Component.OnEvent is an event-property (Button1.OnClick for example) and MyEventprocedure is the procedure to be assigned to the event. Be aware that the procedure needs to have to correct arguments, in this case it would look like this:
procedure MyEventprocedure(Sender: TObject);
For other events this might be a lot different. The easiest way to find out what arguments an event-procedure needs is to double-click on the event and look at the template that Delphi makes for you.
Fourth Day • Sixth Day
Please e-mail me with any comments!
© 27.12.96 Sebastian Boßung
• Home