給定TMaskEdit控制元件的EditMask屬性的此文本,
>AAAAA_AAAAA_AAAAA_AAAAA_AAAAA_AAAAA_A;0;_
用戶鍵入時,每 5 個字符后自動插入一個空格。
但是,如果用戶粘貼已包含空格的文本(例如,從我們發送給他們的電子郵件中復制),則每個空格會用完一個所需字符,并且文本的最后 5 個字符會丟失。
有沒有辦法識別TEditMask中的特定字符,使其為空或特定字符(在本例中為空格)?或者我可以使用不同的控制元件嗎?
uj5u.com熱心網友回復:
不要使用TMaskEdit它可怕的光學和限制。由于您要操作的文本相當短,您可以TEdit直接使用 a 并對其文本的更改做出反應:
- 始終使文本大寫。
- 不要依賴鍵盤和滑鼠輸入。
- 出于視覺原因,故意殺死所有空間并將它們放在您想要的任何地方。
也不要錯誤地TEdit為每個文本塊使用幾個 s,因為這會破壞任何想要一次粘貼長序列(?)的剪貼板內容的人 - 我已經看到軟體安裝這樣做了,它是總是很痛苦。
有一個空表單,添加一個TEdit并將其OnChange設為事件:
procedure TForm1.Edit1Change( Sender: TObject );
var
edt: TEdit; // Easier access
sText: String; // Easier access
iLen, iPos, iCur: Integer; // Text length, Character to inspect, Text cursor position
begin
// Sender might be nil in rare conditions
if not (Sender is TEdit) then exit;
edt:= Sender as TEdit;
// Empty texts don't need our care
iLen:= Length( edt.Text );
if iLen= 0 then exit;
// I guess you always want big letters
sText:= UpperCase( edt.Text );
// Kill all spaces, so it doesn't matter how many are in there
iCur:= edt.SelStart; // Remember text cursor position
iPos:= Pos( ' ', sText ); // Find first occurance
while iPos> 0 do begin
Delete( sText, iPos, 1 );
Dec( iLen );
if iCur>= iPos then Dec( iCur ); // Was text cursor after that spot? Should move, too.
iPos:= Pos( ' ', sText ); // Find next occurance
end;
iPos:= 5; // Character of the text to inspect
while iPos< iLen do begin // Much better than "<=", credit: Tom Brunberg
if sText[iPos 1]<> ' ' then begin // Next character is not a space?
Insert( ' ', sText, iPos 1 ); // Insert space
if iCur> iPos then Inc( iCur ); // Was text cursor after that spot? Should move, too.
Inc( iLen ); // Text size has been increased by us
end;
Inc( iPos, 6 ); // Go to next end of a "block"
end;
edt.OnChange:= nil; // Otherwise setting .Text will trigger this event again
edt.Text:= sText; // We're done
edt.OnChange:= Edit1Change;
edt.SelStart:= iCur; // Recover/fix text cursor
end;
在 D7 上成功測驗:

只洗掉單個字符感覺很奇怪,但是,當最后一個是空格時不起作用. I'll leave it to you to make it perfect.
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/441860.html
標籤:德尔福
