C ++ Builder common code snippet

zhaozj2021-02-08  211

C Builder code snippet

This article contains some common code snipples to see if we think that there is a stone of his mountain to attack jade. CKer

Deleting all the forms of pure virtual functions in the alias Fun Function Page Pointer For the FOR loop variable for the pointer to the reference table and the constant declaration of the constant Check the table in the class name of the component, the text character stream checklist in the class name of the clipboard is open. Operation Change PageControl's label to Query Parameter Date Property Drawing Status

Delete all tables in the alias Void Tdata :: cleantemp () {tstringlist * mytables = new tstringlist (); ttable * table = new ttable (this); try {session-> gettablenames ("temp", ", true, false , myTables);} catch (...) {} // Ansistring str = myTables-> getText (); // showMessage (STR); for (int count = 0; count count; count ) {Table -> DatabaseName = "TEMP"; Table-> TableName = myTables-> strings [count]; table-> close (); table-> deletetable ();} delete myTables; delete table;} back pure virtual function // pure The virtual function only appears in the base class, and the member function that matches there must be // in the subclass. The instance of the subclass declared in the program provides an overloaded member function for each pure virtual function in the base class. class TBaseClass {public: virtual void Display () = 0;}; class TDerivedClass: public TBaseClass {public: void Display () {ShowMessage ( "From Derived");}}; class TSecondDerivedClass: public TDerivedClass {public: void Display ( ) {ShowMessage ( "From Second Derived");}}; void __fastcall TForm1 :: Button1Click (TObject * Sender) {TDerivedClass dc; dc.Display (); // "From Derived" TSecondDerivedClass sc; TBaseClass * bc = & sc; BC-> Display (); // "from second deact"} BACK virtual function // virtual function as a member function of the parent class of other classes. // If there is a function of exactly the same function as the parent class member function, the member function in the subclass is always valid.

class Base {public: virtual void Display () {ShowMessage ( "Base Class");}}; class DerivedOne: public Base {public: void Display () {ShowMessage ( "DerivedOne");}}; class DerivedTwo: public Base {public: void Display () {ShowMessage ( "DerivedTwo");}}; Base * pBases [10]; int count = 0; DerivedOne aDerOne; DerivedTwo aDerTwo; pBases [count ] = & aDerOne; pBases [count ] = & aDerTwo; For (int i = 0; i display (); Back start page UsedataModule; useform ("about.cpp", aboutbox; useform; "main.cpp", mainform; useform ("splash.cpp", splashform); file: // -------------------------- ------------------------------------------------- include "splash.h" WINAPI WinMain (HINSTANCE, HINSTANCE, LPSTR, int) {try {SplashForm = new TSplashForm (Application); SplashForm-> Show (); SplashForm-> Update (); Application-> Initialize (); Application -> Title = "EXAMPLE OF Loading Splash Form"; Application-> HelpFile = "SplashHelp.hlp"; Application-> CreateForm (__ classid (TMainForm), & MainForm); Application-> CreateForm (__ classid (TDataModule), & DataModule); Application-> CreateForm (__ classid (TAboutBox), & AboutBox); SplashForm-> Hide (); splashform-> close (); Application-> Run ();} catch (exception & exception) {Application-> showexception (& Exception);} return 0;} back pointer int Array [] = {2, 4, 6, 8, 10} int myinteger = array [3]; // value is 8 // ---- Use pointer to achieve the same function ----- int arch [] =

{2, 4, 6, 8, 10} int * myinteger = array; int myinteger = myptr [3]; / / value is 8 back to the pointer release reference INT x = 32; int * PTR = & x; // Release pointer Reference file: // to get the content of memory location int y = * ptr; // y = 32 back table for loops VOID TDATAMODULEMPLOYE :: ListNames (TSTRINGS * ITEMS) {TRY {for (TableAll-> first (); ! TableAll-> EOF; TableAll-> Next ()) IF (TableAll-> FieldByname ("deleted") -> asboolean == false) items-> addObject (TableAll-> FieldByname ("name") -> asstring, Tobject *) TableAll-> FieldByname ("refNum") -> asinteger;} catch (exception & e) {Application-> Showexception (& E);};} BACK variables and constants CHAR CH; int count = 1; char * name = "9cbs.net"; struct complex {float my, his;}; float real (complex * p) {return p-> my}; const double pi = 3.1415926535897932385; TempleTate ABC (T a) {Return a 0? -A: a;}; enum Website {One, Two, Three, Four}; INT * A; // * Pointer Char * P [20]; // [] array vid myfunction (int); // ) Function str {short length; char * p;}; char CH1 = 'a'; char * p = & ch1; // & reference, p keeping CH1's address CHAR CH2 = * P; // CH2 = 'a' Back checklist exists #include "io.h" if (access (table1-> tablename.c_str (), 0)) File: // Check the table if there is { // If there is no existence, create ... Table1-> Active = false; table1-> TableType = TTPARADOX; table1-> Fielddefs-> clear (); table1-> fielddefs-> add ("myfield", ftstring, 15, False); table1-> indexdefs-> clear (); table1-> createTable (); table1-> active = true;} else table1-> active = true; BACK component class name // Find lost component class name For (int i = 0; i

Tcomponent &> (* Components [i]). Name) == "") {showMessage (Components [i] -> classname ());}}}}}}}}}}}}}}}}}} <> #Include "clipbrd.hpp" file: // contains the Tclipboard & Clipboard () // Example program containing a MEMO control __fastcall tform1 :: tForm1 (tComponent * Owner): TForm (Owner) {file: // creation TStringList object auto_ptringlist (new tstringlist); file: // Get copy cliptext-> text = clipboard () -> astext; file: // then processed ... MEMO1-> lines-> addstrings (ClipText.get ());} Back character stream // example one #include "sstream" const char * name = "cker"; const char * email = "cker@sina.com"; // Generate "CKER" [ SMTP: cker@sina.com] ostringstream Ost; OST << "/" << name << "/" [SMTP: "<< Email <<"] "; edit1-> text = ost.str (). c_str (); file: // Example 2 Void TdataModuleEmployee :: FullReport (const char * title) {report.header; Report << SFormat ("Employee #% 2D:% S% S / N", TableAllRefnum- > Value, TableAllName-> text.c_str (), TableAllManagerflag-> value? ":" "" << "Address: << TableAlLaddres S-> text.c_str () << Endl << "<< TableAllCityProvzip-> text.c_str () << Endl <<" << NameCountry (TableAllCanada-> Value) << Endl; Report.footer () } The back check list has been opened void tdata :: checkedit () {for (INT i = 0; i State == dsedit) {string s = "table" components [i] -> name "

IS in edit mode "" / rwould you like to post it before enter new task? "; if (MessageBox (null, s.c_str ()," Table in Edit Mode ", MB_YESNO | MB_ICONITION == iDYES) ((Ttable) *) Components [i]) -> POST (); Else ((TTable *) Components [i]) -> Cancel ();}}}} The status operation of the Back table // Close the open table and restore them Be initial.

Void Tdata :: MyTables (TForm * sender) {INT i; TTABLE * TABLE; BOOL * ACTIVE = New Bool [MyClass-> ComponentCount]; // Store the initial state of each table in a dynamic array, then close all tables For (i = 0; i componentcount; i ) {try {if ((Table = Dynamic_casts [i]))! = null) {Active [i] = Table-> Active; TABLE-> ACTIVE; table -> Active = false;}} catch (...) {}; file: // Abnormally should only come from Dynamic Cast ...} for (i = 0; i componentcount; i ) {Try { if ((Table = dynamic_cast (MyClass-> Components [i])) = NULL!) {if continue (Table-> DatabaseName = OPTARDatabase-> DatabaseName!); DBIResult result = DBIERR_NONE 1; while (! result = DBIERR_NONE) File: // If you want, this allows users to retry! {Result = dbipacktable (OptarDatabase-> Handle, Null, Table-> Tablename.c_str (), null, true); if (Result! = DBIERR_NONE) {Ansistring Rslttext = "Unable to Pack" Table-> TableName ". "; CHAR RSLT [DBIMAXMSGLEN 1]; dbigeTerRorstring (Result, RSLT) RSLTTEXT =". TRY AGAIN? "; If (Application-> MessageBox (RsltText.c_STR ()," Error! ", MB_YESNO! = IDyes) Break;}}}} catch (...) {}; file: // Exception should only come from Dynamic Cast ...} // Set all tables back to the initial state.

For (i = 0; i componentcount; i ) {try {if ((Table = Dynamic_Cast (myclass-> Components [i]))! = null) Table-> Active = Active [i];} catch (...) {};} DELETE [] Active;} BACK Change PageControl's label void __fastcall tfmmainform :: cancel1click (TOBJECT * Sender) {INT i; switch (pageControl1-> ActivePage-> tag) {cas 1: For (i = 0; i enabled = false;} Data-> TBDetail-> Cancel (); Break; Case 2: for (i = 0; i enabled = false;} Data-> tbdetail-> Cancel (); Break Case 3: for (i = 0; i text = "";} Break;}} back to Query Pass Parameter // Directly pass the parameters from the table to Query TQuery * query = new tq Uery (this); query-> DatabaseName = "dbserver"; query-> sql-> clear (); query-> sql-> add ("delete from 'Events.db' where (ticketno =" data-> tbproblem -> FieldByname ("Ticketno") -> asstring ")"); query-> xissql (); query-> close (); delete query; back date property Tmaskedit * MeOpen; TLabl * lbcount1; tdatetime date2; void __fastcall Tfmmainform :: Countopen (TOBJECT * Sender) {switch (Dynamic_cast (* sender) .tag) {case 1: count1 = strt (lbcount1-> caption); count1 = 1; Date2 =

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

New Post(0)