(This article is only available for 9CBS, and also authorized Ccrun's demon reprint)
One of the features of object-oriented programming tools is to increase the code reuse (Reuse), and Balan's BCB can of course achieve this. We all know that in BCB, most of the program code corresponds directly or indirectly, this program is called an event handle, which is actually a process. From the application's engineering to windows, components, and programs, BCB emphasizes the reuse of each level during its development process, which can make full use of the written code to reduce workload, which will make your program beautiful. Both the sharing between the code segment is related to the control of the event, and the corresponding processing needs to be made according to the type of control. At this time, the sender parameter is used.
The beginning of each function is like:
Void _fastcall tform1 :: button1click (TOBJECT * Sender)
The Sender is a TOBJECT type parameter, which tells BCB which control receives this event and calls the corresponding process. We can write a single event handle handle to handle multiple components via Sender parameters and IF statements or CASE statements. In Delphi, you can use IS to test the Sender type, or use AS to convert, BCB We only use Dynamic_Cast to perform the above two work, below the Dynamic_cast's usage.
Dynamic_cast can enforce some object to another class. The so-called forces still have its limitations, that is, if the class can't come, the system will not perform conversion operations. Returns a value of 0 when the type conversion cannot be successful. If the parameter T is a reference type, the conversion of the class fails, and the system will throw an exception handling information: BAD_CAST. But you can rest assured that this will not cause the system to crash, so you can use it with confidence. Program:
Dynamic_cast
T parameter must be a pointer, void *, or a class that has been defined, and the PTR parameter must be a pointer or a reference. If the Type of T is void *, then PTR is a member that can access any member in the lower class, and certain classes will not be a basic class.
1. Judgment
We use Dynamic_Case to test Sender to find the type of handle or component that calls this event. For example, we point the handle of the edit box and the label of the tag to the XXX function of the window (in fact, you only name the Click event of a control first, and write a shared code, other controls Click Events point to XXX, which will have different reactions to the edit box and label in this example, the code is as follows:
Void __fastcall tform1 :: xxx (TOBJECT * Sender)
{
IF (Dynamic_cast
ShowMessage ("this is a editbox");
IF (Dynamic_cast
ShowMessage ("this is a label");
}
Of course, if multiple similar components, just want to share an event, it is much simpler than this. For example, if you have a lot of edit boxes (Edit), you want to empty this time when you enter a certain item, you just need to write an onNTER event:
Void __fastcall tform1 :: edit1enter (TOBJECT * Sender) {
Tedit * EditTemp = (TEDIT *) (sender); // Unify different edit boxes
EditTemp-> Text = ""
}
All other EDIT components point to edit1enter, so it will be, try it, is it a little in the edit box, J In fact, this is just a different edit box (Sender clear is the edit box). , Use a common event to handle it. You must pay attention to this when you share the same event in the same component.
2. Mandatory type conversion
Mandatory for several subclasses of the same parent class into the parent class. If there is a TEDIT class control and a TMEMO control in the window, they actually inherited to the TCUStomedit class. If you want to provide the same processing for one of the two, you can point to the custom function of the event handle. YYY, we are still in the OnNTER event (of course you can complete it in other events):
Void __fastcall tform1 :: yyy (TOBJECT * SENDER)
{
Dynamic_cast
}
Or the following format:
Void __fastcall tform1 :: yyy (TOBJECT * SENDER)
{
Dynamic_cast
}
Note the difference between the two, this is actually "." "" -> "is different, you will be clear, you will be clear.
The above two programs are first to force the TEDIT class and TMEMO classes to TCUSTOMEDIT classes, and the properties of their parent class are assigned.
Using the Sender parameter can process multiple class components through a single function segment, truly embodies the object-oriented reuse of BCB.
(Compiled through the above-mentioned XP system, BCB6 is compiled)
The original article has some mistakes, saying to everyone, I am more grateful to some enthusiastic netizens. J
But at the same time, I strictly declare that this article does not agree with any other person or website in addition to 9CBS and CCRUN. However, "BCB master advancement (10) uses Sender parameters to retrofit NXYC_TWZ (original)", which is wrong with me, and each word is not bad, I hope you can open to me and care about my netizens apologize. Otherwise I will keep your right to complain to 9CBS.