![]() |
|
#6
|
|||
|
|||
![]() У вас для каждого Edit'а используется практически один и тот же код с точностью до разницы в изменяемых Label'ах. Не лучше ли вместо того, чтобы плодить повторяющийся код, сделать всем один и тот же обработчик события OnChange, в котором с помощью параметра Sender определять, какой из Edit'ов вызвал событие?
pas-файл: Код:
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Label1: TLabel; Label2: TLabel; Label3: TLabel; Label4: TLabel; Label5: TLabel; Label6: TLabel; Edit1: TEdit; Edit2: TEdit; Edit3: TEdit; Button1: TButton; procedure AnyEditChange(Sender: TObject); procedure Button1Click(Sender: TObject); procedure AnyEditKeyPress(Sender: TObject; var Key: Char); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.AnyEditChange(Sender: TObject); var a: string; b, c, d, e: Integer; begin a := (Sender as TEdit).Text; if a = '' then Exit; c := StrToInt((Sender as TEdit).Text); b := Sqr(c); d := StrToInt((Sender as TEdit).Text); e := Sqr(d) * d; case (Sender as TEdit).Tag of 1: begin Label1.Caption := IntToStr(b); Label2.Caption := IntToStr(e); end; 2: begin Label3.Caption := IntToStr(b); Label4.Caption := IntToStr(e); end; 3: begin Label5.Caption := IntToStr(b); Label6.Caption := IntToStr(e); end; end; end; procedure TForm1.Button1Click(Sender: TObject); begin Edit1.Text := ''; Edit2.Text := ''; Edit3.Text := ''; Label1.Caption := ''; Label2.Caption := ''; Label3.Caption := ''; Label4.Caption := ''; Label5.Caption := ''; Label6.Caption := ''; end; procedure TForm1.AnyEditKeyPress(Sender: TObject; var Key: Char); begin case Key of '4'..'9': if StrToInt((Sender as TEdit).Text + Key) > 9 then begin ShowMessage('Можно вводить числа от 4 до 9'); Key := #0; end; #8: {}; else begin ShowMessage('Можно вводить числа от 4 до 9'); Key := #0; end; end; end; end. dfm-файл: Код:
object Form1: TForm1 Left = 192 Top = 114 Width = 696 Height = 480 Caption = 'Form1' Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'MS Sans Serif' Font.Style = [] OldCreateOrder = False PixelsPerInch = 96 TextHeight = 13 object Label1: TLabel Left = 16 Top = 8 Width = 32 Height = 13 Caption = 'Label1' end object Label2: TLabel Left = 16 Top = 24 Width = 32 Height = 13 Caption = 'Label2' end object Label3: TLabel Left = 16 Top = 40 Width = 32 Height = 13 Caption = 'Label3' end object Label4: TLabel Left = 16 Top = 56 Width = 32 Height = 13 Caption = 'Label4' end object Label5: TLabel Left = 16 Top = 72 Width = 32 Height = 13 Caption = 'Label5' end object Label6: TLabel Left = 16 Top = 88 Width = 32 Height = 13 Caption = 'Label6' end object Edit1: TEdit Tag = 1 Left = 88 Top = 16 Width = 121 Height = 21 TabOrder = 0 Text = 'Edit1' OnChange = AnyEditChange OnKeyPress = AnyEditKeyPress end object Edit2: TEdit Tag = 2 Left = 88 Top = 48 Width = 121 Height = 21 TabOrder = 1 Text = 'Edit2' OnChange = AnyEditChange OnKeyPress = AnyEditKeyPress end object Edit3: TEdit Tag = 3 Left = 88 Top = 80 Width = 121 Height = 21 TabOrder = 2 Text = 'Edit3' OnChange = AnyEditChange OnKeyPress = AnyEditKeyPress end object Button1: TButton Left = 96 Top = 136 Width = 75 Height = 25 Caption = 'Button1' TabOrder = 3 OnClick = Button1Click end end Последний раз редактировалось Nyctos Kasignete, 10.05.2009 в 13:12. |