Recent

Author Topic: [Solved] Validate an item in ShellTreeView  (Read 2210 times)

nikel

  • Full Member
  • ***
  • Posts: 186
[Solved] Validate an item in ShellTreeView
« on: April 20, 2019, 08:34:08 pm »
Hi, I have one ShellTreeView, one ShellListView and a splitter on my form. I want to prevent my program raising exception and exiting. How can I check if a path is valid and continue without error? Here's my code so far:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.ShellTreeView1SelectionChanged(Sender: TObject);
  2. begin
  3.   ShowMessage('Changing item');
  4.   if (not Assigned(ShellTreeView1.Selected)) then
  5.   begin
  6.     ShowMessage('Error');
  7.     Halt(1);
  8.     if (not Assigned(ShellTreeView1.Selected.Items[0])) then
  9.     begin
  10.       ShowMessage('Error');
  11.       Halt(1);
  12.     end;
  13.   end;
  14. end;

MODIFY: "if" statements aren't executed when I run my program.
« Last Edit: April 21, 2019, 07:07:02 pm by nikel »

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Validate an item in ShellTreeView
« Reply #1 on: April 20, 2019, 10:29:35 pm »
Hi, I have one ShellTreeView, one ShellListView and a splitter on my form. I want to prevent my program raising exception and exiting. How can I check if a path is valid and continue without error? Here's my code so far:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.ShellTreeView1SelectionChanged(Sender: TObject);
  2. begin
  3.   ShowMessage('Changing item');
  4.   if (not Assigned(ShellTreeView1.Selected)) then
  5.   begin
  6.     ShowMessage('Error');
  7.     Halt(1);
  8.     if (not Assigned(ShellTreeView1.Selected.Items[0])) then
  9.     begin
  10.       ShowMessage('Error');
  11.       Halt(1);
  12.     end;
  13.   end;
  14. end;

Where is it raising the exception? Surely not in that code if it's not executed!

Anyway, by the time OnSelectionChanged is shot the ShellTreeView is already populated from the file system, so the selected item must exist or it wouldn't be there. But if you want to make sure you can simply test ShellTreeView.SelectionCount.

Also note that in your code:
Code: [Select]
if (not Assigned(ShellTreeView1.Selected.Items[0])) then ...is only executed after you have already determined that ShellTreeView1.Selected is Nil, which makes little sense ... and will raise an exception

Try this instead:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.ShellTreeView1SelectionChanged(Sender: TObject);
  2. begin
  3.   ShowMessage('Changing item');
  4.   if ShellTreeView.SelectionCount < 1 then begin
  5.     ShowMessage('Error: SelectionChanged called but no item selected!!!');
  6.     //Halt(1); { Unless you're absolutely sure of what you're doing, never use Halt on a GUI app}
  7.     Application.Terminate(1);
  8.   end else
  9.     if ShellTreeView1.Selected.Count < 1 then begin
  10.       ShowMessage('Error: Some item selected but Selected.Items[0] = nil');
  11.       Application.Terminate(2);
  12.     end;
  13. end;

I don't understand well what you're trying to do through that, though, because when you get down to a leaf directory (one which has no directories under it) Selected.Count will be zero (or Selectec.Items[0] will be Nil), as it should, and one can't count that as an error.

I guess that your code is just a test and you intend to do some other thing in that handler?


ETA: To get better help it would be best if you share your program (or a significant portion of it) so that we can see where it's failing.

BTW, when you say: "if statements aren't executed when I run my program", do yo mean the "Changing" mesage appears but the ifs are not executed? If so, that's ... very difficult to understand.
« Last Edit: April 20, 2019, 10:39:57 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

ASerge

  • Hero Member
  • *****
  • Posts: 2240
Re: Validate an item in ShellTreeView
« Reply #2 on: April 20, 2019, 10:36:57 pm »
I want to prevent my program raising exception and exiting. How can I check if a path is valid and continue without error?
...
MODIFY: "if" statements aren't executed when I run my program.
Clarify the problem: an exception is raised (which one?) or the "if" operator is not executed?

nikel

  • Full Member
  • ***
  • Posts: 186
Re: Validate an item in ShellTreeView
« Reply #3 on: April 21, 2019, 02:03:11 am »
It's nice to get some help. I meant it doesn't get in "if" clause so it doesn't display a message
Code: Pascal  [Select][+][-]
  1. ShowMessage('Error');

I tried this:

Code: Pascal  [Select][+][-]
  1. ShowMessage('Changing item');
  2. if (ShellTreeView1.SelectionCount < 1) then
  3. begin
  4.   ShowMessage('Error: SelectionChanged called but no item selected!!!');
  5.   Application.Terminate;
  6. end;
  7. if (ShellTreeView1.Selected.Count < 1) then
  8. begin
  9.   ShowMessage('Error: Some item selected but Selected.Items[0] = nil');
  10.   Application.Terminate;
  11. end;

That works perfect. I don't want the program to exit but continue as usual. Do I have to raise a custom event for selecting items? Here's some screenshots:


« Last Edit: April 21, 2019, 02:07:15 am by nikel »

jamie

  • Hero Member
  • *****
  • Posts: 6128
Re: Validate an item in ShellTreeView
« Reply #4 on: April 21, 2019, 02:33:29 am »
maybe you need to test the path the shell provided using the "FileExists" function.

That will report if the path\FileName is valid.

 Other than that I don't know ?
The only true wisdom is knowing you know nothing

ASerge

  • Hero Member
  • *****
  • Posts: 2240
Re: Validate an item in ShellTreeView
« Reply #5 on: April 21, 2019, 08:39:11 am »
Do I have to raise a custom event for selecting items? Here's some screenshots:
Understood. For devices that are missing, such as a DVD-ROM, attempting to set the root throws an exception because the directory does not exist.
You can check this in OnSelectionChanged and perform the required action. For example here it is not possible to select such a disc:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.ShellTreeView1SelectionChanged(Sender: TObject);
  2. begin
  3.   if Assigned(ShellTreeView1.Selected) and not DirectoryExists(ShellTreeView1.Path) then
  4.     ShellTreeView1.Selected := nil;
  5. end;

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Validate an item in ShellTreeView
« Reply #6 on: April 21, 2019, 09:56:21 am »
Here is a more complete solution that will return to the previous selected path if the current doesn't exists. Not the most elegant code in the world but it should do the trick:
Code: Pascal  [Select][+][-]
  1. { Add a "StoredPath: String;" field to your form and
  2.   initialize it on the form's OnCreate handler to either
  3.   ShellTreeeView1.Path, if you set it to something,
  4.   or to an empty string ('') if not.}
  5.  
  6. procedure TForm1.ShellTreeView1SelectionChanged(Sender: TObject);
  7. begin
  8.   if Assigned(ShellTreeView1.Selected) then begin
  9.     if DirectoryExists(ShellTreeView1.Path) then
  10.       {Store it so we can return to it if needs be}
  11.       StoredPath := ShellTreeView1.Path
  12.     else begin
  13.       {If we stored something, return to it; if not, just give up :) }
  14.       if StoredPath <> '' then
  15.         ShellTreeView1.Path := StoredPath
  16.       else
  17.         ShellTreeView1.Selected := nil;
  18.   end;
  19. end;
« Last Edit: April 21, 2019, 09:58:15 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

nikel

  • Full Member
  • ***
  • Posts: 186
Re: Validate an item in ShellTreeView
« Reply #7 on: April 21, 2019, 07:06:01 pm »
Hi, Lucamar I had to change your code a little bit because of collapse / expand issues. Here's my final code, I couldn make it select the item virtually and highlight (as if nothing wrong happened) however it displays some alerts. Thanks for all replies.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.ShellTreeView1Changing(Sender: TObject; Node: TTreeNode;
  2.   var AllowChange: Boolean);
  3. begin
  4.   StoredNode:=Node;
  5. end;
  6.  
  7. procedure TForm1.ShellTreeView1SelectionChanged(Sender: TObject);
  8. begin
  9.   if (Assigned(ShellTreeView1.Selected)) then
  10.   begin
  11.     if (DirectoryExists(ShellTreeView1.Path)) then
  12.     begin
  13.       {Directory exists}
  14.     end
  15.     else
  16.     begin
  17.       StoredNode.Selected:=True;
  18.       ShellTreeView1.Selected:=nil;
  19.       ShowMessage('Directory does''t exist');
  20.     end;
  21.   end
  22.   else
  23.     ShowMessage('Selection not assigned');
  24. end;
« Last Edit: April 21, 2019, 07:14:48 pm by nikel »

 

TinyPortal © 2005-2018