From 9722305b10d68e08c07beb9e1fc04dcac99dcc84 Mon Sep 17 00:00:00 2001 From: Lavin S Date: Fri, 7 May 2021 18:09:05 +0200 Subject: [PATCH] Recursive sample added for TreeNode iteratation (#1058) * Non-recursive tree traversal C# sample added * Changed formatting to reflect markdown correctly * Formatting updated * Updated sentence * Update to confirm to ESlint changes * Resolve indentation mismatch * Correctly link the xref * Added C# code snippets for iteration over TreeView * Add reference to c# code snippet * Added a ref to the code snippet for recursive c# * Fix comment * VisualBasic code snippet for TreeViewTraversal * Updated vb reference to code snippet * Cpp gitignore updated * Added cpp snippet for TreeView iteration * C# commenting out print calls * Vb commenting out print calls * Reorder Folder Structure cpp * Document updated with code snippets * C# proj prpgrp updated * VB prpgrp added * Cpp prpgrp added * Fixed typo * Add description, update date * Updating to match the two sections in style * Adding teh snippets.5000.json file * Update dotnet-desktop-guide/framework/winforms/controls/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control.md * Update dotnet-desktop-guide/framework/winforms/controls/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control.md Co-authored-by: Andy (Steve) De George <67293991+adegeo@users.noreply.github.com> --- .gitignore | 24 ++ ...des-of-a-windows-forms-treeview-control.md | 120 ++------ .../cpp/MyForm.cpp | 13 + .../cpp/MyForm.h | 267 ++++++++++++++++++ .../cpp/MyForm.resx | 120 ++++++++ .../cpp/Student.h | 20 ++ .../cpp/Subject.h | 21 ++ .../cpp/TextBook.h | 16 ++ .../cpp/project.vcxproj | 151 ++++++++++ .../cpp/project.vcxproj.filters | 36 +++ .../cpp/snippets.5000.json | 3 + .../cs/Form1.Designer.cs | 59 ++++ .../cs/Form1.cs | 158 +++++++++++ .../cs/Form1.resx | 120 ++++++++ .../cs/Program.cs | 22 ++ .../cs/Student.cs | 19 ++ .../cs/Subject.cs | 19 ++ .../cs/TextBook.cs | 13 + .../cs/project.csproj | 68 +++++ .../vb/App.config | 6 + .../vb/Form1.Designer.vb | 73 +++++ .../vb/Form1.resx | 120 ++++++++ .../vb/Form1.vb | 111 ++++++++ .../vb/My Project/Application.Designer.vb | 38 +++ .../vb/My Project/Application.myapp | 11 + .../vb/My Project/AssemblyInfo.vb | 35 +++ .../vb/My Project/Resources.Designer.vb | 62 ++++ .../vb/My Project/Resources.resx | 117 ++++++++ .../vb/My Project/Settings.Designer.vb | 73 +++++ .../vb/My Project/Settings.settings | 7 + .../vb/Student.vb | 17 ++ .../vb/Subject.vb | 17 ++ .../vb/TextBook.vb | 9 + .../vb/project.vbproj | 130 +++++++++ 34 files changed, 1996 insertions(+), 99 deletions(-) create mode 100644 dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cpp/MyForm.cpp create mode 100644 dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cpp/MyForm.h create mode 100644 dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cpp/MyForm.resx create mode 100644 dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cpp/Student.h create mode 100644 dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cpp/Subject.h create mode 100644 dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cpp/TextBook.h create mode 100644 dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cpp/project.vcxproj create mode 100644 dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cpp/project.vcxproj.filters create mode 100644 dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cpp/snippets.5000.json create mode 100644 dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cs/Form1.Designer.cs create mode 100644 dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cs/Form1.cs create mode 100644 dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cs/Form1.resx create mode 100644 dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cs/Program.cs create mode 100644 dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cs/Student.cs create mode 100644 dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cs/Subject.cs create mode 100644 dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cs/TextBook.cs create mode 100644 dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cs/project.csproj create mode 100644 dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/App.config create mode 100644 dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/Form1.Designer.vb create mode 100644 dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/Form1.resx create mode 100644 dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/Form1.vb create mode 100644 dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/My Project/Application.Designer.vb create mode 100644 dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/My Project/Application.myapp create mode 100644 dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/My Project/AssemblyInfo.vb create mode 100644 dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/My Project/Resources.Designer.vb create mode 100644 dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/My Project/Resources.resx create mode 100644 dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/My Project/Settings.Designer.vb create mode 100644 dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/My Project/Settings.settings create mode 100644 dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/Student.vb create mode 100644 dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/Subject.vb create mode 100644 dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/TextBook.vb create mode 100644 dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/project.vbproj diff --git a/.gitignore b/.gitignore index 977a176..1a11e6a 100644 --- a/.gitignore +++ b/.gitignore @@ -44,3 +44,27 @@ ehthumbs_vista.db [Oo]bj/ *.sln *.user + +# C++ +# Compiled Object files +*.slo +*.lo +*.o + +# Compiled Dynamic libraries +*.so +*.dylib + +# Compiled Static libraries +*.lai +*.la +*.a + +# Visual Studio Files +Debug +Release +*.suo +*.csproj.user +*.vbproj.user +ipch +*sdf diff --git a/dotnet-desktop-guide/framework/winforms/controls/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control.md b/dotnet-desktop-guide/framework/winforms/controls/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control.md index d561fd1..7adfcda 100644 --- a/dotnet-desktop-guide/framework/winforms/controls/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control.md +++ b/dotnet-desktop-guide/framework/winforms/controls/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control.md @@ -1,6 +1,7 @@ --- title: Iterate Through All Nodes of TreeView Control -ms.date: "03/30/2017" +ms.date: "05/06/2021" +description: Learn how to interate through the nodes of a Windows Forms TreeView control. TreeView Nodes provide properties that navigate through the TreeView control. dev_langs: - "csharp" - "vb" @@ -13,111 +14,32 @@ ms.assetid: 427f8928-ebcf-4beb-887f-695b905d5134 --- # How to: Iterate Through All Nodes of a Windows Forms TreeView Control -It is sometimes useful to examine every node in a Windows Forms control in order to perform some calculation on the node values. This operation can be done using a recursive procedure (recursive method in C# and C++) that iterates through each node in each collection of the tree. +It is sometimes useful to examine every node in a Windows Forms control in order to perform some calculation on the node values. This operation can be done using a recursive method (recursive procedure in VB.NET) that iterates through each node in each collection of the tree. Each object in a tree view has properties that you can use to navigate the tree view: , , , , and . The value of the property is the parent node of the current node. The child nodes of the current node, if there are any, are listed in its property. The control itself has the property, which is the root node of the entire tree view. -### To iterate through all nodes of the TreeView control +## Recursive approach -1. Create a recursive procedure (recursive method in C# and C++) that tests each node. +The recursive approach uses a method that processes a tree node, and then calls the same method for each child node. This repeats until every node in the tree is processed. The drawback to this approach is that if the tree is big, you may run into a stack overflow error and run out of memory. -2. Call the procedure. +The following example shows how to print each object's property: - The following example shows how to print each object's property: - - ```vb - Private Sub PrintRecursive(ByVal n As TreeNode) - System.Diagnostics.Debug.WriteLine(n.Text) - MessageBox.Show(n.Text) - Dim aNode As TreeNode - For Each aNode In n.Nodes - PrintRecursive(aNode) - Next - End Sub - - ' Call the procedure using the top nodes of the treeview. - Private Sub CallRecursive(ByVal aTreeView As TreeView) - Dim n As TreeNode - For Each n In aTreeView.Nodes - PrintRecursive(n) - Next - End Sub - ``` - - ```csharp - private void PrintRecursive(TreeNode treeNode) - { - // Print the node. - System.Diagnostics.Debug.WriteLine(treeNode.Text); - MessageBox.Show(treeNode.Text); - // Print each node recursively. - foreach (TreeNode tn in treeNode.Nodes) - { - PrintRecursive(tn); - } - } - - // Call the procedure using the TreeView. - private void CallRecursive(TreeView treeView) - { - // Print each node recursively. - TreeNodeCollection nodes = treeView.Nodes; - foreach (TreeNode n in nodes) - { - PrintRecursive(n); - } - } - ``` - - ```cpp - private: - void PrintRecursive( TreeNode^ treeNode ) - { - // Print the node. - System::Diagnostics::Debug::WriteLine( treeNode->Text ); - MessageBox::Show( treeNode->Text ); - - // Print each node recursively. - System::Collections::IEnumerator^ myNodes = (safe_cast(treeNode->Nodes))->GetEnumerator(); - try - { - while ( myNodes->MoveNext() ) - { - TreeNode^ tn = safe_cast(myNodes->Current); - PrintRecursive( tn ); - } - } - finally - { - IDisposable^ disposable = dynamic_cast(myNodes); - if ( disposable != nullptr ) - disposable->Dispose(); - } - } - - // Call the procedure using the TreeView. - void CallRecursive( TreeView^ treeView ) - { - // Print each node recursively. - TreeNodeCollection^ nodes = treeView->Nodes; - System::Collections::IEnumerator^ myNodes = (safe_cast(nodes))->GetEnumerator(); - try - { - while ( myNodes->MoveNext() ) - { - TreeNode^ n = safe_cast(myNodes->Current); - PrintRecursive( n ); - } - } - finally - { - IDisposable^ disposable = dynamic_cast(myNodes); - if ( disposable != nullptr ) - disposable->Dispose(); - } - } - ``` +:::code language="csharp" source="snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cs/Form1.cs" id="PrintRecursive"::: + +:::code language="vb" source="snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/Form1.vb" id="PrintRecursive"::: + +:::code language="cpp" source="snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cpp/MyForm.h" id="PrintRecursive"::: +## Non-recursive approach + +The following example is an alternate iterative approach to traversing the nodes of the tree using a collection. This approach doesn't follow the parent-child relationship of a node only ensures every node is printed. If you want to process each tree node and its children, first, use the collection + +:::code language="csharp" source="snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cs/Form1.cs" id="PrintNonRecursive"::: + +:::code language="vb" source="snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/Form1.vb" id="PrintNonRecursive"::: + +:::code language="cpp" source="snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cpp/MyForm.h" id="PrintNonRecursive"::: + ## See also - [TreeView Control](treeview-control-windows-forms.md) diff --git a/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cpp/MyForm.cpp b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cpp/MyForm.cpp new file mode 100644 index 0000000..ae98976 --- /dev/null +++ b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cpp/MyForm.cpp @@ -0,0 +1,13 @@ +#include "MyForm.h" + +using namespace System; +using namespace System::Windows::Forms; +[STAThreadAttribute] + +void main(array^ args) { + Application::SetCompatibleTextRenderingDefault(false); + Application::EnableVisualStyles(); + project::MyForm frm; + Application::Run(%frm); +} + diff --git a/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cpp/MyForm.h b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cpp/MyForm.h new file mode 100644 index 0000000..4fafc95 --- /dev/null +++ b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cpp/MyForm.h @@ -0,0 +1,267 @@ +#pragma once +#include "Student.h" +#include "Subject.h" +#include "TextBook.h" + +namespace project { + + using namespace System; + using namespace System::ComponentModel; + using namespace System::Collections; + using namespace System::Windows::Forms; + using namespace System::Data; + using namespace System::Drawing; + + /// + /// Summary for MyForm + /// + public ref class MyForm : public System::Windows::Forms::Form + { + public: + MyForm(void) + { + InitializeComponent(); + + PopulateTreeView(); + + //Below method implementations will start printing node values to MessageBoxes and can be time consuming. + //An alternate way to check is adding two labels and printing the values of the total nodes in each using both the below traversal methods. + + //CallRecursive(trvTest); + + //CallNonRecursive(trvTest); + } + + private: + //List of all students + ArrayList^ studentArray = gcnew ArrayList(); + + void PopulateTreeView() + { + // Add students to the ArrayList of Student objects. + for (int x = 0; x < 5; x++) + { + studentArray->Add(gcnew Student("Student " + x.ToString())); + } + + // Add subjects to each Student object in the ArrayList. + for(int x = 0; x < 5; x++) + { + for (int y = 0; y < 10; y++) + { + safe_cast(studentArray[x])->StudentSubjects->Add(gcnew Subject("Subject " + y.ToString())); + } + } + + // Add subjects to each Student object in the ArrayList. + for (int x = 0; x < 5; x++) + { + for (int y = 0; y < 10; y++) + { + for (int z = 0; z < 3; z++) + { + safe_cast(safe_cast(studentArray[x])->StudentSubjects[y])->SubjectTextbooks->Add(gcnew TextBook("TextBook " + z.ToString())); + } + } + } + + // Suppress repainting the TreeView until all the objects have been created. + trvTest->BeginUpdate(); + + // Clear the TreeView each time the method is called. + trvTest->Nodes->Clear(); + + System::Collections::IEnumerator^ studArray = (safe_cast(studentArray))->GetEnumerator(); + + // Add a root TreeNode for each Student object in the ArrayList. + while (studArray->MoveNext()) + { + trvTest->Nodes->Add(gcnew TreeNode(safe_cast(studArray->Current)->StudentName)); + + System::Collections::IEnumerator^ studSubjects = (safe_cast(safe_cast(studArray->Current)->StudentSubjects))->GetEnumerator(); + + while (studSubjects->MoveNext()) + { + trvTest->Nodes[studentArray->IndexOf(safe_cast(studArray->Current))]->Nodes->Add(gcnew TreeNode(safe_cast(studSubjects->Current)->SubjectID)); + + System::Collections::IEnumerator^ subTextbooks = (safe_cast(safe_cast(studSubjects->Current)->SubjectTextbooks))->GetEnumerator(); + + while (subTextbooks->MoveNext()) + { + trvTest->Nodes[studentArray->IndexOf(safe_cast(studArray->Current))]->Nodes[safe_cast(studArray->Current)->StudentSubjects->IndexOf(safe_cast(studSubjects->Current))]->Nodes->Add(gcnew TreeNode(safe_cast(subTextbooks->Current)->TextBookID)); + } + } + } + + // Begin repainting the TreeView. + trvTest->EndUpdate(); + } + + // + private: + void PrintRecursive(TreeNode^ treeNode) + { + // Print the node. + System::Diagnostics::Debug::WriteLine(treeNode->Text); + MessageBox::Show(treeNode->Text); + + // Print each node recursively. + System::Collections::IEnumerator^ myNodes = (safe_cast(treeNode->Nodes))->GetEnumerator(); + try + { + while (myNodes->MoveNext()) + { + TreeNode^ tn = safe_cast(myNodes->Current); + PrintRecursive(tn); + } + } + finally + { + delete(myNodes); + } + } + + // Call the procedure using the TreeView. + void CallRecursive(TreeView^ treeView) + { + // Print each node recursively. + TreeNodeCollection^ nodes = treeView->Nodes; + System::Collections::IEnumerator^ myNodes = (safe_cast(nodes))->GetEnumerator(); + try + { + while (myNodes->MoveNext()) + { + TreeNode^ n = safe_cast(myNodes->Current); + PrintRecursive(n); + } + } + finally + { + delete(myNodes); + } + } + // + + // + private: + void PrintNonRecursive(TreeNode^ treeNode) + { + //Using a queue to store and process each node in the TreeView + Queue^ staging = gcnew Queue(); + staging->Enqueue(treeNode); + while (staging->Count > 0) + { + treeNode = safe_cast(staging->Dequeue()); + + // Print the node. + System::Diagnostics::Debug::WriteLine(treeNode->Text); + MessageBox::Show(treeNode->Text); + + System::Collections::IEnumerator^ children = (safe_cast(treeNode->Nodes))->GetEnumerator(); + try + { + while (children->MoveNext()) + { + staging->Enqueue(children->Current); + } + } + finally + { + delete(children); + } + } + + // Print the node. + System::Diagnostics::Debug::WriteLine(treeNode->Text); + MessageBox::Show(treeNode->Text); + + // Print each node recursively. + System::Collections::IEnumerator^ myNodes = (safe_cast(treeNode->Nodes))->GetEnumerator(); + try + { + while (myNodes->MoveNext()) + { + TreeNode^ tn = safe_cast(myNodes->Current); + PrintRecursive(tn); + } + } + finally + { + delete(myNodes); + } + } + + // Call the procedure using the TreeView. + void CallNonRecursive(TreeView^ treeView) + { + // Print each node recursively. + TreeNodeCollection^ nodes = treeView->Nodes; + System::Collections::IEnumerator^ myNodes = (safe_cast(nodes))->GetEnumerator(); + try + { + while (myNodes->MoveNext()) + { + TreeNode^ n = safe_cast(myNodes->Current); + PrintNonRecursive(n); + } + } + finally + { + delete(myNodes); + } + } + // + + private: + + protected: + /// + /// Clean up any resources being used. + /// + ~MyForm() + { + if (components) + { + delete components; + } + } + private: System::Windows::Forms::TreeView^ trvTest; + protected: + + private: + /// + /// Required designer variable. + /// + System::ComponentModel::Container ^components; + +#pragma region Windows Form Designer generated code + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + void InitializeComponent(void) + { + this->trvTest = (gcnew System::Windows::Forms::TreeView()); + this->SuspendLayout(); + // + // trvTest + // + this->trvTest->Location = System::Drawing::Point(12, 12); + this->trvTest->Name = L"trvTest"; + this->trvTest->Size = System::Drawing::Size(474, 396); + this->trvTest->TabIndex = 0; + // + // MyForm + // + this->AutoScaleDimensions = System::Drawing::SizeF(8, 16); + this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; + this->ClientSize = System::Drawing::Size(498, 420); + this->Controls->Add(this->trvTest); + this->Name = L"MyForm"; + this->Text = L"MyForm"; + this->ResumeLayout(false); + + } +#pragma endregion + }; +} diff --git a/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cpp/MyForm.resx b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cpp/MyForm.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cpp/MyForm.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cpp/Student.h b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cpp/Student.h new file mode 100644 index 0000000..2197edc --- /dev/null +++ b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cpp/Student.h @@ -0,0 +1,20 @@ +#pragma once +namespace project { + + using namespace System; + using namespace System::Collections; + public ref class Student + { + private: + ArrayList^ _studentSubjects = gcnew ArrayList(); + public: + String^ StudentName = ""; + + ArrayList^ StudentSubjects = _studentSubjects; + + Student(String^ studentName) + { + StudentName = studentName; + } + }; +}; diff --git a/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cpp/Subject.h b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cpp/Subject.h new file mode 100644 index 0000000..a1d4d88 --- /dev/null +++ b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cpp/Subject.h @@ -0,0 +1,21 @@ +#pragma once +namespace project { + using namespace System; + using namespace System::Collections; + + public ref class Subject + { + private: + ArrayList^ _subjectTextbooks = gcnew ArrayList(); + public: + String^ SubjectID = ""; + + ArrayList^ SubjectTextbooks = _subjectTextbooks; + + Subject(String^ subjectID) + { + SubjectID = subjectID; + } + }; +} + diff --git a/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cpp/TextBook.h b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cpp/TextBook.h new file mode 100644 index 0000000..2766cc7 --- /dev/null +++ b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cpp/TextBook.h @@ -0,0 +1,16 @@ +#pragma once +namespace project { + using namespace System; + using namespace System::Collections; + + public ref class TextBook + { + public: + String^ TextBookID = ""; + + TextBook(String^ textBookID) + { + TextBookID = textBookID; + } + }; +} diff --git a/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cpp/project.vcxproj b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cpp/project.vcxproj new file mode 100644 index 0000000..4cb4c86 --- /dev/null +++ b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cpp/project.vcxproj @@ -0,0 +1,151 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + {A443B067-F8C3-4DFD-9F38-E6C840918865} + v4.8 + ManagedCProj + project + 10.0 + + + CurrentArchitecture + CurrentRuntime + + + + Application + true + v142 + true + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + true + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + + Level3 + WIN32;_DEBUG;%(PreprocessorDefinitions) + + + + Windows + main + + + + + Level3 + _DEBUG;%(PreprocessorDefinitions) + + + + Windows + main + + + + + Level3 + WIN32;NDEBUG;%(PreprocessorDefinitions) + + + + Windows + main + + + + + Level3 + NDEBUG;%(PreprocessorDefinitions) + + + + Windows + main + + + + + + + + + + + + + + + CppForm + + + + + + + + MyForm.h + + + + + + \ No newline at end of file diff --git a/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cpp/project.vcxproj.filters b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cpp/project.vcxproj.filters new file mode 100644 index 0000000..19b9402 --- /dev/null +++ b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cpp/project.vcxproj.filters @@ -0,0 +1,36 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + \ No newline at end of file diff --git a/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cpp/snippets.5000.json b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cpp/snippets.5000.json new file mode 100644 index 0000000..9493e73 --- /dev/null +++ b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cpp/snippets.5000.json @@ -0,0 +1,3 @@ +{ + "host": "visualstudio" +} \ No newline at end of file diff --git a/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cs/Form1.Designer.cs b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cs/Form1.Designer.cs new file mode 100644 index 0000000..d3c9273 --- /dev/null +++ b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cs/Form1.Designer.cs @@ -0,0 +1,59 @@ + +namespace project +{ + partial class Form1 + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.trvTest = new System.Windows.Forms.TreeView(); + this.SuspendLayout(); + // + // trvTest + // + this.trvTest.Location = new System.Drawing.Point(12, 12); + this.trvTest.Name = "trvTest"; + this.trvTest.Size = new System.Drawing.Size(578, 426); + this.trvTest.TabIndex = 0; + // + // Form1 + // + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(598, 451); + this.Controls.Add(this.trvTest); + this.Name = "Form1"; + this.Text = "Form1"; + this.ResumeLayout(false); + + } + + #endregion + + private System.Windows.Forms.TreeView trvTest; + } +} + diff --git a/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cs/Form1.cs b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cs/Form1.cs new file mode 100644 index 0000000..a70d91b --- /dev/null +++ b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cs/Form1.cs @@ -0,0 +1,158 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Windows.Forms; + +namespace project +{ + public partial class Form1 : Form + { + //List of all students + private ArrayList studentArray = new ArrayList(); + + //Windows Form application + public Form1() + { + InitializeComponent(); + + //Loading the TreeView with data + PopulateTreeView(); + + //Below method implementations will start printing node values to MessageBoxes and can be time consuming. + //An alternate way to check is adding two labels and printing the values of the total nodes in each using both the below traversal methods. + + //Printing the total number of nodes recursively + //CallRecursive(trvTest); + + //CallNonRecursive(trvTest); + } + + // + private void PrintRecursive(TreeNode treeNode) + { + // Print the node. + System.Diagnostics.Debug.WriteLine(treeNode.Text); + MessageBox.Show(treeNode.Text); + + // Visit each node recursively. + foreach (TreeNode tn in treeNode.Nodes) + { + PrintRecursive(tn); + } + } + + // Call the procedure using the TreeView. + private void CallRecursive(TreeView treeView) + { + // Print each node recursively. + foreach (TreeNode n in treeView.Nodes) + { + //recursiveTotalNodes++; + PrintRecursive(n); + } + } + // + + // + private void PrintNonRecursive(TreeNode treeNode) + { + if (treeNode != null) + { + //Using a queue to store and process each node in the TreeView + Queue staging = new Queue(); + staging.Enqueue(treeNode); + + while (staging.Count > 0) + { + treeNode = staging.Dequeue(); + + // Print the node. + System.Diagnostics.Debug.WriteLine(treeNode.Text); + MessageBox.Show(treeNode.Text); + + foreach (TreeNode node in treeNode.Nodes) + { + staging.Enqueue(node); + } + } + } + } + + // Call the procedure using the TreeView. + private void CallNonRecursive(TreeView treeView) + { + // Print each node. + foreach (TreeNode n in treeView.Nodes) + { + PrintNonRecursive(n); + } + } + // + + private void PopulateTreeView() + { + // Add students to the ArrayList of Student objects. + for (int x = 0; x < 5; x++) + { + studentArray.Add(new Student("Student " + x.ToString())); + } + + // Add subjects to each Student object in the ArrayList. + foreach (Student student in studentArray) + { + for (int y = 0; y < 10; y++) + { + student.StudentSubjects.Add(new Subject("Subject " + y.ToString())); + } + } + + Random _random = new Random(); + + // Add subjects to each Student object in the ArrayList. + foreach (Student student in studentArray) + { + foreach (Subject subject in student.StudentSubjects) + { + var gen = _random.Next(1, 10); + + for (int y = 0; y < gen; y++) + { + subject.SubjectTextbooks.Add(new TextBook("TextBook " + y.ToString())); + } + } + } + + // Suppress repainting the TreeView until all the objects have been created. + trvTest.BeginUpdate(); + + // Clear the TreeView each time the method is called. + trvTest.Nodes.Clear(); + + // Add a root TreeNode for each Student object in the ArrayList. + foreach (Student student in studentArray) + { + trvTest.Nodes.Add(new TreeNode(student.StudentName)); + + // Add a child treenode for each Subject object in the current student object. + foreach (Subject subject in student.StudentSubjects) + { + trvTest.Nodes[studentArray.IndexOf(student)].Nodes.Add( + new TreeNode(subject.SubjectID)); + + // Add a child treenode for each TextBook object in the current Subject object. + + foreach (TextBook textbook in subject.SubjectTextbooks) + { + trvTest.Nodes[studentArray.IndexOf(student)].Nodes[student.StudentSubjects.IndexOf(subject)].Nodes.Add( + new TreeNode(textbook.TextBookID)); + } + } + } + + // Begin repainting the TreeView. + trvTest.EndUpdate(); + } + + + } +} diff --git a/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cs/Form1.resx b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cs/Form1.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cs/Form1.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cs/Program.cs b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cs/Program.cs new file mode 100644 index 0000000..b7580e1 --- /dev/null +++ b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cs/Program.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace project +{ + static class Program + { + /// + /// The main entry point for the application. + /// + [STAThread] + static void Main() + { + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + Application.Run(new Form1()); + } + } +} diff --git a/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cs/Student.cs b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cs/Student.cs new file mode 100644 index 0000000..5cba221 --- /dev/null +++ b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cs/Student.cs @@ -0,0 +1,19 @@ +using System.Collections; + +namespace project +{ + // The basic Student class. + public class Student + { + protected ArrayList _studentSubjects = new ArrayList(); + + public Student(string studentName) + { + StudentName = studentName; + } + + public string StudentName { get; set; } = ""; + + public ArrayList StudentSubjects => _studentSubjects; + } +} diff --git a/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cs/Subject.cs b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cs/Subject.cs new file mode 100644 index 0000000..cc1697c --- /dev/null +++ b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cs/Subject.cs @@ -0,0 +1,19 @@ +using System.Collections; + +namespace project +{ + // The basic student Subject class. + public class Subject + { + protected ArrayList _subjectTextbooks = new ArrayList(); + + public Subject(string subjectID) + { + SubjectID = subjectID; + } + + public string SubjectID { get; set; } = ""; + + public ArrayList SubjectTextbooks => _subjectTextbooks; + } +} diff --git a/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cs/TextBook.cs b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cs/TextBook.cs new file mode 100644 index 0000000..3193e28 --- /dev/null +++ b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cs/TextBook.cs @@ -0,0 +1,13 @@ +namespace project +{ + public class TextBook + { + + public TextBook(string textBookID) + { + TextBookID = textBookID; + } + + public string TextBookID { get; set; } = ""; + } +} diff --git a/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cs/project.csproj b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cs/project.csproj new file mode 100644 index 0000000..1658c96 --- /dev/null +++ b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/cs/project.csproj @@ -0,0 +1,68 @@ + + + + + Debug + AnyCPU + {50B5AD53-1181-46C6-91EE-0E65BADF34E1} + WinExe + project + project + v4.8 + 512 + true + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + CurrentArchitecture + CurrentRuntime + + + + + + + + + + + + + + + + + Form + + + Form1.cs + + + + + + + Form1.cs + + + + diff --git a/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/App.config b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/App.config new file mode 100644 index 0000000..1c75772 --- /dev/null +++ b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/Form1.Designer.vb b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/Form1.Designer.vb new file mode 100644 index 0000000..575fc85 --- /dev/null +++ b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/Form1.Designer.vb @@ -0,0 +1,73 @@ + _ +Partial Class Form1 + Inherits System.Windows.Forms.Form + + 'Form overrides dispose to clean up the component list. + _ + Protected Overrides Sub Dispose(ByVal disposing As Boolean) + Try + If disposing AndAlso components IsNot Nothing Then + components.Dispose() + End If + Finally + MyBase.Dispose(disposing) + End Try + End Sub + + 'Required by the Windows Form Designer + Private components As System.ComponentModel.IContainer + + 'NOTE: The following procedure is required by the Windows Form Designer + 'It can be modified using the Windows Form Designer. + 'Do not modify it using the code editor. + + Private Sub InitializeComponent() + Me.trvTest = New System.Windows.Forms.TreeView() + Me.Label1 = New System.Windows.Forms.Label() + Me.Label2 = New System.Windows.Forms.Label() + Me.SuspendLayout() + ' + 'trvTest + ' + Me.trvTest.Location = New System.Drawing.Point(12, 28) + Me.trvTest.Name = "trvTest" + Me.trvTest.Size = New System.Drawing.Size(413, 410) + Me.trvTest.TabIndex = 0 + ' + 'Label1 + ' + Me.Label1.AutoSize = True + Me.Label1.Location = New System.Drawing.Point(538, 65) + Me.Label1.Name = "Label1" + Me.Label1.Size = New System.Drawing.Size(51, 17) + Me.Label1.TabIndex = 1 + Me.Label1.Text = "Label1" + ' + 'Label2 + ' + Me.Label2.AutoSize = True + Me.Label2.Location = New System.Drawing.Point(538, 104) + Me.Label2.Name = "Label2" + Me.Label2.Size = New System.Drawing.Size(51, 17) + Me.Label2.TabIndex = 2 + Me.Label2.Text = "Label2" + ' + 'Form1 + ' + Me.AutoScaleDimensions = New System.Drawing.SizeF(8.0!, 16.0!) + Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font + Me.ClientSize = New System.Drawing.Size(800, 450) + Me.Controls.Add(Me.Label2) + Me.Controls.Add(Me.Label1) + Me.Controls.Add(Me.trvTest) + Me.Name = "Form1" + Me.Text = "Form1" + Me.ResumeLayout(False) + Me.PerformLayout() + + End Sub + + Friend WithEvents trvTest As TreeView + Friend WithEvents Label1 As Label + Friend WithEvents Label2 As Label +End Class diff --git a/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/Form1.resx b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/Form1.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/Form1.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/Form1.vb b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/Form1.vb new file mode 100644 index 0000000..32400a8 --- /dev/null +++ b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/Form1.vb @@ -0,0 +1,111 @@ +Imports project.project + +Public Class Form1 + Private studentArray As ArrayList = New ArrayList() + + Public Sub New() + ' This call is required by the designer. + InitializeComponent() + + ' Add any initialization after the InitializeComponent() call. + 'Loading the TreeView with data + PopulateTreeView() + + 'Below method implementations will start printing node values to MessageBoxes and can be time consuming. + 'An alternate way to check is adding two labels and printing the values of the total nodes in each using both the below traversal methods. + + 'Print all the nodes recursively + 'CallRecursive(trvTest) + 'CallNonRecursive(trvTest) + End Sub + + ' + Private Sub PrintRecursive(n As TreeNode) + System.Diagnostics.Debug.WriteLine(n.Text) + MessageBox.Show(n.Text) + Dim aNode As TreeNode + For Each aNode In n.Nodes + PrintRecursive(aNode) + Next + End Sub + + ' Call the procedure using the top nodes of the treeview. + Private Sub CallRecursive(aTreeView As TreeView) + Dim n As TreeNode + For Each n In aTreeView.Nodes + PrintRecursive(n) + Next + End Sub + ' + + ' + Private Sub PrintNonrecursive(n As TreeNode) + If n IsNot Nothing Then + Dim staging As Queue(Of TreeNode) = New Queue(Of TreeNode) + staging.Enqueue(n) + While staging.Count > 0 + n = staging.Dequeue() + + 'Print the node. + System.Diagnostics.Debug.WriteLine(n.Text) + MessageBox.Show(n.Text) + + Dim node As TreeNode + For Each node In n.Nodes + staging.Enqueue(node) + Next + End While + End If + End Sub + + Private Sub CallNonRecursive(aTreeView As TreeView) + Dim n As TreeNode + For Each n In aTreeView.Nodes + PrintNonrecursive(n) + Next + End Sub + ' + + Private Sub PopulateTreeView() + For x As Integer = 0 To 4 + studentArray.Add(New Student("Student " & x.ToString())) + Next + + For Each student As Student In studentArray + + For y As Integer = 0 To 9 + student.StudentSubjects.Add(New Subject("Subject " & y.ToString())) + Next + Next + + Dim _random As Random = New Random() + + For Each student As Student In studentArray + + For Each subject As Subject In student.StudentSubjects + Dim gen = _random.[Next](1, 10) + + For y As Integer = 0 To gen - 1 + subject.SubjectTextbooks.Add(New TextBook("TextBook " & y.ToString())) + Next + Next + Next + + trvTest.BeginUpdate() + trvTest.Nodes.Clear() + + For Each student As Student In studentArray + trvTest.Nodes.Add(New TreeNode(student.StudentName)) + + For Each subject As Subject In student.StudentSubjects + trvTest.Nodes(studentArray.IndexOf(student)).Nodes.Add(New TreeNode(subject.SubjectID)) + + For Each textbook As TextBook In subject.SubjectTextbooks + trvTest.Nodes(studentArray.IndexOf(student)).Nodes(student.StudentSubjects.IndexOf(subject)).Nodes.Add(New TreeNode(textbook.TextBookID)) + Next + Next + Next + + trvTest.EndUpdate() + End Sub +End Class diff --git a/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/My Project/Application.Designer.vb b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/My Project/Application.Designer.vb new file mode 100644 index 0000000..5e05c10 --- /dev/null +++ b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/My Project/Application.Designer.vb @@ -0,0 +1,38 @@ +'------------------------------------------------------------------------------ +' +' This code was generated by a tool. +' Runtime Version:4.0.30319.42000 +' +' Changes to this file may cause incorrect behavior and will be lost if +' the code is regenerated. +' +'------------------------------------------------------------------------------ + +Option Strict On +Option Explicit On + + +Namespace My + + 'NOTE: This file is auto-generated; do not modify it directly. To make changes, + ' or if you encounter build errors in this file, go to the Project Designer + ' (go to Project Properties or double-click the My Project node in + ' Solution Explorer), and make changes on the Application tab. + ' + Partial Friend Class MyApplication + + _ + Public Sub New() + MyBase.New(Global.Microsoft.VisualBasic.ApplicationServices.AuthenticationMode.Windows) + Me.IsSingleInstance = false + Me.EnableVisualStyles = true + Me.SaveMySettingsOnExit = true + Me.ShutDownStyle = Global.Microsoft.VisualBasic.ApplicationServices.ShutdownMode.AfterMainFormCloses + End Sub + + _ + Protected Overrides Sub OnCreateMainForm() + Me.MainForm = Global.project.Form1 + End Sub + End Class +End Namespace diff --git a/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/My Project/Application.myapp b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/My Project/Application.myapp new file mode 100644 index 0000000..1243847 --- /dev/null +++ b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/My Project/Application.myapp @@ -0,0 +1,11 @@ + + + true + Form1 + false + 0 + true + 0 + 0 + true + diff --git a/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/My Project/AssemblyInfo.vb b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/My Project/AssemblyInfo.vb new file mode 100644 index 0000000..76ad56a --- /dev/null +++ b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/My Project/AssemblyInfo.vb @@ -0,0 +1,35 @@ +Imports System +Imports System.Reflection +Imports System.Runtime.InteropServices + +' General Information about an assembly is controlled through the following +' set of attributes. Change these attribute values to modify the information +' associated with an assembly. + +' Review the values of the assembly attributes + + + + + + + + + + +'The following GUID is for the ID of the typelib if this project is exposed to COM + + +' Version information for an assembly consists of the following four values: +' +' Major Version +' Minor Version +' Build Number +' Revision +' +' You can specify all the values or you can default the Build and Revision Numbers +' by using the '*' as shown below: +' + + + diff --git a/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/My Project/Resources.Designer.vb b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/My Project/Resources.Designer.vb new file mode 100644 index 0000000..6a8ced3 --- /dev/null +++ b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/My Project/Resources.Designer.vb @@ -0,0 +1,62 @@ +'------------------------------------------------------------------------------ +' +' This code was generated by a tool. +' Runtime Version:4.0.30319.42000 +' +' Changes to this file may cause incorrect behavior and will be lost if +' the code is regenerated. +' +'------------------------------------------------------------------------------ + +Option Strict On +Option Explicit On + + +Namespace My.Resources + + 'This class was auto-generated by the StronglyTypedResourceBuilder + 'class via a tool like ResGen or Visual Studio. + 'To add or remove a member, edit your .ResX file then rerun ResGen + 'with the /str option, or rebuild your VS project. + ''' + ''' A strongly-typed resource class, for looking up localized strings, etc. + ''' + _ + Friend Module Resources + + Private resourceMan As Global.System.Resources.ResourceManager + + Private resourceCulture As Global.System.Globalization.CultureInfo + + ''' + ''' Returns the cached ResourceManager instance used by this class. + ''' + _ + Friend ReadOnly Property ResourceManager() As Global.System.Resources.ResourceManager + Get + If Object.ReferenceEquals(resourceMan, Nothing) Then + Dim temp As Global.System.Resources.ResourceManager = New Global.System.Resources.ResourceManager("project.Resources", GetType(Resources).Assembly) + resourceMan = temp + End If + Return resourceMan + End Get + End Property + + ''' + ''' Overrides the current thread's CurrentUICulture property for all + ''' resource lookups using this strongly typed resource class. + ''' + _ + Friend Property Culture() As Global.System.Globalization.CultureInfo + Get + Return resourceCulture + End Get + Set(ByVal value As Global.System.Globalization.CultureInfo) + resourceCulture = value + End Set + End Property + End Module +End Namespace diff --git a/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/My Project/Resources.resx b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/My Project/Resources.resx new file mode 100644 index 0000000..af7dbeb --- /dev/null +++ b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/My Project/Resources.resx @@ -0,0 +1,117 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/My Project/Settings.Designer.vb b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/My Project/Settings.Designer.vb new file mode 100644 index 0000000..0515589 --- /dev/null +++ b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/My Project/Settings.Designer.vb @@ -0,0 +1,73 @@ +'------------------------------------------------------------------------------ +' +' This code was generated by a tool. +' Runtime Version:4.0.30319.42000 +' +' Changes to this file may cause incorrect behavior and will be lost if +' the code is regenerated. +' +'------------------------------------------------------------------------------ + +Option Strict On +Option Explicit On + + +Namespace My + + _ + Partial Friend NotInheritable Class MySettings + Inherits Global.System.Configuration.ApplicationSettingsBase + + Private Shared defaultInstance As MySettings = CType(Global.System.Configuration.ApplicationSettingsBase.Synchronized(New MySettings), MySettings) + +#Region "My.Settings Auto-Save Functionality" +#If _MyType = "WindowsForms" Then + Private Shared addedHandler As Boolean + + Private Shared addedHandlerLockObject As New Object + + _ + Private Shared Sub AutoSaveSettings(ByVal sender As Global.System.Object, ByVal e As Global.System.EventArgs) + If My.Application.SaveMySettingsOnExit Then + My.Settings.Save() + End If + End Sub +#End If +#End Region + + Public Shared ReadOnly Property [Default]() As MySettings + Get + +#If _MyType = "WindowsForms" Then + If Not addedHandler Then + SyncLock addedHandlerLockObject + If Not addedHandler Then + AddHandler My.Application.Shutdown, AddressOf AutoSaveSettings + addedHandler = True + End If + End SyncLock + End If +#End If + Return defaultInstance + End Get + End Property + End Class +End Namespace + +Namespace My + + _ + Friend Module MySettingsProperty + + _ + Friend ReadOnly Property Settings() As Global.project.My.MySettings + Get + Return Global.project.My.MySettings.Default + End Get + End Property + End Module +End Namespace diff --git a/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/My Project/Settings.settings b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/My Project/Settings.settings new file mode 100644 index 0000000..85b890b --- /dev/null +++ b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/My Project/Settings.settings @@ -0,0 +1,7 @@ + + + + + + + diff --git a/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/Student.vb b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/Student.vb new file mode 100644 index 0000000..85c13bd --- /dev/null +++ b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/Student.vb @@ -0,0 +1,17 @@ +Namespace project + Public Class Student + Protected _studentSubjects As ArrayList = New ArrayList() + + Public Sub New(studentName As String) + Me.StudentName = studentName + End Sub + + Public Property StudentName As String = "" + + Public ReadOnly Property StudentSubjects As ArrayList + Get + Return _studentSubjects + End Get + End Property + End Class +End Namespace diff --git a/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/Subject.vb b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/Subject.vb new file mode 100644 index 0000000..a67e042 --- /dev/null +++ b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/Subject.vb @@ -0,0 +1,17 @@ +Namespace project + Public Class Subject + Protected _subjectTextbooks As ArrayList = New ArrayList() + + Public Sub New(ByVal subjectID As String) + Me.SubjectID = subjectID + End Sub + + Public Property SubjectID As String = "" + + Public ReadOnly Property SubjectTextbooks As ArrayList + Get + Return _subjectTextbooks + End Get + End Property + End Class +End Namespace diff --git a/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/TextBook.vb b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/TextBook.vb new file mode 100644 index 0000000..410ea2b --- /dev/null +++ b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/TextBook.vb @@ -0,0 +1,9 @@ +Namespace project + Public Class TextBook + Public Sub New(textBookID As String) + Me.TextBookID = textBookID + End Sub + + Public Property TextBookID As String = "" + End Class +End Namespace diff --git a/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/project.vbproj b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/project.vbproj new file mode 100644 index 0000000..a16ca76 --- /dev/null +++ b/dotnet-desktop-guide/framework/winforms/controls/snippets/how-to-iterate-through-all-nodes-of-a-windows-forms-treeview-control/vb/project.vbproj @@ -0,0 +1,130 @@ + + + + + Debug + AnyCPU + {294C4A08-390D-4D86-9D54-0A22DC730AB3} + WinExe + project.My.MyApplication + project + project + 512 + WindowsForms + v4.8 + true + true + + + AnyCPU + true + full + true + true + bin\Debug\ + project.xml + 42016,41999,42017,42018,42019,42032,42036,42020,42021,42022 + + + AnyCPU + pdbonly + false + true + true + bin\Release\ + project.xml + 42016,41999,42017,42018,42019,42032,42036,42020,42021,42022 + + + CurrentArchitecture + CurrentRuntime + + + On + + + Binary + + + Off + + + On + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Form + + + Form1.vb + Form + + + + True + Application.myapp + + + True + True + Resources.resx + + + True + Settings.settings + True + + + + + + + + Form1.vb + + + VbMyResourcesResXFileCodeGenerator + Resources.Designer.vb + My.Resources + Designer + + + + + MyApplicationCodeGenerator + Application.Designer.vb + + + SettingsSingleFileGenerator + My + Settings.Designer.vb + + + + + \ No newline at end of file