ErrorProvider - como usar o evento validating da TextBox com o ErrorProvider

Top  Previous  Next

{

  Este exemplo mostra o uso de "Validating" em conjunto com o ErrorProvider

}

 

unit WinForm;

 

interface

 

uses

  System.Drawing, System.Collections, System.ComponentModel,

  System.Windows.Forms, System.Data;

 

type

  TWinForm = class(System.Windows.Forms.Form)

  {$REGION 'Designer Managed Code'}

  strict private

    /// <summary>

    /// Required designer variable.

    /// </summary>

    Components: System.ComponentModel.Container;

  TextBox1: System.Windows.Forms.TextBox;

  ErrorProvider1: System.Windows.Forms.ErrorProvider;

  TextBox2: System.Windows.Forms.TextBox;

    /// <summary>

    /// Required method for Designer support - do not modify

    /// the contents of this method with the code editor.

    /// </summary>

    procedure InitializeComponent;

  procedure TextBox1_Validating(sender: System.Object; e: System.ComponentModel.CancelEventArgs);

  procedure TextBox1_TextChanged(sender: System.Object; e: System.EventArgs);

  procedure TextBox1_Validated(sender: System.Object; e: System.EventArgs);

  {$ENDREGION}

  strict protected

    /// <summary>

    /// Clean up any resources being used.

    /// </summary>

    procedure Dispose(Disposing: Boolean); override;

  private

    { Private Declarations }

  public

    constructor Create;

  end;

 

  [assembly: RuntimeRequiredAttribute(TypeOf(TWinForm))]

 

implementation

 

{$AUTOBOX ON}

 

{$REGION 'Windows Form Designer generated code'}

/// <summary>

/// Required method for Designer support -- do not modify

/// the contents of this method with the code editor.

/// </summary>

procedure TWinForm.InitializeComponent;

begin

 Self.TextBox1 := System.Windows.Forms.TextBox.Create;

 Self.ErrorProvider1 := System.Windows.Forms.ErrorProvider.Create;

 Self.TextBox2 := System.Windows.Forms.TextBox.Create;

 Self.SuspendLayout;

 // 

 // TextBox1

 // 

 Self.TextBox1.Location := System.Drawing.Point.Create(2852);

 Self.TextBox1.Name := 'TextBox1';

 Self.TextBox1.Size := System.Drawing.Size.Create(18020);

 Self.TextBox1.TabIndex := 0;

 Self.TextBox1.Text := 'TextBox1';

 Include(Self.TextBox1.Validating, Self.TextBox1_Validating);

 Include(Self.TextBox1.Validated, Self.TextBox1_Validated);

 Include(Self.TextBox1.TextChanged, Self.TextBox1_TextChanged);

 // 

 // ErrorProvider1

 // 

 Self.ErrorProvider1.ContainerControl := Self;

 // 

 // TextBox2

 // 

 Self.TextBox2.Location := System.Drawing.Point.Create(2880);

 Self.TextBox2.Name := 'TextBox2';

 Self.TextBox2.Size := System.Drawing.Size.Create(17620);

 Self.TextBox2.TabIndex := 2;

 Self.TextBox2.Text := 'TextBox2';

 // 

 // TWinForm

 // 

 Self.AutoScaleBaseSize := System.Drawing.Size.Create(513);

 Self.ClientSize := System.Drawing.Size.Create(292273);

 Self.Controls.Add(Self.TextBox2);

 Self.Controls.Add(Self.TextBox1);

 Self.Name := 'TWinForm';

 Self.Text := 'WinForm';

 Self.ResumeLayout(False);

end;

{$ENDREGION}

 

procedure TWinForm.TextBox1_Validated(sender: System.Object; e: System.EventArgs);

begin

  ErrorProvider1.SetError(TextBox1, '');

end;

 

procedure TWinForm.TextBox1_Validating(sender: System.Object; e: System.ComponentModel.CancelEventArgs);

begin

  e.Cancel := (TextBox1.Text.ToUpper <> 'AAA');

  if e.Cancel then ErrorProvider1.SetError(TextBox1, 'Digite AAA');

end;

 

end.