Archive

Posts Tagged ‘forms’

How to Create a Splash Screen

December 15th, 1999 m3Rlin No comments

When you are creating a splash screen you probably want it to show up as soon as possible. I have seen source codes where the splash screen has been shown on the main form’s OnCreate event and closed on its OnShow event. Well, this is not acceptable. Why? Because the splash screen is only shown for a very short period of time. Well, some come up with the idea to put a TTimer on the splash screen… I don’t suggest making the user wait until the splash screen goes away while being able to see the program already “waiting” behind. Aslo, you want to free the splashform after closing it because there is no use of keeping it in the memory since anyway its not going to be used. The solution to this is quite simple. Create the splashform first and release it last. But how? If we set the splashform to be the first created form it automitaclly becomes the main form. Then we have problems. We can’t even close it, because this will close the application itself. Well, there is a way around to this. All we have to do is create it manually. Here is a sample code:

{ Project source file }
...
uses
  Splash; { The unit with the splash screen }
...
var
  SplashScreen: TSplashScreen; { In the Splash unit }
...
begin
  try
    SplashScreen := TSplashScreen.Create(Application);
    with SplashScreen do begin
      Show;
      Update; { To paint the splash screen }
    end;
    Application.CreateForm(TForm1, Form1);
    (* Create other forms or any other processing before the
    ** application is to be opened
    *)
   SplashScreen.Close;
  finally  { Make sure the splash screen gets released }
    SplashScreen.Free;
  end;
  Application.Run;
end.

How to Focus the Next Edit Control With the Enter Key II

December 15th, 1999 m3Rlin 1 comment

When creating forms with a lot of data entry you may want to consider the option to let the user focus the next edit control by hitting the Enter key. The default key is Tab which can become annoying :-)
All we have to do is add the following code the each TEdit control on the form. You can do so by holding down the shift key and selecting each control. Then click the ObjectInspector and double click on OnKeyPress to create a shared event for all the controls. Then enter this code in the event procedure:

  ...
  uses
    Windows;
  ...
  if Key = #13 then begin
    { Make Windows focus the next edit control }
    Perform(WM_NEXTDLGCLTL, 0, 0);
    { Eat the enter key }
    Key := #0;
  end;
  ...

This code will work only with the controls that use this OnKeyPress event.
If you want all the edit controls on the form to act this way use a different way. This code does not require each edit control to use the above routine. Just the form’s KeyPreview property to True. Now add this code to the form’s KeyUp event and your all set :-) If, for some reason you want to exclude any edit control from using Enter key instead to pass the focus all you have to do is modify the if condition to exclude certain edits. Instead of using there names you may want to consider the Tag property for this.

  ...
  if (Key = VK_RETURN) and ([ssCtrl, ssShift] * Shift = []) then
    Perform(WM_NEXTDLGCTL, 0, 0);
  ...

Note: Tip #42 refers to this topic too.