Quick design of Delphi (programming section)

xiaoxiao2021-03-06  63

* All code involved in this article is in WinNT 4, and Delphi4 C / S is correct. Since Delphi uses a real fully object-to-object method, the world's fastest compiler and leading database technology, especially her completely compatible with Windows API, excellent code automatically generates ... People have seen, in short In the past few years, Delphi has standfined in many development tools! It is Delphi's excellent quality, so that the programmer can develop a special professional use software in a short period of time, but the work is quite small! The so-called let's us make better! How should we make full use of Delphi's advantages to make programming programs more quickly? First, make full use of code full sharing (loungel): We do this is not only for "lazy", which is also a way to optimize the program code. Of course, the first thing to consider is to be shared (this is very simple, see if it is included in the drop collection collection of the event), followed by whether there is "trouble" after sharing, because it is the first step, It is necessary to pursue optimization if it is correct. For example, we give a confirmation message before the user deletes data: procedure tform1.table1beforeDelete (DataSet: TDataSet); Begin // Confirm Delete Record if MessageBox (Handle, 'Do you want to delete the current record?', 'Confirm', MB_ICONQUESTION MB_YESNO) = IDNO THENABORT; END; then, you only want users to confirm before deleting data records before confirming, you can completely share this code in the TDataSet.BeforeDelete event (BeforeDelete ...) in Table3. Good luck will not be every day, there is no such direct, simple ... II. Take advantage of Delphi's forced conversion: "AS", the word is not only used in English, but also in Delphi! When we want to share the code for several "same type" components, we can play her huge role: we only need to write code for one of the components. For example, if you want a group of text edit boxes to get the input focus, the background color changes to a brown color, and the focus is the recovery system color, we can share the code so you can share the code (let each text edit box): 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 Enter the focus end; three, inconspicuous TAG properties: The above example seems to be a bit too special, what should I do if I do only some of the components? Take the above as an example, for example, there are two text input boxes to enter numbers, for reference users, the color changes to blue, but I still don't want to write code separately for each component.

To resolve this problem, you can use the tag properties of the component, first assign non-zero, unequal TAGs to be specifically processed, unqualified TAG (must not explain whether the TAG value is equal, you can master it yourself); then write code as follows, Sharing can still be implemented: Procedure TFORM1.EDIT1ENTER (Sender: TOBJECT); Begincase (Sender as tedit) .tag of10, 20: (sender as tedit) .COLOR: = CLBLUE // Enter a text box Else (Sender As Tedit) ) .COLOR: = CLTEAL; // The rest; four, make full use of Delphi's CodeInsight technology: (The following speech assumes that under the default condition of Delphi) wrong! In fact, here you have to introduce you to the author (because of the less than a child) "trick". In some cases, we may notify the exact name of the component (name), then if you return to the form table to confirm it, (most friends should be like this), there is a bit too sorry, this If you use the self, you can use the self, because Self is explicitly referenced in Delphi, and therefore, we can enter Self and add a comma to this to activate Delphi's code completion, at which time the system will immediately Show you a drop-down list that contains all components and form itself attributes, you can select the desired component from it. This way, avoid this workload of the back-to-back form form. 2, use code completion: We now have no need to let yourself remember too much parameter details, because there is a Delphi code makeup function. Before the specific explanation, there is a point, and the default shortcuts of Delphi will conflict with some of the shortcuts we have used (such as Ctrl Space, turn off the shortcut conflict of the Chinese character input method), So please solve this problem first. Of course, Code Completion is usually automatically activated, but don't think that there is all the best, I think this technology of Delphi should learn to VB. How can this speech? For example: When you enter strat1. After Delphi does not automatically display the properties of Edit1, you will use Ctrl Space to activate the Code Completion, especially when nest is very deep, Delphi seems to be confused. .3, use code parameters: The activation shortcut is Ctrl Shift Space. This feature is also Delphi Automatic implementation, as described above, when we come back, the cursor is removed, this function is also It will disappear, when we need to confirm the specific parameters of this function, you can open .4, use the code template: this technology once again reflects the engineers of the Inprise Delphi Development Group. And the noble product virtue of the users! (Just I don't want to hear anyone who is smashed by Microsoft) Activation shortcut is Ctrl J. This feature implementation mechanism is similar to the code filling, but she is not automatic, Of course, the most exciting side is that the user can define its own template.

For example, a template defined by the author: Fast -> "MSG"; Code -> "" MessageBoxMessageBox (Handle, '|', '', MB_ICON ... MB _...); ", just one input MSG then press CTRL J The entire code "stand out", and help me move the cursor to the place I have added, it's hard! Note: If you can't define a lot of cute templates for yourself, you may wish to back up the Delphi32.dci file under the Delphi4 / bin directory, and then reinstall the Delphi to overwrite this file. 5. When you are lazy, you should be lazy: this topic should not be written here, but you can't help but let it go ... 1, you can use the object observation directly to set up and don't affect the process, there is no need to write code: due to this The topic involves a wide range, only an example of this example: For example, the automatic prompt to Delphi4 on the status bar is not too "harsh" condition, we don't have to define a process before before, first defined a process ( ShowmyHint (...), then assign her to Tapplication's onhin event where the program is initialized. Because just set the autohint attribute of the status bar to True! Of course, she seems to only display on the first panel (this is when there is no "harsh" condition). Of course, blame me, some friends don't know that Delphi's Hint property consists of two parts. It is not a sense of speaking. I am interested, please go to the author's personal homepage www.zg169.net/~delphiok (just opened There are still few things in things). 2, save code: If you can write a code, there is no need to write more write code, you can refine if you don't reduce the redundancy: For example, the following two code implementations are identical, which parade you like? A code segment: if ToolButton1.Down = true thenbeginDbgrid1.show; DelBtn.Enabled: = true; endelsebeginDbgrid1.Hide; DelBtn.Enabled: = false; end; two code segment: Dbgrid1.Visible: = ToolButton1.Down; DelBtn.Enabled : = ToolButton1.down; therefore, after we finish the program, don't just pursue the program and blow, you may also fry the procedure. 3, save variables: Some programmers define variables in places that need variables, define redundant variables in places where variables, I personally think this is not good habits, especially if you don't move, define global variables. Discuss.

转载请注明原文地址:https://www.9cbs.com/read-119072.html

New Post(0)