Items Manipulation Example for WinForms

Nevron NOV UserInterface for WinForms ships with hundreds of examples with full source code, as part of the Nevron Open Vision Suite. All WinForms UserInterface examples are also available as a stand-alone Visual Studio solution in C# and VB.NET, which you can download from the links below.

Items Manipulation Example for WinForms Description

This example demonstrates how to create a simple tree view with text only items and how to add/remove items. You can use the buttons on the right to add/remove items from the tree view.


Tree View Items Manipulation Example

C# Items Manipulation Example Code for Nevron UserInterface for WinForms
Language: C#

using Nevron.Nov;
using Nevron.Nov.DataStructures;
using Nevron.Nov.Dom;
using Nevron.Nov.Editors;
using Nevron.Nov.Graphics;
using Nevron.Nov.Layout;
using Nevron.Nov.UI;
using Nevron.Nov.Windows.Forms;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace NovWinFormsProject
{
	public partial class Form1 : Form
	{
		#region Constructors

		public Form1()
		{
			InitializeComponent();

			// Set form title
			Text = "NOV WinForms - NTreeViewItemsManipulationExample";

			// Create the NOV example and its controls
			NWidget exampleContent = CreateExampleContent();
			NWidget exampleControls = CreateExampleControls();
			if (exampleControls != null)
			{
				exampleControls.PreferredWidth = 300;
			}

			// Place the example and its controls in a pair box
			NPairBox pairBox = new NPairBox(exampleContent, exampleControls);
			pairBox.FillMode = ENStackFillMode.First;
			pairBox.FitMode = ENStackFitMode.First;
			pairBox.Margins = new NMargins(NDesign.HorizontalSpacing, NDesign.VerticalSpacing);

			// Host the example in the form
			NNovWidgetHost host = new NNovWidgetHost(pairBox);
			host.Dock = DockStyle.Fill;
			Controls.Add(host);
		}

		#endregion

		#region Example

		public NWidget CreateExampleContent()
		{
			// Create the tree view
			m_TreeView = new NTreeView();
			m_TreeView.HorizontalPlacement = ENHorizontalPlacement.Left;
			// Check whether the application is in touch mode and set the width of the tree view.
			bool touchMode = NApplication.Desktop.TouchMode;
			m_TreeView.PreferredWidth = touchMode ? 300 : 200;

			// Add some items
			for (int i = 0; i < 32; i++)
			{
				bool checkBox = (i >= 8 && i < 16) || i >= 24;
				bool image = i >= 16;

				NTreeViewItem l1Item = CreateTreeViewItem(String.Format("Item {0}", i));
				m_TreeView.Items.Add(l1Item);

				for (int j = 0; j < 8; j++)
				{
					NTreeViewItem l2Item = CreateTreeViewItem(String.Format("Item {0}.{1}", i, j));
					l1Item.Items.Add(l2Item);

					for (int k = 0; k < 2; k++)
					{
						NTreeViewItem l3Item = CreateTreeViewItem(String.Format("Item {0}.{1}.{2}", i, j, k));
						l2Item.Items.Add(l3Item);
					}
				}
			}

			// Hook to the tree view events
			m_TreeView.SelectedPathChanged += OnTreeViewSelectedPathChanged;
			return m_TreeView;
		}
		public NWidget CreateExampleControls()
		{
			NStackPanel stack = new NStackPanel();
			stack.FillMode = ENStackFillMode.Last;
			stack.FitMode = ENStackFitMode.Last;

			// Create the commands group box
			stack.Add(CreateCommandsGroupBox());

			// Create the events log
			m_EventsLog = new NExampleEventsLog();
			stack.Add(m_EventsLog);

			return stack;
		}
		public string GetExampleDescription()
		{
			return @"

This example demonstrates how to create a simple tree view with text only items and how to add/remove items. You can use the buttons on the right to add/remove items from the tree view.

"; } /// /// Gets the relevant documentation links for the example /// /// public (string Name, string Url)[] GetDocumentationUrls() { return new[] { ("Tree View", "https://www.nevron.com/docs/openvision/ui-tree-view.html"), ("Widget Elements", "https://www.nevron.com/docs/openvision/ui-widget-elements.html"), ("User Input", "https://www.nevron.com/docs/openvision/ui-user-input.html"), }; } #endregion #region Implementation private NTreeViewItem CreateTreeViewItem(string text) { NTreeViewItem item = new NTreeViewItem(text); item.Tag = text; item.ExpandedChanged += OnTreeViewItemExpandedChanged; return item; } private NGroupBox CreateCommandsGroupBox() { NStackPanel commandsStack = new NStackPanel(); // Create the command buttons m_AddButton = new NButton("Add Child Item"); m_AddButton.Click += OnAddButtonClicked; commandsStack.Add(m_AddButton); m_InsertBeforeButton = new NButton("Insert Item Before"); m_InsertBeforeButton.Click += OnAddButtonClicked; commandsStack.Add(m_InsertBeforeButton); m_InsertAfterButton = new NButton("Insert Item After"); m_InsertAfterButton.Click += OnAddButtonClicked; commandsStack.Add(m_InsertAfterButton); NButton expandAllButton = new NButton("Expand All"); expandAllButton.Margins = new NMargins(0, 15, 0, 0); expandAllButton.Click += OnExpandAllClicked; commandsStack.Add(expandAllButton); NButton collapseAllButton = new NButton("Collapse All"); collapseAllButton.Margins = new NMargins(0, 0, 0, 15); collapseAllButton.Click += OnCollapseAllClicked; commandsStack.Add(collapseAllButton); m_RemoveSelectedButton = new NButton("Remove Selected"); m_RemoveSelectedButton.Click += OnRemoveSelectedButtonClicked; commandsStack.Add(m_RemoveSelectedButton); m_RemoveChildrenButton = new NButton("Remove Children"); m_RemoveChildrenButton.Click += OnRemoveChildrenButtonClicked; commandsStack.Add(m_RemoveChildrenButton); m_RemoveAllButton = new NButton("Remove All"); m_RemoveAllButton.Click += OnRemoveAllButtonClicked; commandsStack.Add(m_RemoveAllButton); // Create the commands group box NGroupBox commmandsGroupBox = new NGroupBox("Commands"); commmandsGroupBox.Content = commandsStack; UpdateButtonsState(); return commmandsGroupBox; } private void UpdateButtonsState() { NTreeViewItem selectedItem = m_TreeView.SelectedItem; if (selectedItem == null) { m_InsertBeforeButton.Enabled = false; m_InsertAfterButton.Enabled = false; m_RemoveSelectedButton.Enabled = false; m_RemoveChildrenButton.Enabled = false; } else { m_InsertBeforeButton.Enabled = true; m_InsertAfterButton.Enabled = true; m_RemoveSelectedButton.Enabled = true; m_RemoveChildrenButton.Enabled = selectedItem.Items.Count > 0; } } #endregion #region Event Handlers private void OnAddButtonClicked(NEventArgs args) { NTreeViewItem selItem = m_TreeView.SelectedItem; string text = selItem == null ? "Item " + m_TreeView.Items.Count.ToString() : selItem.Tag.ToString(); if (args.TargetNode == m_AddButton) { if (selItem == null) { // Add the item as a last item in the tree view m_TreeView.Items.Add(CreateTreeViewItem(text)); } else { // Add the item as a last item in the selected item text += "." + selItem.Items.Count.ToString(); selItem.Items.Add(CreateTreeViewItem(text)); } } else if (args.TargetNode == m_InsertBeforeButton) { // Insert the item before the selected one NTreeViewItemCollection items = (NTreeViewItemCollection)selItem.ParentNode; text += ".Before"; items.Insert(selItem.IndexInParent, CreateTreeViewItem(text)); } else if (args.TargetNode == m_InsertAfterButton) { // Insert the item after the selected one NTreeViewItemCollection items = (NTreeViewItemCollection)selItem.ParentNode; text += ".After"; items.Insert(selItem.IndexInParent + 1, CreateTreeViewItem(text)); } if (m_TreeView.Items.Count == 1) { m_RemoveAllButton.Enabled = true; } } private void OnRemoveSelectedButtonClicked(NEventArgs args) { NTreeViewItem selectedItem = m_TreeView.SelectedItem; if (selectedItem == null) return; selectedItem.ParentNode.RemoveChild(selectedItem); } private void OnRemoveChildrenButtonClicked(NEventArgs arg1) { NTreeViewItem selectedItem = m_TreeView.SelectedItem; if (selectedItem == null) return; selectedItem.Items.RemoveAllChildren(); m_RemoveChildrenButton.Enabled = false; } private void OnRemoveAllButtonClicked(NEventArgs args) { m_TreeView.Items.Clear(); m_RemoveAllButton.Enabled = false; } private void OnExpandAllClicked(NEventArgs args) { IEnumerator treeIterator = m_TreeView.GetSubtreeIterator(ENTreeTraversalOrder.DepthFirstPreOrder, NIsFilter.Instance); IEnumerator itemIterator = new NAsEnumerator(treeIterator); while (itemIterator.MoveNext()) { itemIterator.Current.Expanded = true; } } private void OnCollapseAllClicked(NEventArgs args) { IEnumerator treeIterator = m_TreeView.GetSubtreeIterator(ENTreeTraversalOrder.DepthFirstPreOrder, NIsFilter.Instance); IEnumerator itemIterator = new NAsEnumerator(treeIterator); while (itemIterator.MoveNext()) { itemIterator.Current.Expanded = false; } } private void OnTreeViewSelectedPathChanged(NValueChangeEventArgs args) { UpdateButtonsState(); NTreeViewItem selectedItem = m_TreeView.SelectedItem; if (selectedItem != null) { m_EventsLog.LogEvent("Selected: " + selectedItem.Tag.ToString()); } } private void OnTreeViewItemExpandedChanged(NValueChangeEventArgs args) { NTreeViewItem item = (NTreeViewItem)args.TargetNode; if (item.Expanded) { m_EventsLog.LogEvent("Expanded: " + item.Tag.ToString()); } else { m_EventsLog.LogEvent("Collapsed: " + item.Tag.ToString()); } } #endregion #region Fields private NButton m_AddButton; private NButton m_InsertBeforeButton; private NButton m_InsertAfterButton; private NButton m_RemoveSelectedButton; private NButton m_RemoveChildrenButton; private NButton m_RemoveAllButton; private NTreeView m_TreeView; private NExampleEventsLog m_EventsLog; #endregion #region Static Methods #endregion #region Constants #endregion #region Nested Types #endregion } }

Download a fully functional C# (.cs) Items Manipulation Example for WinForms Solution

VB.NET Items Manipulation Example Code for Nevron UserInterface for WinForms
Language: VB.NET

Imports Nevron.Nov
Imports Nevron.Nov.DataStructures
Imports Nevron.Nov.Dom
Imports Nevron.Nov.Graphics
Imports Nevron.Nov.Layout
Imports Nevron.Nov.UI
Imports Nevron.Nov.Windows.Forms
Imports System.Collections.Generic

Public Class Form1

#Region "Constructors"

    Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        ' Set form title
        Text = "NOV WinForms - NTreeViewItemsManipulationExample"

        ' Create the NOV example and its controls
        Dim exampleContent As NWidget = CreateExampleContent()
        Dim exampleControls As NWidget = CreateExampleControls()
        If Not exampleControls Is Nothing Then
            exampleControls.PreferredWidth = 300
        End If

        ' Place the example and its controls in a pair box
        Dim pairBox As NPairBox = New NPairBox(exampleContent, exampleControls)
        pairBox.FillMode = ENStackFillMode.First
        pairBox.FitMode = ENStackFitMode.First
        pairBox.Margins = New NMargins(NDesign.HorizontalSpacing, NDesign.VerticalSpacing)

        ' Host the example in the form
        Dim host As NNovWidgetHost(Of NWidget) = New NNovWidgetHost(Of NWidget)(pairBox)
        host.Dock = DockStyle.Fill
        Controls.Add(host)
    End Sub


#End Region

#Region "Example"

        Public Function CreateExampleContent() As NWidget
            ' Create the tree view
            m_TreeView = New NTreeView()
            m_TreeView.HorizontalPlacement = ENHorizontalPlacement.Left
            ' Check whether the application is in touch mode and set the width of the tree view.
            Dim touchMode = NApplication.Desktop.TouchMode
            m_TreeView.PreferredWidth = If(touchMode, 300, 200)

            ' Add some items
            For i = 0 To 31
                Dim checkBox = i >= 8 AndAlso i < 16 OrElse i >= 24
                Dim image = i >= 16

                Dim l1Item = CreateTreeViewItem(String.Format("Item {0}", i))
                m_TreeView.Items.Add(l1Item)

                For j = 0 To 7
                    Dim l2Item = CreateTreeViewItem(String.Format("Item {0}.{1}", i, j))
                    l1Item.Items.Add(l2Item)

                    For k = 0 To 1
                        Dim l3Item = CreateTreeViewItem(String.Format("Item {0}.{1}.{2}", i, j, k))
                        l2Item.Items.Add(l3Item)
                    Next
                Next
            Next

            ' Hook to the tree view events
            Me.m_TreeView.SelectedPathChanged += AddressOf OnTreeViewSelectedPathChanged
            Return m_TreeView
        End Function
        Public Function CreateExampleControls() As NWidget
            Dim stack As NStackPanel = New NStackPanel()
            stack.FillMode = ENStackFillMode.Last
            stack.FitMode = ENStackFitMode.Last

            ' Create the commands group box
            stack.Add(CreateCommandsGroupBox())

            ' Create the events log
            m_EventsLog = New NExampleEventsLog()
            stack.Add(m_EventsLog)

            Return stack
        End Function
        Public Function GetExampleDescription() As String
            Return "

This example demonstrates how to create a simple tree view with text only items and how to add/remove items. You can use the buttons on the right to add/remove items from the tree view.

" End Function ''' ''' Gets the relevant documentation links for the example ''' ''' Public Function GetDocumentationUrls() As (String, String)() Return {("Tree View", "https://www.nevron.com/docs/openvision/ui-tree-view.html"), ("Widget Elements", "https://www.nevron.com/docs/openvision/ui-widget-elements.html"), ("User Input", "https://www.nevron.com/docs/openvision/ui-user-input.html")} End Function #End Region #Region "Implementation" Private Function CreateTreeViewItem(text As String) As NTreeViewItem Dim item As NTreeViewItem = New NTreeViewItem(text) item.Tag = text item.ExpandedChanged += AddressOf OnTreeViewItemExpandedChanged Return item End Function Private Function CreateCommandsGroupBox() As NGroupBox Dim commandsStack As NStackPanel = New NStackPanel() ' Create the command buttons m_AddButton = New NButton("Add Child Item") Me.m_AddButton.Click += AddressOf OnAddButtonClicked commandsStack.Add(m_AddButton) m_InsertBeforeButton = New NButton("Insert Item Before") Me.m_InsertBeforeButton.Click += AddressOf OnAddButtonClicked commandsStack.Add(m_InsertBeforeButton) m_InsertAfterButton = New NButton("Insert Item After") Me.m_InsertAfterButton.Click += AddressOf OnAddButtonClicked commandsStack.Add(m_InsertAfterButton) Dim expandAllButton As NButton = New NButton("Expand All") expandAllButton.Margins = New NMargins(0, 15, 0, 0) expandAllButton.Click += AddressOf OnExpandAllClicked commandsStack.Add(expandAllButton) Dim collapseAllButton As NButton = New NButton("Collapse All") collapseAllButton.Margins = New NMargins(0, 0, 0, 15) collapseAllButton.Click += AddressOf OnCollapseAllClicked commandsStack.Add(collapseAllButton) m_RemoveSelectedButton = New NButton("Remove Selected") Me.m_RemoveSelectedButton.Click += AddressOf OnRemoveSelectedButtonClicked commandsStack.Add(m_RemoveSelectedButton) m_RemoveChildrenButton = New NButton("Remove Children") Me.m_RemoveChildrenButton.Click += AddressOf OnRemoveChildrenButtonClicked commandsStack.Add(m_RemoveChildrenButton) m_RemoveAllButton = New NButton("Remove All") Me.m_RemoveAllButton.Click += AddressOf OnRemoveAllButtonClicked commandsStack.Add(m_RemoveAllButton) ' Create the commands group box Dim commmandsGroupBox As NGroupBox = New NGroupBox("Commands") commmandsGroupBox.Content = commandsStack UpdateButtonsState() Return commmandsGroupBox End Function Private Sub UpdateButtonsState() Dim selectedItem = m_TreeView.SelectedItem If selectedItem Is Nothing Then m_InsertBeforeButton.Enabled = False m_InsertAfterButton.Enabled = False m_RemoveSelectedButton.Enabled = False m_RemoveChildrenButton.Enabled = False Else m_InsertBeforeButton.Enabled = True m_InsertAfterButton.Enabled = True m_RemoveSelectedButton.Enabled = True m_RemoveChildrenButton.Enabled = selectedItem.Items.Count > 0 End If End Sub #End Region #Region "Event Handlers" Private Sub OnAddButtonClicked(args As NEventArgs) Dim selItem = m_TreeView.SelectedItem Dim text As String = If(selItem Is Nothing, "Item " & m_TreeView.Items.Count.ToString(), selItem.Tag.ToString()) If args.TargetNode Is m_AddButton Then If selItem Is Nothing Then ' Add the item as a last item in the tree view m_TreeView.Items.Add(CreateTreeViewItem(text)) Else ' Add the item as a last item in the selected item text += "." & selItem.Items.Count.ToString() selItem.Items.Add(CreateTreeViewItem(text)) End If ElseIf args.TargetNode Is m_InsertBeforeButton Then ' Insert the item before the selected one Dim items = CType(selItem.ParentNode, NTreeViewItemCollection) text += ".Before" items.Insert(selItem.IndexInParent, CreateTreeViewItem(text)) ElseIf args.TargetNode Is m_InsertAfterButton Then ' Insert the item after the selected one Dim items = CType(selItem.ParentNode, NTreeViewItemCollection) text += ".After" items.Insert(selItem.IndexInParent + 1, CreateTreeViewItem(text)) End If If m_TreeView.Items.Count = 1 Then m_RemoveAllButton.Enabled = True End If End Sub Private Sub OnRemoveSelectedButtonClicked(args As NEventArgs) Dim selectedItem = m_TreeView.SelectedItem If selectedItem Is Nothing Then Return selectedItem.ParentNode.RemoveChild(selectedItem) End Sub Private Sub OnRemoveChildrenButtonClicked(arg1 As NEventArgs) Dim selectedItem = m_TreeView.SelectedItem If selectedItem Is Nothing Then Return selectedItem.Items.RemoveAllChildren() m_RemoveChildrenButton.Enabled = False End Sub Private Sub OnRemoveAllButtonClicked(args As NEventArgs) m_TreeView.Items.Clear() m_RemoveAllButton.Enabled = False End Sub Private Sub OnExpandAllClicked(args As NEventArgs) Dim treeIterator As IEnumerator(Of NNode) = m_TreeView.GetSubtreeIterator(ENTreeTraversalOrder.DepthFirstPreOrder, NIsFilter(Of NNode, NTreeViewItem).Instance) Dim itemIterator As IEnumerator(Of NTreeViewItem) = New NAsEnumerator(Of NNode, NTreeViewItem)(treeIterator) While itemIterator.MoveNext() itemIterator.Current.Expanded = True End While End Sub Private Sub OnCollapseAllClicked(args As NEventArgs) Dim treeIterator As IEnumerator(Of NNode) = m_TreeView.GetSubtreeIterator(ENTreeTraversalOrder.DepthFirstPreOrder, NIsFilter(Of NNode, NTreeViewItem).Instance) Dim itemIterator As IEnumerator(Of NTreeViewItem) = New NAsEnumerator(Of NNode, NTreeViewItem)(treeIterator) While itemIterator.MoveNext() itemIterator.Current.Expanded = False End While End Sub Private Sub OnTreeViewSelectedPathChanged(args As NValueChangeEventArgs) UpdateButtonsState() Dim selectedItem = m_TreeView.SelectedItem If selectedItem IsNot Nothing Then m_EventsLog.LogEvent("Selected: " & selectedItem.Tag.ToString()) End If End Sub Private Sub OnTreeViewItemExpandedChanged(args As NValueChangeEventArgs) Dim item = CType(args.TargetNode, NTreeViewItem) If item.Expanded Then m_EventsLog.LogEvent("Expanded: " & item.Tag.ToString()) Else m_EventsLog.LogEvent("Collapsed: " & item.Tag.ToString()) End If End Sub #End Region #Region "Fields" Private m_AddButton As NButton Private m_InsertBeforeButton As NButton Private m_InsertAfterButton As NButton Private m_RemoveSelectedButton As NButton Private m_RemoveChildrenButton As NButton Private m_RemoveAllButton As NButton Private m_TreeView As NTreeView Private m_EventsLog As NExampleEventsLog #End Region #Region "Static Methods" #End Region #Region "Constants" #End Region #Region "Nested Types" #End Region End Class

Download a fully functional VB.NET (.vb) Items Manipulation Example for WinForms Solution

Related Documentation Links