2017-12-05 19 views
2

私はTEditから派生した新しいコンポーネントを作成しました。以下のコードでは、AllowValuesをtrueに設定した後、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からのストリーミングコンポーネントの間に効果を取るとき、何Handleは、HandleAllocated戻り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

関連する問題