我想要一些小字段。当他们不会被填补时,他们会变红,需要填写。 在帖子可以完成之前
以下是我想要实现的内容的截图:
答案 0 :(得分:5)
我会添加一个TShape
,它可以在您的编辑框周围画一条红线。如果您希望红色边框替换正常的TEdit边框,您可以修改Edit控件的属性,使其没有边框。
如果您希望将形状填充,请将画笔样式更改为bsClear
答案 1 :(得分:5)
如果需要,您可以挂钩WM_Paint消息并在ControlCanvas上绘制一个矩形。一种方法可以是这样的:
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.