// ---------------------------------------------------------------------------
#include <fmx.h>
#pragma hdrstop
#include "Unit1.h"
// ---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.fmx"
TForm1 *Form1;
// ---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) {
Mask = "+7(00)000-00-00;0;*";
PhoneNumber = "";
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::edtPhoneTyping(TObject *Sender) {
edtPhone->CaretPosition = GetCaretPos();
}
// ---------------------------------------------------------------------------
int __fastcall TForm1::GetCaretPos() {
int i;
int Result = 0;
for (i = 0; i < Mask.Length(); i++) {
if (!(MaskGetCharType(Mask, i) == mcDirective || MaskGetCharType(Mask,
i) == mcMask))
Result += 1;
if ((Result + PhoneNumber.Length()) == i)
break;
}
Result = Result + PhoneNumber.Length() - 1;
return Result;
}
int __fastcall TForm1::GetMaxLength() {
int Result = 0;
for (int i = 0; i < Mask.Length(); i++)
if (MaskGetCharType(Mask, i) == mcMask)
Result = Result + 1;
return Result;
}
void __fastcall TForm1::edtPhoneKeyDown(TObject *Sender, WORD &Key,
System::WideChar &KeyChar, TShiftState Shift) {
if ((Key == 8) && (PhoneNumber.Length() > 0))
PhoneNumber = PhoneNumber.SubString(1, PhoneNumber.Length() - 1);
else if ((PhoneNumber.Length() < GetMaxLength()) && (Key == 0)
&& IsDigit(KeyChar))
PhoneNumber = PhoneNumber + KeyChar;
else
KeyChar = 0;
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::edtPhoneKeyUp(TObject *Sender, WORD &Key, System::WideChar &KeyChar,
TShiftState Shift)
{
edtPhone->CaretPosition = GetCaretPos();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::edtPhoneValidating(TObject *Sender, UnicodeString &Text)
{
Text = FormatMaskText(Mask, PhoneNumber);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
edtPhone->Text = FormatMaskText(Mask, PhoneNumber);
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <FMX.Controls.hpp>
#include <FMX.Forms.hpp>
#include <System.MaskUtils.hpp>
#include <FMX.Controls.Presentation.hpp>
#include <FMX.Edit.hpp>
#include <FMX.StdCtrls.hpp>
#include <FMX.Types.hpp>
#include <System.Character.hpp>
#include <string.h>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TEdit *edtPhone;
TButton *Button1;
void __fastcall edtPhoneTyping(TObject *Sender);
void __fastcall edtPhoneKeyDown(TObject *Sender, WORD &Key, System::WideChar &KeyChar,
TShiftState Shift);
void __fastcall edtPhoneKeyUp(TObject *Sender, WORD &Key, System::WideChar &KeyChar,
TShiftState Shift);
void __fastcall edtPhoneValidating(TObject *Sender, UnicodeString &Text);
void __fastcall FormCreate(TObject *Sender);
private: // User declarations
public:
String PhoneNumber;
String Mask;
int __fastcall GetCaretPos();
int __fastcall GetMaxLength(); // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
Для тех кому нужен на C++