Cumartesi, Ağustos 11, 2018

Can not focus a disabled or invisible window!

My suggestion is:

procedure _setFocus(o: TObject);
var
   m: TMsg;
   c: Cardinal;
begin
   c := GetTickCount;
   // Wait max. 200 milliseconds...
   while not Application.Terminated and (GetTickCount - c > 200) do
      begin
         if (o is TWinControl) and 
            IsWindowVisible(TWinControl(o).Handle) and
            TWinControl(o).Enabled
         then
            begin
               TWinControl(o).SetFocus;
               break;
            end
         else if PeekMessage(m, 0, 0, 0, PM_REMOVE) then
            begin
               TranslateMessage(m);
               DispatchMessage(m);
            end;
      end;
   // As a matter of fact, the best way to do this is with THREAD.
   // Murat
end;
.
.
.
_setFocus(edit1);

Salı, Temmuz 10, 2018

If source formatter doesn't work...

I do not know why it happened. The source formatter (CTRL + D) isn't working on some files. I found a solution for this.

  1. Open Project Manager (CTRL + ALT + F11)
  2. Select the file that the source code formatter does not work with.
  3. Rename it. (e.g., Form1.pas >> Form101.pas)
  4. Press CTRL + D.
  5. Rename. (e.g., Form101.pas >> Form1.pas)
  6. Press CTRL + D.

That's all...


Perşembe, Temmuz 05, 2018

Catch the error of validation of TMaskEdit

type
   TMaskEdit = class(Vcl.Mask.TMaskEdit)
   protected
      procedure ValidateError; override;
   end;

   TForm1 = class(TForm)
      .
      .
      procedure FormCreate(Sender: TObject);
      .
      .

.
.
.

{ TMaskEdit }
// All TMaskEdits in your form will use this procedure.
procedure TMaskEdit.ValidateError;
begin
   try
      inherited; // Original ValidateError
   except
      // Validation error occurred!
      Self.Clear;
      // I preferred clear the text.
      // If you want you can prefer show any message.
   end;

end;