Component Record - How to properly handle parameter values ​​of an Inspector object?

Quick question. I cannot find any information.

On one of my components that I create, I have a value that is of type Integer.

I need to allow only the values ​​entered in the Object Inspector, between 0-10, everything that falls outside this range should display a message to say that the entered value does not fit, and then return the focus back to the Delphi Object Inspector.

Example:

TMyComponent = class(TComponent) private FRangeVal: Integer; procedure SetRangeVal(const Value: Integer); public constructor Create(AOwner: TComponent); override; destructor Destroy; override; published property RangeVal: Integer read FRangeVal write SetRangeVal default 0; end; ... procedure SetRangeVal(const Value: Integer); var OldValue: Integer; begin OldValue := Value; if (Value < 0) or (Value > 10) then begin MessageDlg('Please enter a value between 0-10', mtError, [mbOK], 0); // FRangeVal := OldValue; ?? revert to the last value that worked // return focus back to property in Inspector ?? end else begin if Value <> FRangeVal then begin FRangeVal := Value; end; end; end; 

Do I need to raise some special built-in exception that I don’t know about, maybe? The above works with my message box, but the focus on the fault property in the Object Inspector is lost, and I need to click back again to change the value again. If the entered value is bad, I just want to show the message and return focus so that I can quickly enter the new value.

PS, the code above was written in a web browser, so the original question showed that I did not use setter SetRangeVal for the RangeVal property - it was just an input error.

+6
source share
3 answers

First, if your property can only contain values ​​between 0 and 10 , do not define it as an undefined integer property; define it as a subtype with a specific range of values:

 type TMyComponentRangeValue = 0..10; TMyComponent = class(TComponent) private FRangeVal: TMyComponentRangeValue; procedure SetRangeVal(const Value: TMyComponentRangeValue); public constructor Create(AOwner: TComponent); override; destructor Destroy; override; published property RangeVal: TMyComponentRangeValue read FRangeVal write SetRangeVal default 0; end; 

Now you can let the compiler and IDEs check valid values ​​without doing anything yourself. (The wlll IDE handles the exception and reverts to the previous value if an invalid value is entered.)

 procedure TMyComponent.SetRangeValue(const Value: TMyComponentRangeValue); begin if Value <> FRangeValue then FRangeValue := Value; end; 
+11
source

@KenWhite answers the question.
In cases that cannot be processed, you need to define your own type, you can simply raise the error instead of displaying the Messagebox.

+2
source

This is your corrected code (using the Setter method) and Integer .

You can choose between silence or with the exception of:

 {$DEFINE SILENT_SETTER} TMyComponent = class(TComponent) private FRangeVal: Integer; procedure SetRangeVal(const Value: Integer); public constructor Create(AOwner: TComponent); override; destructor Destroy; override; published property RangeVal: Integer read FRangeVal write SetRangeVal default 0; end; ... procedure SetRangeVal(const Value: Integer); begin // Range Checking if (Value < 0) or (Value > 10) then {$IFDEF SILENT_SETTER} Exit; {$ELSEIF} raise Exception.Create('Value out of Range'); {$ENDIF} // Store if needed if Value <> FRangeVal then FRangeVal := Value; end; 
+2
source

All Articles