6.4.7.2 Delete Directory
In implementing directory deletion, it is far less successful than creating a directory. The problems encountered are:
1.RmDir The current directory is not allowed to be deleted. But in order to operate conveniently, we request the deletion is precisely the current directory;
2. Calling the Refresh method or the Update method after a directory deletion does not remove the directory from the screen display. As a result, a user attempting to enter the directory can cause the system to crash.
For the first question, our solution is to convert the current directory to its parent directory. If the reader remembers that the directory is being treated as a special file by the operating system, then the following statement will not be surprised:
Path: = Directoryoutline.directory;
Directoryoutlin.directory: = Expandfilepath (Path);
And then call the RMDIR process:
RmDir (Path);
The solution to the second problem is rather troubling. Because Directoryoutline is the sample part provided by Delphi, there is no Help file support. The experiment found that only when the Directoryoutline drive property changed, the directory was read back from the corresponding drive. And it's basically read-only, except that it's cleared (clear), like add, delete, and these methods are not valid for it.
I have considered a clumsy way to change the current drive and then change it back. But this approach is intolerable on the one hand, and the system crashes when only a single drive is available.
Just when I was helpless, I suddenly thought: Directoryoutline is a sample part, Delphi provides its source code. And when I analyzed its source code, I know what to do, that is to add a reset method for Directoryoutline!
6.7.3 adds a method to the part
Strictly speaking, the work we do belongs to creating a new part. But because we have the source code, we don't have to inherit from Directoryoutline but modify it directly. This allows us to omit many of the tedious work associated with creating a part. Readers interested in creating new parts can read the relevant chapters in part III of this book.
After opening the Directoryoutline source file in the Delphi IDE:
1. Change the library unit name to Dirplus and the class name to Tdirectoryoutlineplus, indicating that this is a directoryoutline enhanced version. Then deposited into another directory;
2. Add a public method reset. The purpose of this method is to reread the directory of the current drive. The list of procedures is as follows.
Procedure Tdirectoryoutlineplus.reset;
Begin
ChDir (fdrive + ': ');
Getdir (0, fdirectory);
Fdirectory: = Forcecase (fdirectory);
If not (csloading in componentstate) then Buildtree;
End
The reader may be confused by the code. We are not prepared to explain it in detail because of space constraints and the content of many custom component developments. If the reader wants to understand it thoroughly, I suggest that we take a look at the third part of the book on the development of custom parts, and then compare the original directoryoutline source code analysis.
3. Compile into a library file dirplus.tpu;
4. Add Dirplus to the Samples page of the part.
How to add a part see section III for an introduction to the chapter.
When an enhanced directory tree is ready, you must modify our child window design without having to modify the source code yourself.
1. Delete the Tdirectoryoutline class part directoryoutline in the child window. At this time filelist occupied the entire client area;
2. Change the align genus of the filelist to none and leave the left blank for the spare parts;
3. Add the parts of the Tdirectoryoutlinplus class to the left part of the window directoryoutline;
4. Convert the Align property of directoryoutline to left,filelist align attribute to client;
5. Select Directoryoutlinechange in the Directoryoutline event onchange list, which is the process of the original directoryoutline.
The ultimate goal of the above work is to implement the directory creation, delete the correct display of the screen. This only needs to call the Directoryoutline reset method.
The implementation code for the directory deletion process is as follows.
Procedure Tfmform.deletedirectory1click (Sender:tobject);
Var
path:string;
K:integer;
Begin
{$I-}
Path: = Directoryoutline.directory;
Directoryoutline.directory: = Extractfilepath (Path);
If Messagedlg (' Delete ' + path + '? ', Mtconfirmation,[mbyes, Mbno], 0) = Idyes Then
RmDir (path);
If IOResult <> 0 Then
Messagedlg (' cannot remove directory! The path might not ' +
' Exist,non-empty or is the current logged directory. ', Mtwarning,[mbok], 0
Else
Directoryoutline.reset;
End
The modified directory creation process is as follows.
Procedure Tfmform.createdirectory1click (Sender:tobject);
Var
Newdir:tnewdir;
dir:string;
Begin
{$I-}
Newdir: = Tnewdir.create (self);
With Newdir do
Begin
Currentdir.caption: = directoryoutline.directory;
if (ShowModal <> idCancel) and (Dirname.text <> ") Then
Dir: = Expandfilename (Dirname.text);
End
MkDir (Dir);
If IOResult <> 0 Then
Messagedlg (' cannot Create directory ', mtwarning, [Mbok], 0)
Else
Directoryoutline.reset;
End
When the work is done and the program is recompiled and run, we can see that the functionality we want to implement is fully realized! At the same time, we have a better directory tree widget.