2017-12-05 14 views
2

TEdit에서 파생 된 새 구성 요소를 만들었습니다. 아래 코드에서 AllowValues를으로 설정 한 후 MaxLength 속성을 10으로 설정합니다.CustomComponent (TEDIT) 설정 MaxLength 속성이 작동하지 않습니다.

폼에 구성 요소를 설정하고 AllowValues를 true로 설정하고을 실행하고 응용 프로그램을 실행하고 편집 상자에서 10 자 이상을 허용합니다. 내 코드가 뭐가 잘못 됐어?

unit DummyEdit; 

interface 

uses 
    SysUtils, Classes, Controls, StdCtrls,Dialogs,Windows,Messages; 

type 
    TDUMMYEdit = class(TEdit) 
    private 
    { Private declarations } 
    FAllowValues : Boolean; 
    FMaxLength: Integer; 
    Procedure SetAllowValues(Value : Boolean); 
    procedure SetMaxLength(Value: Integer); 
    protected 
    { Protected declarations } 
    public 
    { Public declarations } 
    published 
    { Published declarations } 
    Property AllowValues : Boolean read FAllowValues write SetAllowValues; 
    property MaxLength: Integer read FMaxLength write SetMaxLength default 0; 
    end; 

procedure Register; 

implementation 

procedure Register; 
begin 
    RegisterComponents('DUMMY', [TDUMMYEdit]); 
end; 

{ TDUMMYEdit } 


procedure TDUMMYEdit.SetAllowValues(Value: Boolean); 
begin 
    if FAllowValues <> value then 
    FAllowValues := Value; 
    if FAllowValues then 
    MaxLength := 10 
    else 
    MaxLength := 0;  
end; 

procedure TDUMMYEdit.SetMaxLength(Value: Integer); 
begin 
    if FMaxLength <> Value then 
    begin 
    FMaxLength := Value; 
    if HandleAllocated then SendMessage(Handle, EM_LIMITTEXT, Value, 0); 
    end; 
end; 

end. 

답변

2

당신이 사용자 정의 편집을 위해MaxLength 속성을 재 발명 한 것을 깨달았다 답변에서 작업. 그것이 당신의 의도였습니까? 내가 현재 잘못한 것이 아니라면 실수로이 질문의 주제 인 버그를 소개했다는 것이 틀린 것입니다. 해당 속성을 도입하지 않았다면 코드가 작동해야합니다.

문제를 해결하는 한 가지 방법은 MaxLength 속성을 TDUMMYEdit에서 제거하고 대신 TCustomEdit 구현 도구를 사용하는 것입니다.

문제는 코드가 DFM에서 스트리밍 구성 요소 중 적용될 때 더 HandleHandleAllocated 반환 False 할당되지 않으며, 당신의 EM_LIMITTEXT 메시지가 전송되지 않을 것입니다. 나중에 AllowValues 속성을 설정합니다 (예 : 양식의 이벤트 처리기에서 예상대로 작동하고, 그 시점에서 핸들이 할당됩니다.

이 문제를 해결하는 방법이 Vcl.StdCtrlsTCustomEdit보고 찾을 수 있습니다 - 당신이 예로 들어 수도 코드를 매우 비슷합니다 - 절차 DoSetMaxLength - 보내려는 동일한 메시지를 보내드립니다 -도있을 것입니다 유효한 Handle이 생성 된 시점에 TCustomEdit.CreateWnd에서 호출되었습니다. 코드 수정 사항은 다음과 같습니다.

TDUMMYEdit = class(TEdit) 
    private 
    { Private declarations } 
    FAllowValues : Boolean; 
    FMaxLength: Integer; 
    Procedure SetAllowValues(Value : Boolean); 
    procedure SetMaxLength(Value: Integer); 
    protected 
    { Protected declarations } 
    procedure CreateWnd; override;  
    public 
    { Public declarations } 
    published 
    { Published declarations } 
    Property AllowValues : Boolean read FAllowValues write SetAllowValues; 
    property MaxLength: Integer read FMaxLength write SetMaxLength default 0; 
    end; 

procedure Register; 

implementation 

procedure Register; 
begin 
    RegisterComponents('DUMMY', [TDUMMYEdit]); 
end; 

{ TDUMMYEdit } 

... 
procedure TDUMMYEdit.CreateWnd; 
begin 
    inherited CreateWnd; 
    SendMessage(Handle, EM_LIMITTEXT, FMaxLength, 0); 
end; 
... 
+0

구성 요소 작성에 초보자이며 놀고 있습니다. 나는 Createwnd를 점검 할 것이다. – DelphiLearner

+0

내가 작동하는 MaxLength 속성을 제거하면. – DelphiLearner

+0

다행입니다. 나는 당신의 원래 코드와'CreateWnd'를 가지고 솔루션을 추가했다. – nil