Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set required Edit fields with a red border?

Tags:

delphi

I want to require a little fields. And when they will not be filled in that they will be red and needed to fill in. before the post can be done

Here is a screenshot of what do I want to achieve:

enter image description here

like image 961
Daan Kleijngeld Avatar asked Nov 06 '13 13:11

Daan Kleijngeld


People also ask

How do you input a field to a border?

Textboxes are input fields created by the <textarea> element. You can use the CSS border property to add a border around your HTML textboxes. You can also use border-width , border-style , and border-color , but the border property covers all of these anyway.


2 Answers

I would add a TShape, which can draw a red line around your edit box. If you want the red border to replace the normal TEdit border you can modify the properties of your Edit control so it has no Border.

If you want the shape to be unfilled, change brush style to bsClear

like image 101
Warren P Avatar answered Nov 09 '22 18:11

Warren P


You might hook the WM_Paint message and draw a rectangle on the ControlCanvas if required. One way to do this could look like this:

unit Edit_WithFrame_If_Needed_But_Empty;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TEdit = Class(StdCtrls.TEdit)
    procedure WMPaint(var Message: TWMPaint); message WM_PAINT;
    procedure CMTextChanged(var Message: TMessage); message CM_TEXTCHANGED;
    procedure WMKEYUP(var Message: TWMPaint); message WM_KEYUP;
  private
    FPaintedRed: Boolean;
    FRequired: Boolean;
    procedure CheckForInvalidate;
  published
  public
    Property Required: Boolean read FRequired Write FRequired;
  End;

  TForm2 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    procedure FormCreate(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

{ TEdit }
procedure TEdit.CheckForInvalidate;
begin
  if Required and (Length(Trim(Text)) = 0) then
  begin
    if not FPaintedRed then
      Invalidate;
  end
  else if FPaintedRed then
    Invalidate;
end;

procedure TEdit.CMTextChanged(var Message: TMessage);
begin
  inherited;
  CheckForInvalidate;
end;

procedure TEdit.WMKEYUP(var Message: TWMPaint);
begin
  CheckForInvalidate;
end;

procedure TEdit.WMPaint(var Message: TWMPaint);
var
  CC: TControlCanvas;
begin
  inherited;
  if Required and (Length(Trim(Text)) = 0) then
  begin
    FPaintedRed := true;
    CC := TControlCanvas.Create;
    try
      CC.Control := Self;
      CC.Pen.Color := clRed;
      CC.Pen.Width := 3;
      CC.Rectangle(ClientRect);
    finally
      CC.Free;
    end;
  end
  else
    FPaintedRed := false;
end;

procedure TForm2.FormCreate(Sender: TObject);
begin
  Edit1.Required := true;
  Edit3.Required := true;
end;

end.
like image 28
bummi Avatar answered Nov 09 '22 20:11

bummi