TEdit maxlength for Multibyte character lazarus / 21-General ---------------------------------------------------------------------- meeeeeeeeee · 2024-08-06 06:54 Hello. I'm using Lazarus v.2.2.6. I paste TEdit on a Form and set TEdit maxlength to 6, for example. In Delphi when I type Multibyte character on TEdit, I can do like this ok:123456 ok:123 In Lazarus when I do the same things, it is different from Delphi. Half-width character and Multibyte character is the same number. [123456] and [123456] is counted as 6 on TEdit of Lazarus. I want to limit Multibyte character letters like Delphi in Lazarus TEdit. I tried to do like this... procedure TForm1.Edit6Change(Sender: TObject); var    xMaxLen:integer;    wdr1:widestring;  st1:string;  i, xNowPosition:integer; begin  xMaxLen := Edit6.MaxLength;  if xMaxLen <= 0 then    Exit;  if Length(UTF8towinCp(Edit6.Text)) <= xMaxLen then begin   Exit;  end;  wdr1:=Edit6.Text;  xNowPosition :=0;  for i:=1 to Length(wdr1) do begin    st1 :=wdr1;    xNowPosition :=xNowPosition+Length(UTF8towinCp(st1));    if xNowPosition>xMaxLen then begin        xNowPosition:=xNowPosition-Length(UTF8towinCp(st1));        break;    end;  end;  Edit6.Text :=winCpToUTF8(Copy(UTF8towinCp(Edit6.Text), 1, xNowPosition));  Edit6.SelLength := 0;  Edit6.SelStart  :=xMaxLen; end; But It doesn't work well. When I type Multibyte letters from the begining, letters are overwrite. How can I modify maxlength of TEdit of Lazarus like Delphi? Best regards. ---------------------------------------------------------------------- wp · 2024-08-06 07:17 I don't have Laz 2.2.6 available at the moment. But in Laz 3.4 the Edit is working correctly: When I drop a TEdit on a form, set its MaxLength to 6, run and type "1 2 3 4 5 6 " (with spaces) the string is truncated before the "4". And when I type a string with UTF8 characters "äöüß1234" it is truncated after the "2" - as expected. Normally you do not have to care about "widestrings" in Lazarus. Simply use "string". Only in a few cases (Length, Copy, Pos, so so) you must use the UTF8 versions of these functions from unit LazUTF8. ---------------------------------------------------------------------- Bart · 2024-08-06 12:52 Characters outside of the BMP (Basic Multilingual Plane) might be a problem though (they are counted more than 1 when using SelStart/SelLength, which is a Windows "feature". You actually can select half of such a charater.) Bart ---------------------------------------------------------------------- meeeeeeeeee · 2024-08-07 07:26 I also have Laz 3.2, and tried the same things, but  the result was same. I take this as Lazarus feature. Thank you for replying me Bart and wp ---------------------------------------------------------------------- Bart · 2024-08-07 11:25 @meeeeeeeeee: please use [code][/code] tags to properly format you code. I'm not sure what you are trying to accomplish in your code. Basically, if MaxLength <> 0, then the code should do nothing at all. Also, why all the conversions (Utf8 <-> WinCP)? If you want to know the "length in visual characters" (i.e. ä counts as 1 visual character), simply use Utf8Length(). Quote[123456] and [123456] is counted as 6 on TEdit of Lazarus. I'm not sure what you mean by that. I see [123456] as a followed by a space, followed by 2, followed by a space etc., yet you talk about these as if they were multi-byte characters. When I try with MaxLength = 6, I can type e.g. '日本語日本語' into the control, and after that nothing. Utf8Length=6 Length=18  // 3 bytes per character Length(UTF8towinCp(Text)=6 And Utf8ToWinCP(Edit6.Text)=??????  //the characters of the string in question cannot be translated to my Windows codepage, so they will become the '?' character. Also, you access all individual bytes of Edit6.Text and perfom a Utf8ToWinCP() on them. This is in itself totally non-sensical: the individual bytes of an UTF-8 encoded string have no meaning in itself as a "character". And then you take the Length() of this??? Notice that Utf8ToWinCP with a single-byte character as input, will always return a 1-byte chacater: either Utf8ToWinCP will return the same character (if that charater is in the lower ASCII range), and therefor Length will be 1, orUtf8ToWinCP will return '?' as the input character is not valid UTF8, and again Length will be 1 Bart ---------------------------------------------------------------------- meeeeeeeeee · 2025-01-29 02:32 This is my self response. I created a custom TEdit component to limit input Multibyte character when characters reach the maxlength. I almost satisfied with this. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit setmaxlengtheditexa; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, StdCtrls,  Clipbrd, LazUtf8, Windows,LCLType, IMM, Menus;type  TImeMode=(ioHira, ioFullKata, ioHalfkata, ioClose);  Tsetmaxlengtheditexa = class(TEdit)  private    FImeMode         : TImeMode;    FPopupMenu     : TPopupMenu;    MenuItemCopy     : TMenuItem;    MenuItemPaste    : TMenuItem;    MenuItemCut      : TMenuItem;    MenuItemCloseIME : TMenuItem;    MenuI]"]>BlockedpenIME  : TMenuItem;    MenuI]"]>Blockedndo     : TMenuItem;    MenuItemDelete   : TMenuItem;    MenuItemSelectAll: TMenuItem;     procedure SetImeMode(Value: TImeMode);    procedure CreatePopupItems;     procedure MenuItemCopyClick(Sender: TObject);    procedure MenuItemPasteClick(Sender: TObject);    procedure MenuItemCutClick(Sender: TObject);    procedure MenuItemCloseIMEClick(Sender: TObject);    procedure MenuI]"]>BlockedpenIMEClick(Sender: TObject);    procedure MenuI]"]>BlockedndoClick(Sender: TObject);    procedure MenuItemDeleteClick(Sender: TObject);    procedure MenuItemSelectAllClick(Sender: TObject);    function  IsIMEOpen: Boolean;  protected    procedure DoEnter; override;    procedure Change; override;    procedure KeyDown(var Key: Word; Shift: TShiftState); override;    procedure UTF8KeyPress(var UTF8Key: TUTF8Char); override;        procedure SetImeModeOpen;    procedure SetIMEOpenClose(xOpen: Boolean);        procedure PopupMenuOnPopup(Sender: TObject); virtual;  public    constructor Create( AOwner: TComponent ); override;   published        property ImeMode: TImeMode read FImeMode write SetImeMode default ioClose;    property PopupMenu;     property OnKeyDown;    property OnUTF8KeyPress;  end; procedure Register; implementation procedure Register;begin  RegisterComponents('MyCompo',[TSetMaxLengthEditExA]);end;  constructor TSetMaxLengthEditExA.Create(AOwner : TComponent);begin  Inherited Create( AOwner );  FImeMode     :=ioClose;  ControlStyle := ControlStyle - [csSetCaption];  Text := '';   if not(csDesigning in ComponentState) then begin     FPopupMenu :=TPopupMenu.Create(Self);     FPopupMenu.OnPopup := @PopupMenuOnPopup;    CreatePopupItems;     PopupMenu  :=FPopupMenu;  end;end; procedure TSetMaxLengthEditExA.Change;var   st1 :string;   xMaxLen, i, xNowPosition:integer;begin  xMaxLen :=MaxLength;  if xMaxLen <= 0 then    Exit;   if Length(UTF8towinCp(Text)) <= xMaxLen then   Exit;   xNowPosition     :=0;   for i := 1 to UTF8Length(Text) do  begin     st1           :=UTF8Copy(Text, i, 1);      xNowPosition  :=xNowPosition+Length(UTF8towinCp(st1));     if xNowPosition>xMaxLen then begin       xNowPosition:=xNowPosition-Length(UTF8towinCp(st1));       break;     end;   end;   Text :=winCpToUTF8(Copy(UTF8towinCp(Text), 1, xNowPosition));   Invalidate;   Inherited;end; procedure TSetMaxLengthEditExA.KeyDown(var Key: Word; Shift: TShiftState);var  xText_Edit, xSelText_Edit, xReplaceText_Edit:string;   xMaxLength_Edit, xStartPos_Edit:integer;begin  inherited;  if Shift = [ssCtrl] then begin     Case Key of     $43, $63:  // Ctrl + 'C'       begin         if SelLength > 0 then             CopyToClipboard;          end;     $58, $78:  // Ctrl + 'X'       begin         //Memo1.Lines.Add(xMsg+' Ctrl + X '+Edit.Text);         CutToClipboard;               end;     $56, $76:       begin // CTrl + 'V'         if not Clipboard.HasFormat(CF_TEXT) then  exit;         xMaxLength_Edit  :=MaxLength;         xText_Edit       :=Text;       xSelText_Edit    :=SelText;         xStartPos_Edit   :=SelStart;         xReplaceText_Edit:=xText_Edit;          UTF8Delete(xReplaceText_Edit, xStartPos_Edit+1, UTF8Length(xSelText_Edit));          UTF8Insert(Clipboard.AsText, xReplaceText_Edit, xStartPos_Edit+1);             if xMaxLength_Edit <= 0 then begin             Exit;         end;         if Length(UTF8towinCp(xReplaceText_Edit)) > xMaxLength_Edit then begin          MessageBeep(MB_ICONEXCLAMATION);             Key:=0;         end;       end;     end;   end;end; procedure TSetMaxLengthEditExA.UTF8KeyPress(var UTF8Key: TUTF8Char);var  xCurrentText:string;  xNewLength  :integer;begin  inherited;  xCurrentText   :=Text;  if MaxLength <= 0 then begin     Exit;  end;  If (UTF8Key <> ^C) and (UTF8Key <> ^V) and (UTF8Key <> ^X) and (UTF8Key <> #8) and (SelLength=0)then begin       xCurrentText := xCurrentText + UTF8Key;  end;  xNewLength     :=Length(UTF8towinCp(xCurrentText));    if Length(UTF8towinCp(xCurrentText)) > MaxLength then begin     MessageBeep(MB_ICONEXCLAMATION);      UTF8Key     :=#0;    end;end;procedure TSetMaxLengthEditExA.DoEnter;begin  inherited;   SetImeModeOpen;end; procedure TSetMaxLengthEditExA.CreatePopupItems;begin  MenuI]"]>Blockedndo           :=TMenuItem.Create(FPopupMenu);  MenuI]"]>Blockedndo.Caption   :='Undo(&U)';  MenuI]"]>Blockedndo.OnClick     :=@MenuI]"]>BlockedndoClick;  FPopupMenu.Items.Add(MenuI]"]>Blockedndo);   MenuItemCut              :=TMenuItem.Create(FPopupMenu);  MenuItemCut.Caption      :='Cut(&T)';  MenuItemCut.OnClick      :=@MenuItemCutClick;  FPopupMenu.Items.Add(MenuItemCut);   MenuItemCopy             :=TMenuItem.Create(FPopupMenu);  MenuItemCopy.Caption     :='Copy(&C)';  MenuItemCopy.OnClick     :=@MenuItemCopyClick;  FPopupMenu.Items.Add(MenuItemCopy);   MenuItemPaste            :=TMenuItem.Create(FPopupMenu);  MenuItemPaste.Caption    :='Paste(&P)';  MenuItemPaste.OnClick    :=@MenuItemPasteClick;  FPopupMenu.Items.Add(MenuItemPaste);   MenuItemDelete           :=TMenuItem.Create(FPopupMenu);  MenuItemDelete.Caption   :='Delete(&D)';  MenuItemDelete.OnClick   :=@MenuItemDeleteClick;  FPopupMenu.Items.Add(MenuItemDelete);   MenuItemSelectAll        :=TMenuItem.Create(FPopupMenu);  MenuItemSelectAll.Caption:='SelectAll(&A)';  MenuItemSelectAll.OnClick:=@MenuItemSelectAllClick;  FPopupMenu.Items.Add(MenuItemSelectAll);   MenuI]"]>BlockedpenIME        :=TMenuItem.Create(FPopupMenu);  MenuI]"]>BlockedpenIME.Caption  :='mOpenIME(&O)';  MenuI]"]>BlockedpenIME.OnClick  :=@MenuI]"]>BlockedpenIMEClick;  FPopupMenu.Items.Add(MenuI]"]>BlockedpenIME);   MenuItemCloseIME       :=TMenuItem.Create(FPopupMenu);  MenuItemCloseIME.Caption :='CloseIME(&L)';  MenuItemCloseIME.OnClick :=@MenuItemCloseIMEClick;  FPopupMenu.Items.Add(MenuItemCloseIME); end; procedure TSetMaxLengthEditExA.PopupMenuOnPopup(Sender: TObject);begin  MenuItemCopy.Enabled       :=SelLength > 0;   MenuItemPaste.Enabled    :=Clipboard.HasFormat(CF_TEXT);  MenuItemCut.Enabled        :=MenuItemCopy.Enabled;  MenuItemDelete.Enabled     :=MenuItemCopy.Enabled;  MenuI]"]>Blockedndo.Enabled       :=CanUndo;  MenuItemSelectAll.Enabled  :=UTF8Length(Text) > 0;   if IsIMEOpen then begin    MenuI]"]>BlockedpenIME.Visible  :=False;    MenuItemCloseIME.Visible :=True;  end  else begin    MenuI]"]>BlockedpenIME.Visible  :=True;    MenuItemCloseIME.Visible :=False;  end; end; procedure TSetMaxLengthEditExA.MenuI]"]>BlockedndoClick(Sender: TObject);begin  if CanUndo then    Undo;end; procedure TSetMaxLengthEditExA.MenuItemCutClick(Sender: TObject);begin  CutToClipboard;end; procedure TSetMaxLengthEditExA.MenuItemCopyClick(Sender: TObject);begin  if SelLength > 0 then    CopyToClipboard; end; procedure TSetMaxLengthEditExA.MenuItemPasteClick(Sender: TObject);var  xText_Edit, xSelText_Edit, xReplaceText_Edit:string;   xMaxLength_Edit, xStartPos_Edit:integer;begin  if Clipboard.HasFormat(CF_TEXT)   then begin    xMaxLength_Edit  :=MaxLength;    xText_Edit       :=Text;    xSelText_Edit    :=SelText;    xStartPos_Edit   :=SelStart;    xReplaceText_Edit:=xText_Edit;     UTF8Delete(xReplaceText_Edit, xStartPos_Edit+1, UTF8Length(xSelText_Edit));     UTF8Insert(Clipboard.AsText, xReplaceText_Edit, xStartPos_Edit+1);   if xMaxLength_Edit <= 0 then begin        PasteFromClipboard;       Exit;    end;     if Length(UTF8towinCp(xReplaceText_Edit)) > xMaxLength_Edit then begin      MessageBeep(MB_ICONEXCLAMATION);    end    else  PasteFromClipboard;  end;end; procedure TSetMaxLengthEditExA.MenuItemDeleteClick(Sender: TObject);begin  if SelLength > 0 then    ClearSelection; end; procedure TSetMaxLengthEditExA.MenuItemSelectAllClick(Sender: TObject);begin    SelectAll;    Invalidate;end; procedure TSetMaxLengthEditExA.MenuI]"]>BlockedpenIMEClick(Sender: TObject);begin  SetIMEOpenClose(True);end;procedure TSetMaxLengthEditExA.MenuItemCloseIMEClick(Sender: TObject);begin  SetIMEOpenClose(False);end; procedure TSetMaxLengthEditExA.SetImeMode(Value: TImeMode);begin  if FImeMode <> Value then begin    FImeMode := Value;    SetImeModeOpen;  end;end;  function TSetMaxLengthEditExA.IsIMEOpen: Boolean;var  IMC: HIMC;begin  IMC := ImmGetContext(Handle);  if IMC <> 0 then  begin    Result := ImmGetOpenStatus(IMC);    ImmReleaseContext(Handle, IMC);  end  else    Result := False;end;procedure TSetMaxLengthEditExA.SetIMEOpenClose(xOpen: Boolean);var  IMC: HIMC;begin  IMC := ImmGetContext(Handle);  if IMC <> 0 then  ImmSetOpenStatus(IMC, xOpen);  ImmReleaseContext(Handle, IMC);end; procedure TSetMaxLengthEditExA.SetImeModeOpen;var  IMC: HIMC;  dwConversion,dwSentence : DWORD;begin  IMC := ImmGetContext(Handle);  if IMC <> 0 then begin    if FImeMode=ioClose then ImmSetOpenStatus(IMC, False)     else begin      ImmSetOpenStatus(IMC, True);       case FImeMode of        ioHira    : dwConversion :=IME_CMODE_NATIVE   or IME_CMODE_FULLSHAPE;         ioFullKata: dwConversion :=IME_CMODE_KATAKANA or IME_CMODE_NATIVE or IME_CMODE_FULLSHAPE;         ioHalfkata: dwConversion :=IME_CMODE_NATIVE   or IME_CMODE_KATAKANA;        end;      dwSentence   := IME_SMODE_NONE;       ImmSetConversionStatus(Imc, dwConversion, dwSentence);    end;  end;  ImmReleaseContext(Handle, IMC); end; initialization  {$I setmaxlengtheditexapack.lrs} end.