Como simular o efeito do CTRL + BACKSPACE
O efeito do CTRL + BACKSPACE em uma edit é apagar a palavra interira
que está atras do cursor.
Exemplo: Teste de frase| = Ao executar CTRL + BACKSPACE
o código que virá a seguir irá apagar a palavra “frase”.
Código compartilhado por Caique Rodrigues no grupo da comunidade no Skype.
Código fonte do procedimento
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| procedure TForm1.CtrlBackspace2(AEdit: TEdit);
var
LOffSet, I: integer;
LText: string;
LBeginWord: boolean;
begin
LOffSet := -1;
LText := AEdit.Text;
LBeginWord := False;
for I := AEdit.SelStart downto 0 do
begin
LOffSet := I;
if not LBeginWord then
LBeginWord := CharInSet(LText[I], ['A' .. 'Z', 'a' .. 'z', '0' .. '9'])
else if not CharInSet(LText[I], ['A' .. 'Z', 'a' .. 'z', '0' .. '9']) then
Break;
end;
Delete(LText, LOffSet + 1, AEdit.SelStart - LOffSet + 1);
AEdit.Text := LText;
AEdit.SelStart := LOffSet;
end; |
procedure TForm1.CtrlBackspace2(AEdit: TEdit);
var
LOffSet, I: integer;
LText: string;
LBeginWord: boolean;
begin
LOffSet := -1;
LText := AEdit.Text;
LBeginWord := False;
for I := AEdit.SelStart downto 0 do
begin
LOffSet := I;
if not LBeginWord then
LBeginWord := CharInSet(LText[I], ['A' .. 'Z', 'a' .. 'z', '0' .. '9'])
else if not CharInSet(LText[I], ['A' .. 'Z', 'a' .. 'z', '0' .. '9']) then
Break;
end;
Delete(LText, LOffSet + 1, AEdit.SelStart - LOffSet + 1);
AEdit.Text := LText;
AEdit.SelStart := LOffSet;
end;
Para utilizar, deixe o evento OnKeyPress da sua edit
conforme o exemplo abaixo:
1
2
3
4
5
6
7
8
| procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if Key = #127 then
begin
Key := #0;
CtrlBackspace2(TEdit(Sender));
end;
end; |
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if Key = #127 then
begin
Key := #0;
CtrlBackspace2(TEdit(Sender));
end;
end;
Dúvidas ou sugestões, deixe nos comentários do post.
-
Giovani Da Cruz
-
2.631 views
- 0 comentários
- 3 de dezembro de 2016
Está gostando do conteúdo? Considere pagar um cafezinho para nossa equipe!
Posts Relacionados - Continue Aprendendo