Because Delphi adopts a truly completely object-oriented approach, the fastest compiler in the world today, and leading database technology, especially her full compatibility with Windows API, and excellent code automatic generation capabilities... people have seen that in just a short time In the past few years, Delphi has stood out among many development tools! It is the many excellent qualities of Delphi that enable programmers to develop professional-level application software in a short period of time, while doing very little work!
As the saying goes, Let's us make better! How should we make full use of the advantages of Delphi to design programs more quickly?
1. Make full use of complete code sharing (Jubang):
The purpose of this is not just to be "lazy", it is also a way to optimize the program code. Of course, the first thing to consider is whether it can be shared (this is very simple, see if it is included in the drop-down collection of the event), and the second thing to pay attention to is whether there will be "trouble" after sharing, because correctness is the first step. It is necessary to pursue optimization only when it is guaranteed to be correct!
For example: we give a confirmation message before the user deletes data:
PRocedure TForm1.Table1BeforeDelete(DataSet: TDataSet);
begin
//Confirm deletion record
if MessageBox(handle,'Are you sure you want to delete the current record?','Confirm',MB_IconQuestion+MB_YesNo)=IDNO then
Abort;
end;
Then, as long as you are willing to let the user confirm before deleting the data record, you can completely share this code in the TDataSet.BeforeDelete event (BeforeDelete of Table2, BeforeDelete of Table3...). Good luck doesn't happen every day, sometimes it's not so direct and simple...
The word "As" is not only extremely useful in English, but also in Delphi! This comes into play when we want to share code for several "similar" components: we only need to write code for one of them.
For example: if we want the background color of a group of text edit boxes to change to brown when they receive input focus, and to restore the system color when they lose focus, we can share the code like this (let the events of each text edit box share the following code):
procedure TForm1.Edit1Enter(Sender: TObject);
begin
(Sender as TEdit).color:=ClTeal; //Get input focus
end;
procedure TForm1.Edit1Exit(Sender: TObject);
begin
(Sender as TEdit).color:=ClWindow; //Lost input focus
end;
The above example seems a bit too specific. What if I only pay special attention to some of the components? Still taking the above example, for example, there are two text input boxes that need to enter numbers. In order to attract the user's attention, the color changes to blue when the focus is obtained, but I still don't want to write code for each component separately.
To solve this problem, you can use the Tag attribute of the component. First, assign non-zero and unequal Tags to the components to be specially processed (it must be noted that Delphi will not check whether the Tag values are equal, and you must control it yourself during design); then write the code as follows, You can still share:
procedure TForm1.Edit1Enter(Sender: TObject);
begin
case (Sender as TEdit).Tag of
10,20:(Sender as TEdit).Color:=clBlue //Text box for entering numbers
else
(Sender as TEdit).Color:=clTeal; //Other text boxes
end;
end;
(The following remarks are assumed to be under Delphi's default conditions)
1. Use the Self keyword:
Maybe someone thinks I'm mistaken! In fact, what I want to introduce to you here is another "trick" of the "recidivist" of the author (due to his poor memory). Sometimes, we may not be able to remember the exact name of the component. At this time, if you return to the form to confirm it again (most of my friends should be like this), it would be a bit too sorry for the time. It is particularly labor-saving if you use Self, because Self in Delphi explicitly refers to the form where the component is located. Therefore, we can enter Self followed by a comma to activate Delphi's Code Completion, the system will immediately show you a drop-down list containing all components of the form and the properties of the form itself, from which you can select the required components. In this way, the workload of switching back and forth between forms and tables is avoided.
2. Use code completion (Code Completion):
We no longer have to remember too many parameter details because of Delphi's code filling function. Before I explain it in detail, I want to be a little wordy. The default shortcut keys of Delphi's Code Completion will conflict with some of the shortcut keys we have used (for example, Ctrl+Space conflicts with the shortcut keys for opening and closing the Chinese character input method). So please resolve this issue first.
Of course, Code Completion will usually be activated automatically, but don't think that everything will be fine. I think Delphi should learn from VB for this technology. How can I say this? For example: when you enter StrToint(edit1., Delphi will not automatically display the properties of edit1. At this time, you will use Ctrl+Space to activate Code Completion. Especially when the nesting is deep, Delphi seems confused. .
3. Use Code Parameters:
The activation shortcut key is Ctrl+Shift+Space. This function is also automatically implemented by Delphi. As mentioned above, when we switch back and forth or move the cursor, this function will disappear. When we need to confirm the specific parameters of the function, we can open it with this shortcut key. .
4. Use Code Templates:
This technology once again reflects the superb technology of the engineers of Inprise's Delphi development team and their noble moral character of always thinking about users! (I just don’t want to hear about another person being poached by Microsoft)
The activation shortcut key is Ctrl+J. The implementation mechanism of this function is similar to that of code filling, except that it is not automatic. Of course, the most exciting aspect is that users can define their own templates. For example, a template defined by the author: shortcut -> "msg"; code -> "MessageboxMessageBox(
handle,'|',',MB_Icon...+MB_...);", as soon as I enter msg and then press Ctrl+J, the whole code "stands out" and moves the cursor to the place where I should add the input. It’s so cool!
Note: If you have defined a lot of cute templates for yourself and are reluctant to throw them away, you might as well back up the Delphi32.dci file in the Delphi4Bin directory and overwrite it with this file after reinstalling Delphi.
This topic shouldn’t be written here, but I can’t help but...
1. If you can use the object observer to set it directly without affecting the normal operation of the program, there is no need to write code:
Since this topic is too broad, here is just one example: For example, you want to implement automatic prompts on the status bar in Delphi4 without being too "harsh".
"Hint" condition, we no longer need to define a process (ShowMyHint(...)) as before, and then initialize it where the program
Assign her value to Tapplication's OnHin event. Because just set the AutoHint property of the status bar to True! Of course, it seems she can only
Displayed on the first Panel (this is when there are no too "harsh" conditions). Of course, blame me for being wordy, some friends don’t know Delphi’s Hint
Attributes are composed of two parts. This is not the place to talk. If you are interested, please go to the author's personal homepage www.zg169.net/~delphiok (just opened
Zhang, there aren’t many things yet).
2. Save code:
If you can write less code, there is no need to write more code. If you can refine it, there is no need to be redundant:
For example, the following two pieces of code implement exactly the same function. Which piece do you prefer to use?
Code snippet one:
if ToolButton1.Down=true then
begin
Dbgrid1.show;
DelBtn.Enabled:=true;
end
else
begin
Dbgrid1.Hide;
DelBtn.Enabled:=false;
end;
Code snippet two:
Dbgrid1.Visible:=ToolButton1.Down;
DelBtn.Enabled:=ToolButton1.Down;
Therefore, after we finish writing the program, we should not just pursue program adjustment, but also fry the program code.
3. Save variables:
Some programmers define variables where they are not needed, and define redundant variables where variables are needed. I personally think this is not good.
Habits, especially the habit of defining global variables at every turn, are even more questionable. Some readers may think that I am a miser. Today’s processor speeds are
What era is it... But I will simply show you a piece of code and you will understand:
procedure TForm1.Button1Click(Sender: TObject);
Var s1,s2:String;
begin
s1:=Inputbox('data','Please enter the user name',');
if s1<>' then
begin
Btn_Name.Enabled:=True;
s2:=Inputbox('data','Please enter the user's address',');
if s2<>' then
Btn_Adress.Enabled:=True;
end;
end;