softtouch 9 Posted April 30, 2023 I need to prevent that user can add a duplicate key in the valuelisteditor. For that, I set the property keyUnique in KeyOptions. Unfortunately, it trigger an exception, and I cant find a way to catch this exception. There is no event for when a key exist or the exception is triggered. Howe can I catch this so that the program wont terminate due to that exception? Share this post Link to post
programmerdelphi2k 237 Posted April 30, 2023 (edited) procedure TForm1.Button1Click(Sender: TObject); var LRow: Integer; begin ValueListEditor1.KeyOptions := [keyUnique]; // quickly... { if not ValueListEditor1.Strings.Text.Contains('key1=') then // function TValueListStrings.KeyIsValid(const Key: string; RaiseError: Boolean = True): Boolean; ValueListEditor1.InsertRow('key1', 'hello', true); } // if (keyUnique in ValueListEditor1.KeyOptions) and ValueListEditor1.FindRow('key1', LRow) then begin if (LRow = -1) then ValueListEditor1.InsertRow('key1', 'hello', true) end else ValueListEditor1.InsertRow('key1', 'hello', true); // Memo1.Text := ValueListEditor1.Strings.Text; end; Edited April 30, 2023 by programmerdelphi2k Share this post Link to post
Remy Lebeau 1397 Posted April 30, 2023 TValueListEditor has a KeyIsValid() method, which has a RaiseError property. This is the same method that TValueListEditor calls internally, with RaiseError=True, but you can also call it yourself, with RaiseError=False, eg: procedure TForm1.Button1Click(Sender: TObject); var if TValueListStrings(ValueListEditor1.Strings).KeyIsValid('key1', False) then ValueListEditor1.InsertRow('key1', 'hello', true); end; Share this post Link to post
softtouch 9 Posted April 30, 2023 11 minutes ago, Remy Lebeau said: TValueListEditor has a KeyIsValid() method, which has a RaiseError property. This is the same method that TValueListEditor calls internally, with RaiseError=True, but you can also call it yourself, with RaiseError=False, eg: procedure TForm1.Button1Click(Sender: TObject); var if TValueListStrings(ValueListEditor1.Strings).KeyIsValid('key1', False) then ValueListEditor1.InsertRow('key1', 'hello', true); end; Thanks for that. But the user edit the key in the valuelisteditor grid, Ido not add the key with insertrow or such. There are existing entries, the user select the entry to edit and change it, and when the user leaves the cell (with mouse or keyword/arrow keys for example), the exception is triggered. Share this post Link to post
PeterBelow 238 Posted May 1, 2023 19 hours ago, softtouch said: Thanks for that. But the user edit the key in the valuelisteditor grid, Ido not add the key with insertrow or such. There are existing entries, the user select the entry to edit and change it, and when the user leaves the cell (with mouse or keyword/arrow keys for example), the exception is triggered. Try to handle the OnSetEditText or OnValidate events. Share this post Link to post
Remy Lebeau 1397 Posted May 1, 2023 (edited) On 4/30/2023 at 9:45 AM, softtouch said: Unfortunately, it trigger an exception, and I cant find a way to catch this exception. There is no event for when a key exist or the exception is triggered. Like Peter said, try the OnSetEditText or OnValidate event. OnSetEditText is called when a new grid cell value is being saved. It is called before the cell is updated, and before the new value is validated. OnValidate is called if the InplaceEditor is active and modified when 1) before a new cell is selected, 2) when the ValueListEditor is losing input focus, 3) when the mouse is being pressed down on the ValueListEditor. Either way, you can raise an exception if the user input is not acceptable to you. For instance, you can raise a custom exception that a TApplication.OnException handler can look for, or you can call SysUtils.Abort() to raise an EAbort exception, which the VCL will catch and discard. You could handle the ValueListEditor's default keyUnique exception in the TApplication.OnException event. However, TValueListEditor does not use a specialized descendant class for that particular exception, it just uses the base SysUtils.Exception class directly. So, to differentiate that exception from others, you would have to look at its Message text, which is the localizable resource string "SKeyConflict" from the Vcl.Consts unit: SKeyConflict = 'A key with the name of "%s" already exists'; Quote Howe can I catch this so that the program wont terminate due to that exception? The exception will not terminate the program. It is being raised in a UI handler in the context of the main message loop, which will catch any uncaught exception, and either pass it to the TApplication.OnException event if assigned or just display it to the user. Either way, the exception is then discarded. Edited May 1, 2023 by Remy Lebeau Share this post Link to post
softtouch 9 Posted May 1, 2023 @Remy LebeauIndeed, it does not kill the exe, when madexcept is turned off. So that was the problem, because madexcept terminate the exe after showing its exception dialog. I now turned it off and it will just show an error that the value already exist. Thanks for the help! Share this post Link to post
Stano 143 Posted May 1, 2023 1 minute ago, softtouch said: So that was the problem, because madexcept terminate the exe after showing its exception dialog. You can certainly set what should happen after the dialog is closed. Eurekalog has it. Check it out. Turning off the whole madexcept is nonsense. Share this post Link to post