Creating the ObjectSerialization Class and Windows Form

   

Creating the ObjectSerialization Class and Windows Form

As was just mentioned, you will be creating a managed C++ application. Select New, Project from Visual Studio .NET's File menu. Select Visual C++ projects from the list of project types and select the Managed C++ application project template. Give your project the name ObjectSerialization and click OK to create the project.

The project you are going to create consists of a main Windows Form. Contained on that form is a text box that will be used to dump the values of various data members so that you don't need to view them in the debugger each time you run your application. Serialization and deserialization will occur when you click the corresponding form's button. Also, in order to demonstrate serialization customization, two text boxes will be used to set the values of the internal member variables. Listing 24.1 shows the necessary code for the main user interface of the form.

Listing 24.1 Creating the ObjectSerialization Form
  1: #include "stdafx.h"   2:   3: #using <mscorlib.dll>   4: #using <System.dll>   5: #using <System.Drawing.dll>   6: #using <System.Windows.Forms.dll>   7:   8: #include <tchar.h>   9:  10: using namespace System;  11: using namespace System::Drawing;  12: using namespace System::Windows::Forms;  13:  14: __gc class ObjectSerializationForm : public System::Windows::Forms::Form  15: {  16: private:  17:    // UI Controls  18:     Label* label1;  19:     Label* label2;  20:     Label* label3;  21:     TextBox* m_tbObjectDump;  22:     TextBox* m_tbSerializedString;  23:     TextBox* m_tbNonSerializedString;  24:     Button* m_btnSetSerialized;  25:     Button* m_btnSetNonSerialized;  26:     Button* m_btnSerialize;  27:     Button* m_btnLoad;  28:     Button* m_btnDump;  30:  31: public:  32:     ObjectSerializationForm()  33:     {  34:        InitializeComponent();  35:     }  36:     virtual ~ObjectSerializationForm(){};  37:  38: private:  39:     void InitializeComponent()  40:     {  41:         m_tbObjectDump = new TextBox();  42:         m_tbSerializedString = new TextBox();  43:         m_tbNonSerializedString = new TextBox();  44:  45:         label1 = new Label();  46:         label2 = new Label();  47:         label3 = new Label();  48:  49:         m_btnSetSerialized = new Button();  50:         m_btnSetNonSerialized = new Button();  51:         m_btnSerialize = new Button();  52:         m_btnLoad = new Button();  53:         m_btnDump = new Button();  54:         SuspendLayout();  55:  56:         // m_tbObjectDump  57:         m_tbObjectDump->Anchor = AnchorStyles(  58:             AnchorStyles::Top |  59:             AnchorStyles::Bottom |  60:             AnchorStyles::Left |  61:             AnchorStyles::Right);  62:         m_tbObjectDump->Location = System::Drawing::Point(8, 120);  63:         m_tbObjectDump->Multiline = true;  64:         m_tbObjectDump->Name = "m_tbObjectDump";  65:         m_tbObjectDump->ReadOnly = true;  66:         m_tbObjectDump->Size = System::Drawing::Size(426, 160);  67:         m_tbObjectDump->TabIndex = 0;  68:         m_tbObjectDump->Text = "";  69:  70:         // m_tbSerializedString  71:         m_tbSerializedString->Anchor = AnchorStyles(  72:             AnchorStyles::Top |  73:             AnchorStyles::Left |  74:             AnchorStyles::Right);  75:         m_tbSerializedString->Location = System::Drawing::Point(8, 24);  76:         m_tbSerializedString->Name = "m_tbSerializedString";  77:         m_tbSerializedString->Size = System::Drawing::Size(234, 20);  78:         m_tbSerializedString->TabIndex = 1;  79:         m_tbSerializedString->Text = "";  80:  81:         // label1  82:         label1->Anchor = AnchorStyles(AnchorStyles::Top |  83:             AnchorStyles::Left |  84:             AnchorStyles::Right);  85:         label1->Location = System::Drawing::Point(8, 8);  86:         label1->Name = "label1";  87:         label1->Size = System::Drawing::Size(118, 16);  88:         label1->TabIndex = 2;  89:         label1->Text = "Serialized String";  90:  91:         // label2  92:         label2->Anchor = AnchorStyles(AnchorStyles::Top |  93:             AnchorStyles::Left |  94:             AnchorStyles::Right);  95:         label2->Location = System::Drawing::Point(8, 56);  96:         label2->Name = "label2";  97:         label2->Size = System::Drawing::Size(118, 16);  98:         label2->TabIndex = 3;  99:         label2->Text = "Non-Serialized String"; 100: 101:         // m_tbNonSerializedString 102:         m_tbNonSerializedString->Anchor = AnchorStyles( 103:             AnchorStyles::Top | 104:             AnchorStyles::Left | 105:             AnchorStyles::Right); 106:         m_tbNonSerializedString->Location = System::Drawing::Point(8, 72); 107:         m_tbNonSerializedString->Name = "m_tbNonSerializedString"; 108:         m_tbNonSerializedString->Size = System::Drawing::Size(234, 20); 109:         m_tbNonSerializedString->TabIndex = 4; 110:         m_tbNonSerializedString->Text = ""; 111: 112:         // m_btnSetSerialized 113:         m_btnSetSerialized->Anchor = AnchorStyles(AnchorStyles::Top | 114:             AnchorStyles::Right); 115:         m_btnSetSerialized->Location = System::Drawing::Point(250, 24); 116:         m_btnSetSerialized->Name = "m_btnSetSerialized"; 117:         m_btnSetSerialized->Size = System::Drawing::Size(40, 23); 118:         m_btnSetSerialized->TabIndex = 6; 119:         m_btnSetSerialized->Text = "Set"; 120:         m_btnSetSerialized->Click += new System::EventHandler( 121:             this, SetSerialized_Click); 122: 123:         // m_btnSetNonSerialized 123:         m_btnSetNonSerialized->Anchor = AnchorStyles(AnchorStyles::Top | 124:             AnchorStyles::Right ); 125:         m_btnSetNonSerialized->Location = System::Drawing::Point(250, 72); 126:         m_btnSetNonSerialized->Name = "m_btnSetNonSerialized"; 127:         m_btnSetNonSerialized->Size = System::Drawing::Size(40, 23); 128:         m_btnSetNonSerialized->TabIndex = 8; 129:         m_btnSetNonSerialized->Text = "Set"; 130:         m_btnSetNonSerialized->Click += new System::EventHandler( 131:             this, SetNonSerialized_Click); 132: 133:         // label3 134:         label3->Location = System::Drawing::Point(8, 104); 135:         label3->Name = "label3"; 136:         label3->Size = System::Drawing::Size(100, 16); 137:         label3->TabIndex = 9; 138:         label3->Text = "Object Dump"; 139: 140:         // m_btnSerialize 141:         m_btnSerialize->Anchor = AnchorStyles(AnchorStyles::Top | 142:             AnchorStyles::Right); 143:         m_btnSerialize->Location = System::Drawing::Point(354, 16); 144:         m_btnSerialize->Name = "m_btnSerialize"; 145:         m_btnSerialize->TabIndex = 10; 146:         m_btnSerialize->Text = "Serialize"; 147:         m_btnSerialize->Click += new System::EventHandler 148:            (this, Serialize_Click); 149: 150:         // m_btnLoad 151:         m_btnLoad->Anchor = AnchorStyles(AnchorStyles::Top | 152:             AnchorStyles::Right); 153:         m_btnLoad->Location = System::Drawing::Point(354, 48); 154:         m_btnLoad->Name = "m_btnLoad"; 155:         m_btnLoad->TabIndex = 11; 156:         m_btnLoad->Text = "Load"; 157:         m_btnLoad->Click += new System::EventHandler 158:             (this, Load_Click); 159: 160:         // m_btnDump 161:         m_btnDump->Anchor = AnchorStyles(AnchorStyles::Top | 162:             AnchorStyles::Right); 163:         m_btnDump->Location = System::Drawing::Point(354, 80); 164:         m_btnDump->Name = "m_btnDump"; 165:         m_btnDump->TabIndex = 12; 166:         m_btnDump->Text = "Dump"; 167:         m_btnDump->Click += new System::EventHandler 168:            (this, Dump_Click); 169: 170:         // Form1 171:         AutoScaleBaseSize = System::Drawing::Size(5, 13); 172:         ClientSize = System::Drawing::Size(442, 294); 173:         Control* pControls? = { 174:                 m_btnDump, 175:                 m_btnLoad, 176:                 m_btnSerialize, 177:                 label3, 178:                 m_btnSetNonSerialized, 179:                 m_btnSetSerialized, 180:                 m_tbNonSerializedString, 181:                 label2, 182:                 label1, 183:                 m_tbSerializedString, 184:                 m_tbObjectDump}; 185:         Controls->AddRange( pControls ); 186: 187:             MinimumSize = System::Drawing::Size(450, 328); 188:             Name = "Form1"; 189:             Text = "Object Serialization"; 190:             ResumeLayout(false); 191: 192:     } 193: 194:     void Serialize_Click(Object* sender, EventArgs* e) 195:     { 196:     } 197: 198:     void Load_Click(Object* sender, EventArgs* e) 199:     { 200:    } 201: 202:     void Dump_Click(Object* sender, EventArgs* e) 203:     { 204:     } 205: 206:     void SetSerialized_Click(Object* sender, EventArgs* e) 207:     { 208:     } 209: 210:     void SetNonSerialized_Click(Object* sender, EventArgs* e) 211:     { 212:     } 213: }; 214: 215: int _tmain(void) 216: { 217:     Application::Run(new ObjectSerializationForm ); 218: 219:     return 0; 220: } 

If you decided to write in all the code yourself rather than download the project from the Sams Web site, the probability that you entered at least one error is pretty high. Compile and run your application to ensure everything is working. You should see a form similar to the one in Figure 24.1 when your application is running.

Figure 24.1. The ObjectSerialization Windows Form.

graphics/24fig01.jpg


   
Top


Sams Teach Yourself Visual C++. NET in 24 Hours
Sams Teach Yourself Visual C++.NET in 24 Hours
ISBN: 0672323230
EAN: 2147483647
Year: 2002
Pages: 237

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net