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.
#pragma once
//addition to use Serialization
#using
<System.Runtime.Serialization.Formatters.Soap.dll>
//#using
<System.Runtime.Serialization.ISerializable.GetObjectData>
namespace treeViewSerialization {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
//addition to serialize Nodes//
using namespace System::IO;
using namespace System::Runtime::Serialization;
using namespace
System::Runtime::Serialization::Formatters::Soap;
//using namespace
System::Runtime::Serialization::Formatters::Soap;
/// <summary>
/// Summary for Form1
///
/// WARNING: If you change the name of this class,
you will need to change the
/// 'Resource File Name' property for the
managed resource compiler tool
/// associated with all .resx files this
class depends on. Otherwise,
/// the designers will not be able to
interact properly with localized
/// resources associated with this form.
/// </summary>
//..ref class Serializable treeView
[Serializable]
public ref class SerializableTreeView : TreeView,
ISerializable
{
public: SerializableTreeView() : TreeView()
{ }
public: SerializableTreeView(SerializationInfo^ info,
StreamingContext^ context) : TreeView()
{
SerializationInfoEnumerator^ infoEnumerator =
info->GetEnumerator();
while (infoEnumerator->MoveNext())
{
System::Windows::Forms::TreeNode^ node =
(System::Windows::Forms::TreeNode^)info->GetValue
(infoEnumerator->Name,
infoEnumerator->ObjectType);
if (node != nullptr)
{
Nodes->Add(node);
}
}
}
public: void virtual GetObjectData(SerializationInfo^
info, StreamingContext context)
{
for each(System::Windows::Forms::TreeNode^ node
in this->Nodes)
{
// info->AddValue(node->FullPath, node);
System::Guid guid = System::Guid::NewGuid();
info->AddValue(guid.ToString(),node);
}
}
/// <summary>
/// Serialize all the nodes of this tree to the stream
provided, using the formatter provided.
/// </summary>
/// <param name="stream">The stream to serialize
to.</param>
/// <param name="formatter">The formatter used to
serialize.</param>
public: void Serialize(Stream^ stream, IFormatter^
formatter)
{
formatter->Serialize(stream, this);
}
/// <summary>
/// Recreate this tree from a serialized version.
/// </summary>
/// <param name="stream">the stream that contains the
serialized tree.</param>
/// <param name="formatter">the formatter used to
desrialize the stream.</param>
public: void Deserialize(Stream^ stream, IFormatter^
formatter)
{
// Clear our tree:
this->Nodes->Clear();
SerializableTreeView^ temp =
(SerializableTreeView^
)formatter->Deserialize(stream);
if (temp != nullptr)
{
//copy the nodes from the temp to our tree:
for each(System::Windows::Forms::TreeNode^
node in temp->Nodes)
{
this->Nodes->Add((System::Windows::Forms::TreeNode^)node->Clone());
}
}
}
private: System::Void InitializeComponent() {
this->listView1 = (gcnew
System::Windows::Forms::ListView());
this->SuspendLayout();
//
// listView1
//
this->listView1->Location =
System::Drawing::Point(0, 0);
this->listView1->Name = L"listView1";
this->listView1->Size = System::Drawing::Size(121,
97);
this->listView1->TabIndex = 0;
this->listView1->UseCompatibleStateImageBehavior =
false;
this->ResumeLayout(false);
}
};
//}
//..ref class Serializable tree View
public ref class Form1 : public
System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::TreeView^
treeView1;
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::Button^ button2;
private: System::Windows::Forms::TextBox^ textBox1;
private: System::Windows::Forms::Label^ label1;
protected:
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
//
private: void ShowNodes(void){ //control
tampilan listView baik pada saat form di-load dan
setelah data di-update.
SoapFormatter ^bcrSoap = gcnew SoapFormatter();
String ^strFilename = "TreeNode.nod";
if( File::Exists(strFilename) )
{
MessageBox::Show(strFilename);
FileStream ^bcrStream = gcnew
FileStream(strFilename, FileMode::Open,
FileAccess::Read, FileShare::Read);
//disini Deserialize
ArrayList ^lstEmpl =
dynamic_cast<ArrayList^>(bcrSoap->Deserialize(bcrStream));
bcrStream->Close();
//Employee^ empl;
//bersihkan isi listView untuk di-update
tampilannya.
//lvwEmployees->Items->Clear();
TreeNodeCollection^ tn = this->treeView1->Nodes;
tn->GetEnumerator();
System::Collections::IEnumerator^ myEnum =
lstEmpl->GetEnumerator();
while (myEnum->MoveNext()) {
TreeNode^ tnc =
safe_cast<TreeNode^>(myEnum->Current);
treeView1->Nodes->Add(tnc);
}
}
} //end of void ShowNodes()
//
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not
modify
/// the contents of this method with the code
editor.
/// </summary>
void InitializeComponent(void)
{
this->treeView1 = (gcnew
System::Windows::Forms::TreeView());
this->button1 = (gcnew
System::Windows::Forms::Button());
this->button2 = (gcnew
System::Windows::Forms::Button());
this->textBox1 = (gcnew
System::Windows::Forms::TextBox());
this->label1 = (gcnew
System::Windows::Forms::Label());
this->SuspendLayout();
this->treeView1->Location =
System::Drawing::Point(2, 3);
this->treeView1->Name = L"treeView1";
this->treeView1->Size = System::Drawing::Size(247,
447);
this->treeView1->TabIndex = 0;
this->button1->Location =
System::Drawing::Point(332, 53);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75,
23);
this->button1->TabIndex = 1;
this->button1->Text = L"Add Node";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew
System::EventHandler(this, &Form1::button1_Click_1);
this->button2->Location =
System::Drawing::Point(413, 53);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(75,
23);
this->button2->TabIndex = 2;
this->button2->Text = L"Delete Node";
this->button2->UseVisualStyleBackColor = true;
this->textBox1->Location =
System::Drawing::Point(332, 9);
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing::Size(233,
20);
this->textBox1->TabIndex = 3;
this->label1->AutoSize = true;
this->label1->Location =
System::Drawing::Point(255, 12);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(71, 13);
this->label1->TabIndex = 4;
this->label1->Text = L"Node To Add";
this->AutoScaleDimensions =
System::Drawing::SizeF(6, 13);
this->AutoScaleMode =
System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(724, 462);
this->Controls->Add(this->label1);
this->Controls->Add(this->textBox1);
this->Controls->Add(this->button2);
this->Controls->Add(this->button1);
this->Controls->Add(this->treeView1);
this->Name = L"Form1";
this->Text = L"Form1";
this->FormClosed += gcnew
System::Windows::Forms::FormClosedEventHandler(this,
&Form1::Form1_FormClosed);
this->Load += gcnew System::EventHandler(this,
&Form1::Form1_Load);
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void button1_Click_1(System::Object^
sender, System::EventArgs^ e) {
TreeNode ^ newNode = gcnew
TreeNode(textBox1->Text);
treeView1->Nodes->Add(newNode);
//TreeNode ^ tn = treeView1->SelectedNode;
//treeView1->BeginUpdate();
//tn->Nodes->Add(textBox1->Text);
//treeView1->EndUpdate();
treeView1->Refresh();
treeView1->ExpandAll();
//if (tn->Level == 0) {
// treeView1->BeginUpdate();
// tn->Nodes->Add(textBox1->Text);
// treeView1->EndUpdate();
// treeView1->Refresh();
// treeView1->ExpandAll();
//}
//else {//level 1 / level 2 : parent / child
// MessageBox::Show("Penambahan cabang hanya
bisa pada root node.","Address Book",
MessageBoxButtons::OK, MessageBoxIcon::Information);
// return;
// } //level 1 / level 2
}
private: System::Void Form1_Load(System::Object^
sender, System::EventArgs^ e) {
//Begin of Deserialize
String ^path = "TreeNode.nod";
if (File::Exists(path)) {
FileStream^ stream = gcnew FileStream(path,
FileMode::Open, FileAccess::Read);
SoapFormatter^ formatter = gcnew
Formatters::Soap::SoapFormatter();
//BinaryFormatter^ formatter = gcnew
BinaryFormatter();
SerializableTreeView^ srtv = gcnew
SerializableTreeView;
srtv->Deserialize(stream, formatter);
stream->Close();
ShowNodes();
}
//End of Deserialize
}
private: System::Void Form1_FormClosed(System::Object^
sender, System::Windows::Forms::FormClosedEventArgs^
e) {
//Begin of Serialize
String ^path = "TreeNodes.nod";
if (File::Exists(path)){
File::Delete(path);
}
//coba buat class baru untuk menampung
TreeNode / TreeNodeCollection
FileStream^ stream = gcnew FileStream(path,
FileMode::CreateNew, FileAccess::Write);
//BinaryFormatter^ formatter = gcnew
BinaryFormatter();
SoapFormatter^ formatter = gcnew
Formatters::Soap::SoapFormatter();
SerializableTreeView^ srtv = gcnew
SerializableTreeView;
srtv->Serialize(stream, formatter);
stream->Close();
ShowNodes();
//End of Serialize
}
};
}
> Thank you very much.