0

I am currently coding a school project. I apologize in advance for a possibly noob question.

I need to make a system for a fictional NGO, and I need to have all users register and login whenever they use the program.

I use editboxes and maskedits to receive the login data. The login screen has two editboxes, one for the username and one for the password. The registration screen is the same form, resized and with two additional, dynamic editboxes: One for confirming the password and another for the user's e-mail address.

Now, I made the static editboxes to contain a default value: If the user enters the editbox, then the default value disappears. If the user then exits the editbox without entering any values, the default value reappears. I haven't managed to get the dynamic editboxes to do the same.

How does one get OnActivate event handlers such as OnEnter/OnExit to fire when an already created dynamic component is activated?

  • 2
    You just assign the event properties that you want to set up to the procedure the IDE creates when you double-click in an event in the IDE, e.g. MyForm.OnShow := FormShow. It might seem a bit confusing at first ... – MartynA Sep 09 '14 at 17:15
  • 1
    As in [this answer](http://stackoverflow.com/a/1005382). – Sertac Akyuz Sep 09 '14 at 17:17
  • Although you can assign the events at runtime, but did you know, that you can place the controls at designtime and hide the controls at runtime if you do not need them? – Sir Rufo Sep 09 '14 at 23:09

1 Answers1

5

If you are using a relatively modern Delphi version that supports XP+ Visual Styles, then TEdit has a TextHint property that does exactly what you are looking for, without the need to use any events at all.

procedure TMyForm.FormCreate(Sender: TObject);
var
  Edit: TEdit;
begin
  Edit := TEdit.Create(Self);
  Edit.Parent := ...;
  ...
  Edit.TextHint := 'default text here';
end;

Otherwise, if you really want to use the events, then you can do this instead:

procedure TMyForm.FormCreate(Sender: TObject);
var
  Edit: TEdit;
begin
  Edit := TEdit.Create(Self);
  Edit.Parent := ...;
  ...
  Edit.Text := 'default text here';
  Edit.OnEnter := EditEnter;
  Edit.OnExit := EditExit;
end;

procedure TMyForm.EditEnter(Sender: TObject);
begin
  if TEdit(Sender).Text = 'default text here' then
    TEdit(Sender).Text := '';
end;

procedure TMyForm.EditExit(Sender: TObject);
begin
  if TEdit(Sender).Text = '' then
    TEdit(Sender).Text := 'default text here';
end;
TLama
  • 75,147
  • 17
  • 214
  • 392
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770