Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit Unit1;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Menus, Vcl.ExtDlgs;
- type
- TForm1 = class(TForm)
- lblInstruction: TLabel;
- edtName: TEdit;
- lblValidity: TLabel;
- lblResult: TLabel;
- mnMain: TMainMenu;
- mnItmHelp: TMenuItem;
- mnItmFile: TMenuItem;
- mnSbItemInputFromFile: TMenuItem;
- OpenTextFileDialog: TOpenTextFileDialog;
- SaveTextFileDialog: TSaveTextFileDialog;
- mnSbItmSaveToFile: TMenuItem;
- procedure edtNameChange(Sender: TObject);
- procedure edtNameKeyPress(Sender: TObject; var Key: Char);
- procedure mnItmHelpClick(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- procedure mnSbItemInputFromFileClick(Sender: TObject);
- procedure FormClose(Sender: TObject; var Action: TCloseAction);
- procedure mnSbItmSaveToFileClick(Sender: TObject);
- procedure edtNameKeyDown(Sender: TObject; var Key: Word;
- Shift: TShiftState);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- Form1: TForm1;
- implementation
- {$R *.dfm}
- procedure ValidateInput(Name: string; out TextMessage: string);
- var
- Letters: set of Char;
- i: Integer;
- IsValid: Boolean;
- begin
- IsValid := True;
- Letters := ['a'..'z', 'A'..'Z'];
- for i := 1 to Length(Name) do
- if not (Name[i] in Letters) then
- IsValid := False;
- TextMessage := '';
- if Name <> '' then
- if IsValid and (UpperCase(Name[1]) = Name[1]) then
- TextMessage := ''
- else
- TextMessage := 'Name should contain only letters (first letter should be uppercase)'
- else
- TextMessage := 'Name should contain only letters (first letter should be uppercase)';
- end;
- procedure TForm1.edtNameChange(Sender: TObject);
- var
- s, ErrorMessage : string;
- Letters: set of Char;
- i: Integer;
- IsValid: Boolean;
- begin
- s := edtName.Text;
- ValidateInput(s, ErrorMessage);
- lblValidity.Caption := ErrorMessage;
- end;
- procedure TForm1.edtNameKeyDown(Sender: TObject; var Key: Word;Shift: TShiftState);
- begin
- if ((ssCtrl in Shift) AND (Key = ord('V'))) then
- Key := 0;
- end;
- procedure TForm1.edtNameKeyPress(Sender: TObject; var Key: Char);
- var
- Name: string;
- begin
- Name := edtName.Text;
- if key = #13 then
- if lblValidity.Caption = '' then
- if Name = 'Lynn' then
- lblResult.Caption := 'Hello Carolyn!'
- else
- if Name = 'Mike' then
- lblResult.Caption := 'Hello Michael!'
- else
- if Name = 'Bob' then
- lblResult.Caption := 'Hello Robert!'
- else
- if Name = 'Liz' then
- lblResult.Caption := 'Hello Elizabeth!'
- else
- if Name = 'Peg' then
- lblResult.Caption := 'Hello Margaret!'
- else
- lblResult.Caption := 'I DON''T KNOW YOU!';
- end;
- procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
- var
- Answer: Integer;
- begin
- Answer := MessageBox(Handle, PChar('Вы действительно хотите выйти?'), PChar('Выйти?'), MB_OKCANCEL);
- if Answer = 2 then
- Action := TCloseAction.caNone
- end;
- procedure TForm1.FormCreate(Sender: TObject);
- begin
- lblValidity.Caption := 'Name should contain only letters (first letter should be uppercase)';
- end;
- procedure TForm1.mnSbItemInputFromFileClick(Sender: TObject);
- var
- InputFilePath, ErrorMessage: string;
- InputFile: TextFile;
- Input: string;
- begin
- OpenTextFileDialog.InitialDir := GetCurrentDir();
- OpenTextFileDialog.Title := 'Choose file to input data from';
- if OpenTextFileDialog.Execute then
- begin
- AssignFile(InputFile, OpenTextFileDialog.FileName);
- Reset(InputFile);
- Read(InputFile, Input);
- ValidateInput(Input, ErrorMessage);
- if ErrorMessage = '' then
- edtName.Text := Input
- else
- MessageDlg(ErrorMessage, mtError, [mbOk], 0);
- CloseFile(InputFile);
- end;
- end;
- procedure TForm1.mnSbItmSaveToFileClick(Sender: TObject);
- var
- OutputFile: TextFile;
- Greeting: string;
- begin
- SaveTextFileDialog.InitialDir := GetCurrentDir();
- SaveTextFileDialog.Title := 'Choose file to save data in';
- if lblResult.Caption = '' then
- MessageDlg('You can''t output data cause of empty result', mtWarning, [mbOk], 0)
- else
- if SaveTextFileDialog.Execute then
- begin
- AssignFile(OutputFile, SaveTextFileDialog.FileName);
- Rewrite(OutputFile);
- Greeting := lblResult.Caption;
- Write(OutputFile, Greeting);
- CloseFile(OutputFile);
- end;
- end;
- procedure TForm1.mnItmHelpClick(Sender: TObject);
- begin
- MessageDlg('This program can recognize and greet one of 5 friends', mtInformation, [mbOk], 0);
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement