Gets all information in listView into a binary file/String variables/textBox in VC++

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2007
Posts: 3
Reputation: chrisliando is an unknown quantity at this point 
Solved Threads: 0
chrisliando chrisliando is offline Offline
Newbie Poster

Gets all information in listView into a binary file/String variables/textBox in VC++

 
0
  #1
Nov 18th, 2007
Hi I am new in Visual C++ and I want to know how to get all the
> information in the listView to textBox/String
> variable/ to a binary file /Serialization maybe?
>
> So if on the listView is consists of:
>
> Company | Name | Address | ZIP
> ===========================================
> IBM | Chris | DEF | 62646
> -------------------------------------------
> Yahoo! | John | XYZ | 23452
>
> the purpose is how to get all the information above to
> String variables / textBox? I need to show the
> information on a textBox so that they would be able to
> be edited. So the flow is like below:
> When I select IBM, Chris, DEF and 62646 line and then
> click Edit_Process, the form with a company text box,
> nama textbox, address textbox and ZIP textbox will be
> shown and each textbox will be filled with :
>
> Company textbox : IBM
> Name textbox : Chris
> Address textbox : DEF
> ZIP textbox : 62646
>
> The users are able to make a necessary change they
> desired. If they want to change name then they focused
> on name textbox and change Chris with Jim for example.
> After that they click button Save to save the changes.
>
> How to do that? Or is there any facilities to edit
> listView items in place?
>
I also don't know how to save/serialize
> nodes in the treeView? It doesn't matter if it using
> manual savings / serialize, because the purpose is
> to save the last state of the treeView to a binary file / any file when the form is
> closed and to load the saved file when the form is loaded.
Here is my effort to serialize the node of a treeView but it did not work.
  1. #pragma once
  2. //addition to use Serialization
  3. #using
  4. <System.Runtime.Serialization.Formatters.Soap.dll>
  5. //#using
  6. <System.Runtime.Serialization.ISerializable.GetObjectData>
  7.  
  8.  
  9. namespace treeViewSerialization {
  10.  
  11. using namespace System;
  12. using namespace System::ComponentModel;
  13. using namespace System::Collections;
  14. using namespace System::Windows::Forms;
  15. using namespace System::Data;
  16. using namespace System::Drawing;
  17. //addition to serialize Nodes//
  18. using namespace System::IO;
  19. using namespace System::Runtime::Serialization;
  20. using namespace
  21. System::Runtime::Serialization::Formatters::Soap;
  22. //using namespace
  23. System::Runtime::Serialization::Formatters::Soap;
  24.  
  25.  
  26. /// <summary>
  27. /// Summary for Form1
  28. ///
  29. /// WARNING: If you change the name of this class,
  30. you will need to change the
  31. /// 'Resource File Name' property for the
  32. managed resource compiler tool
  33. /// associated with all .resx files this
  34. class depends on. Otherwise,
  35. /// the designers will not be able to
  36. interact properly with localized
  37. /// resources associated with this form.
  38. /// </summary>
  39.  
  40. //..ref class Serializable treeView
  41. [Serializable]
  42.  
  43. public ref class SerializableTreeView : TreeView,
  44. ISerializable
  45. {
  46.  
  47. public: SerializableTreeView() : TreeView()
  48.  
  49. { }
  50.  
  51. public: SerializableTreeView(SerializationInfo^ info,
  52. StreamingContext^ context) : TreeView()
  53. {
  54. SerializationInfoEnumerator^ infoEnumerator =
  55. info->GetEnumerator();
  56. while (infoEnumerator->MoveNext())
  57. {
  58. System::Windows::Forms::TreeNode^ node =
  59. (System::Windows::Forms::TreeNode^)info->GetValue
  60. (infoEnumerator->Name,
  61. infoEnumerator->ObjectType);
  62. if (node != nullptr)
  63. {
  64. Nodes->Add(node);
  65. }
  66. }
  67.  
  68. }
  69.  
  70. public: void virtual GetObjectData(SerializationInfo^
  71. info, StreamingContext context)
  72. {
  73. for each(System::Windows::Forms::TreeNode^ node
  74. in this->Nodes)
  75. {
  76. // info->AddValue(node->FullPath, node);
  77. System::Guid guid = System::Guid::NewGuid();
  78. info->AddValue(guid.ToString(),node);
  79. }
  80.  
  81. }
  82.  
  83. /// <summary>
  84.  
  85. /// Serialize all the nodes of this tree to the stream
  86. provided, using the formatter provided.
  87.  
  88. /// </summary>
  89.  
  90. /// <param name="stream">The stream to serialize
  91. to.</param>
  92.  
  93. /// <param name="formatter">The formatter used to
  94. serialize.</param>
  95.  
  96. public: void Serialize(Stream^ stream, IFormatter^
  97. formatter)
  98.  
  99. {
  100.  
  101. formatter->Serialize(stream, this);
  102.  
  103. }
  104.  
  105. /// <summary>
  106.  
  107. /// Recreate this tree from a serialized version.
  108.  
  109. /// </summary>
  110.  
  111. /// <param name="stream">the stream that contains the
  112. serialized tree.</param>
  113.  
  114. /// <param name="formatter">the formatter used to
  115. desrialize the stream.</param>
  116.  
  117. public: void Deserialize(Stream^ stream, IFormatter^
  118. formatter)
  119. {
  120. // Clear our tree:
  121. this->Nodes->Clear();
  122. SerializableTreeView^ temp =
  123. (SerializableTreeView^
  124. )formatter->Deserialize(stream);
  125. if (temp != nullptr)
  126. {
  127.  
  128. //copy the nodes from the temp to our tree:
  129.  
  130. for each(System::Windows::Forms::TreeNode^
  131. node in temp->Nodes)
  132. {
  133.  
  134. this->Nodes->Add((System::Windows::Forms::TreeNode^)node->Clone());
  135.  
  136. }
  137.  
  138. }
  139.  
  140. }
  141.  
  142. private: System::Void InitializeComponent() {
  143. this->listView1 = (gcnew
  144. System::Windows::Forms::ListView());
  145. this->SuspendLayout();
  146. //
  147. // listView1
  148. //
  149. this->listView1->Location =
  150. System::Drawing::Point(0, 0);
  151. this->listView1->Name = L"listView1";
  152. this->listView1->Size = System::Drawing::Size(121,
  153. 97);
  154. this->listView1->TabIndex = 0;
  155. this->listView1->UseCompatibleStateImageBehavior =
  156. false;
  157. this->ResumeLayout(false);
  158.  
  159. }
  160. };
  161.  
  162. //}
  163. //..ref class Serializable tree View
  164.  
  165. public ref class Form1 : public
  166. System::Windows::Forms::Form
  167. {
  168. public:
  169. Form1(void)
  170. {
  171. InitializeComponent();
  172. //
  173. //TODO: Add the constructor code here
  174. //
  175. }
  176.  
  177. protected:
  178. /// <summary>
  179. /// Clean up any resources being used.
  180. /// </summary>
  181. ~Form1()
  182. {
  183. if (components)
  184. {
  185. delete components;
  186. }
  187. }
  188. private: System::Windows::Forms::TreeView^
  189. treeView1;
  190. private: System::Windows::Forms::Button^ button1;
  191. private: System::Windows::Forms::Button^ button2;
  192. private: System::Windows::Forms::TextBox^ textBox1;
  193. private: System::Windows::Forms::Label^ label1;
  194. protected:
  195.  
  196.  
  197. private:
  198. /// <summary>
  199. /// Required designer variable.
  200. /// </summary>
  201. System::ComponentModel::Container ^components;
  202. //
  203. private: void ShowNodes(void){ //control
  204. tampilan listView baik pada saat form di-load dan
  205. setelah data di-update.
  206. SoapFormatter ^bcrSoap = gcnew SoapFormatter();
  207. String ^strFilename = "TreeNode.nod";
  208.  
  209. if( File::Exists(strFilename) )
  210. {
  211. MessageBox::Show(strFilename);
  212. FileStream ^bcrStream = gcnew
  213. FileStream(strFilename, FileMode::Open,
  214. FileAccess::Read, FileShare::Read);
  215. //disini Deserialize
  216. ArrayList ^lstEmpl =
  217. dynamic_cast<ArrayList^>(bcrSoap->Deserialize(bcrStream));
  218.  
  219. bcrStream->Close();
  220.  
  221. //Employee^ empl;
  222.  
  223. //bersihkan isi listView untuk di-update
  224. tampilannya.
  225. //lvwEmployees->Items->Clear();
  226. TreeNodeCollection^ tn = this->treeView1->Nodes;
  227. tn->GetEnumerator();
  228.  
  229. System::Collections::IEnumerator^ myEnum =
  230. lstEmpl->GetEnumerator();
  231. while (myEnum->MoveNext()) {
  232. TreeNode^ tnc =
  233. safe_cast<TreeNode^>(myEnum->Current);
  234. treeView1->Nodes->Add(tnc);
  235. }
  236. }
  237. } //end of void ShowNodes()
  238. //
  239.  
  240. #pragma region Windows Form Designer generated code
  241. /// <summary>
  242. /// Required method for Designer support - do not
  243. modify
  244. /// the contents of this method with the code
  245. editor.
  246. /// </summary>
  247. void InitializeComponent(void)
  248. {
  249. this->treeView1 = (gcnew
  250. System::Windows::Forms::TreeView());
  251. this->button1 = (gcnew
  252. System::Windows::Forms::Button());
  253. this->button2 = (gcnew
  254. System::Windows::Forms::Button());
  255. this->textBox1 = (gcnew
  256. System::Windows::Forms::TextBox());
  257. this->label1 = (gcnew
  258. System::Windows::Forms::Label());
  259. this->SuspendLayout();
  260. this->treeView1->Location =
  261. System::Drawing::Point(2, 3);
  262. this->treeView1->Name = L"treeView1";
  263. this->treeView1->Size = System::Drawing::Size(247,
  264. 447);
  265. this->treeView1->TabIndex = 0;
  266. this->button1->Location =
  267. System::Drawing::Point(332, 53);
  268. this->button1->Name = L"button1";
  269. this->button1->Size = System::Drawing::Size(75,
  270. 23);
  271. this->button1->TabIndex = 1;
  272. this->button1->Text = L"Add Node";
  273. this->button1->UseVisualStyleBackColor = true;
  274. this->button1->Click += gcnew
  275. System::EventHandler(this, &Form1::button1_Click_1);
  276. this->button2->Location =
  277. System::Drawing::Point(413, 53);
  278. this->button2->Name = L"button2";
  279. this->button2->Size = System::Drawing::Size(75,
  280. 23);
  281. this->button2->TabIndex = 2;
  282. this->button2->Text = L"Delete Node";
  283. this->button2->UseVisualStyleBackColor = true;
  284. this->textBox1->Location =
  285. System::Drawing::Point(332, 9);
  286. this->textBox1->Name = L"textBox1";
  287. this->textBox1->Size = System::Drawing::Size(233,
  288. 20);
  289. this->textBox1->TabIndex = 3;
  290. this->label1->AutoSize = true;
  291. this->label1->Location =
  292. System::Drawing::Point(255, 12);
  293. this->label1->Name = L"label1";
  294. this->label1->Size = System::Drawing::Size(71, 13);
  295. this->label1->TabIndex = 4;
  296. this->label1->Text = L"Node To Add";
  297. this->AutoScaleDimensions =
  298. System::Drawing::SizeF(6, 13);
  299. this->AutoScaleMode =
  300. System::Windows::Forms::AutoScaleMode::Font;
  301. this->ClientSize = System::Drawing::Size(724, 462);
  302. this->Controls->Add(this->label1);
  303. this->Controls->Add(this->textBox1);
  304. this->Controls->Add(this->button2);
  305. this->Controls->Add(this->button1);
  306. this->Controls->Add(this->treeView1);
  307. this->Name = L"Form1";
  308. this->Text = L"Form1";
  309. this->FormClosed += gcnew
  310. System::Windows::Forms::FormClosedEventHandler(this,
  311. &Form1::Form1_FormClosed);
  312. this->Load += gcnew System::EventHandler(this,
  313. &Form1::Form1_Load);
  314. this->ResumeLayout(false);
  315. this->PerformLayout();
  316.  
  317. }
  318. #pragma endregion
  319. private: System::Void button1_Click_1(System::Object^
  320. sender, System::EventArgs^ e) {
  321. TreeNode ^ newNode = gcnew
  322. TreeNode(textBox1->Text);
  323. treeView1->Nodes->Add(newNode);
  324. //TreeNode ^ tn = treeView1->SelectedNode;
  325. //treeView1->BeginUpdate();
  326. //tn->Nodes->Add(textBox1->Text);
  327. //treeView1->EndUpdate();
  328. treeView1->Refresh();
  329. treeView1->ExpandAll();
  330.  
  331. //if (tn->Level == 0) {
  332. // treeView1->BeginUpdate();
  333. // tn->Nodes->Add(textBox1->Text);
  334. // treeView1->EndUpdate();
  335. // treeView1->Refresh();
  336. // treeView1->ExpandAll();
  337. //}
  338. //else {//level 1 / level 2 : parent / child
  339.  
  340. // MessageBox::Show("Penambahan cabang hanya
  341. bisa pada root node.","Address Book",
  342. MessageBoxButtons::OK, MessageBoxIcon::Information);
  343. // return;
  344. // } //level 1 / level 2
  345.  
  346. }
  347. private: System::Void Form1_Load(System::Object^
  348. sender, System::EventArgs^ e) {
  349.  
  350. //Begin of Deserialize
  351. String ^path = "TreeNode.nod";
  352. if (File::Exists(path)) {
  353. FileStream^ stream = gcnew FileStream(path,
  354. FileMode::Open, FileAccess::Read);
  355. SoapFormatter^ formatter = gcnew
  356. Formatters::Soap::SoapFormatter();
  357. //BinaryFormatter^ formatter = gcnew
  358. BinaryFormatter();
  359. SerializableTreeView^ srtv = gcnew
  360. SerializableTreeView;
  361. srtv->Deserialize(stream, formatter);
  362. stream->Close();
  363. ShowNodes();
  364. }
  365.  
  366. //End of Deserialize
  367. }
  368.  
  369. private: System::Void Form1_FormClosed(System::Object^
  370. sender, System::Windows::Forms::FormClosedEventArgs^
  371. e) {
  372. //Begin of Serialize
  373. String ^path = "TreeNodes.nod";
  374.  
  375. if (File::Exists(path)){
  376. File::Delete(path);
  377. }
  378. //coba buat class baru untuk menampung
  379. TreeNode / TreeNodeCollection
  380.  
  381. FileStream^ stream = gcnew FileStream(path,
  382. FileMode::CreateNew, FileAccess::Write);
  383. //BinaryFormatter^ formatter = gcnew
  384. BinaryFormatter();
  385. SoapFormatter^ formatter = gcnew
  386. Formatters::Soap::SoapFormatter();
  387. SerializableTreeView^ srtv = gcnew
  388. SerializableTreeView;
  389. srtv->Serialize(stream, formatter);
  390. stream->Close();
  391. ShowNodes();
  392.  
  393. //End of Serialize
  394. }
  395. };
  396. }
  397.  
> Thank you very much.
Last edited by Ancient Dragon; Nov 18th, 2007 at 2:24 pm. Reason: add code tags and remove smilies
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC