Dock Panel 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.

Dock Panel Example for WinForms Description

This example demonstrates how to create a dock layout panel and add widgets to it specifying the dock area each widget should be placed in.


Dock Panel Example

C# Dock Panel 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.Presentation;
using Nevron.Nov.UI;
using Nevron.Nov.Windows.Forms;
using System;
using System.Windows.Forms;

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

		public Form1()
		{
			InitializeComponent();

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

			// 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 a dock panel with red border
			m_DockPanel = new NDockPanel();
			m_DockPanel.SetBorder(1, NColor.Red);

            // Create and dock several widgets
            NWidget widget = CreateDockedWidget(ENDockArea.Left);
			widget.PreferredSize = new NSize(100, 100);
			m_DockPanel.Add(widget);

			widget = CreateDockedWidget(ENDockArea.Top);
			widget.PreferredSize = new NSize(100, 100);
			m_DockPanel.Add(widget);

			widget = CreateDockedWidget(ENDockArea.Right);
			widget.PreferredSize = new NSize(100, 100);
			m_DockPanel.Add(widget);

			widget = CreateDockedWidget(ENDockArea.Bottom);
			widget.PreferredSize = new NSize(100, 100);
			m_DockPanel.Add(widget);

			widget = CreateDockedWidget(ENDockArea.Center);
			widget.PreferredSize = new NSize(300, 300);
			m_DockPanel.Add(widget);

			return m_DockPanel;
		}
		public NWidget CreateExampleControls()
		{
			NStackPanel stack = new NStackPanel();
			
			// properties stack
			NList editors = NDesigner.GetDesigner(m_DockPanel).CreateMemberEditors(
				m_DockPanel,
				NDockPanel.EnabledProperty,
				NDockPanel.HorizontalPlacementProperty,
				NDockPanel.VerticalPlacementProperty,
				NDockPanel.VerticalSpacingProperty,
				NDockPanel.HorizontalSpacingProperty,
				NDockPanel.UniformWidthsProperty,
				NDockPanel.UniformHeightsProperty);

			NStackPanel propertiesStack = new NStackPanel();
			for (int i = 0; i < editors.Count; i++)
			{
				propertiesStack.Add(editors[i]);
			}

			stack.Add(new NGroupBox("Properties", new NUniSizeBoxGroup(propertiesStack)));

			// items stack
			NStackPanel itemsStack = new NStackPanel();
			m_DockAreaCombo = new NComboBox();
			m_DockAreaCombo.Items.Add(new NComboBoxItem("Left"));
			m_DockAreaCombo.Items.Add(new NComboBoxItem("Top"));
			m_DockAreaCombo.Items.Add(new NComboBoxItem("Right"));
			m_DockAreaCombo.Items.Add(new NComboBoxItem("Bottom"));
			m_DockAreaCombo.Items.Add(new NComboBoxItem("Center"));
			m_DockAreaCombo.SelectedIndex = 1;
			NLabel dockAreaLabel = new NLabel("Dock Area:");
			dockAreaLabel.VerticalPlacement = ENVerticalPlacement.Center;
			itemsStack.Add(new NPairBox(dockAreaLabel, m_DockAreaCombo, true));

			NButton addSmallItemButton = new NButton("Add Small Item");
			addSmallItemButton.Click += OnAddSmallItemButtonClick;
			itemsStack.Add(addSmallItemButton);

			NButton addLargeItemButton = new NButton("Add Large Item");
			addLargeItemButton.Click += OnAddLargeItemButtonClick;
			itemsStack.Add(addLargeItemButton);

			NButton addRandomItemButton = new NButton("Add Random Item");
			addRandomItemButton.Click += OnAddRandomItemButtonClick;
			itemsStack.Add(addRandomItemButton);

			NButton removeAllItemsButton = new NButton("Remove All Items");
			removeAllItemsButton.Click += OnRemoveAllItemsButtonClick;
			itemsStack.Add(removeAllItemsButton);

			stack.Add(new NGroupBox("Items", itemsStack));
			return new NUniSizeBoxGroup(stack);
		}
		public string GetExampleDescription()
		{
			return @"

This example demonstrates how to create a dock layout panel and add widgets to it specifying the dock area each widget should be placed in.

"; } /// /// Gets the relevant documentation links for the example /// /// public (string Name, string Url)[] GetDocumentationUrls() { return new[] { ("ui-dock-panel", "https://www.nevron.com/docs/openvision/ui-dock-panel.html"), ("Panels Overview", "https://www.nevron.com/docs/openvision/ui-panels-overview.html"), }; } #endregion #region Implementation /// /// Gets the currently selected dock area from the DockAreaCombo. /// /// private ENDockArea GetCurrentDockArea() { switch (m_DockAreaCombo.SelectedIndex) { case 0: return ENDockArea.Left; case 1: return ENDockArea.Top; case 2: return ENDockArea.Right; case 3: return ENDockArea.Bottom; case 4: return ENDockArea.Center; default: return ENDockArea.Top; } } /// /// Sets the currently selected dock area to the specified widget. /// /// private void SetCurrentDockArea(NWidget widget) { SetDockArea(widget, GetCurrentDockArea()); } /// /// Sets a custom dock area to the specified widget and colors its background accordingly /// /// /// private void SetDockArea(NWidget widget, ENDockArea dockArea) { NDockLayout.SetDockArea(widget, dockArea); switch (dockArea) { case ENDockArea.Bottom: widget.BackgroundFill = new NColorFill(new NColor(0, 162, 232)); break; case ENDockArea.Center: widget.BackgroundFill = new NColorFill(new NColor(239, 228, 176)); break; case ENDockArea.Left: widget.BackgroundFill = new NColorFill(new NColor(34, 177, 76)); break; case ENDockArea.Right: widget.BackgroundFill = new NColorFill(new NColor(163, 73, 164)); break; case ENDockArea.Top: widget.BackgroundFill = new NColorFill(new NColor(237, 28, 36)); break; default: throw new Exception("New ENDockArea?"); } } /// /// Creates the default docked widget for this example, that is docked to the current dock area. /// /// private NWidget CreateDockedWidget() { return CreateDockedWidget(GetCurrentDockArea()); } /// /// Creates the default docked widget for this example, that is docked to the specified area. /// /// /// private NWidget CreateDockedWidget(ENDockArea dockArea) { NLabel label = new NLabel(dockArea.ToString() + "(" + m_DockPanel.Count.ToString() + ")"); label.HorizontalPlacement = ENHorizontalPlacement.Center; label.VerticalPlacement = ENVerticalPlacement.Center; NWidget widget = new NContentHolder(label); widget.Border = NBorder.CreateFilledBorder(NColor.Black); widget.BorderThickness = new NMargins(1); SetDockArea(widget, dockArea); return widget; } #endregion #region Event Handlers private void OnAddSmallItemButtonClick(NEventArgs args) { NWidget item = CreateDockedWidget(); item.MinSize = new NSize(20, 20); item.PreferredSize = new NSize(60, 60); m_DockPanel.Add(item); } private void OnAddLargeItemButtonClick(NEventArgs args) { NWidget item = CreateDockedWidget(); item.MinSize = new NSize(40, 40); item.PreferredSize = new NSize(100, 100); m_DockPanel.Add(item); } private void OnAddRandomItemButtonClick(NEventArgs args) { int range = 50; Random rnd = new Random(); NWidget item = CreateDockedWidget(); item.MinSize = new NSize(rnd.Next(range), rnd.Next(range)); item.PreferredSize = new NSize(rnd.Next(range) + range, rnd.Next(range) + range); m_DockPanel.Add(item); } private void OnRemoveAllItemsButtonClick(NEventArgs args) { m_DockPanel.Clear(); } #endregion #region Fields private NComboBox m_DockAreaCombo; private NDockPanel m_DockPanel; #endregion #region Static Methods #endregion #region Constants #endregion #region Nested Types #endregion } }

Download a fully functional C# (.cs) Dock Panel Example for WinForms Solution

VB.NET Dock Panel Example Code for Nevron UserInterface for WinForms
Language: VB.NET

Imports Nevron.Nov
Imports Nevron.Nov.Dom
Imports Nevron.Nov.Editors
Imports Nevron.Nov.Graphics
Imports Nevron.Nov.Layout
Imports Nevron.Nov.UI
Imports Nevron.Nov.Windows.Forms
Imports System

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 - NDockPanelExample"

        ' 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 a dock panel with red border
            m_DockPanel = New NDockPanel()
            m_DockPanel.SetBorder(1, NColor.Red)

            ' Create and dock several widgets
            Dim widget = CreateDockedWidget(ENDockArea.Left)
            widget.PreferredSize = New NSize(100, 100)
            m_DockPanel.Add(widget)

            widget = CreateDockedWidget(ENDockArea.Top)
            widget.PreferredSize = New NSize(100, 100)
            m_DockPanel.Add(widget)

            widget = CreateDockedWidget(ENDockArea.Right)
            widget.PreferredSize = New NSize(100, 100)
            m_DockPanel.Add(widget)

            widget = CreateDockedWidget(ENDockArea.Bottom)
            widget.PreferredSize = New NSize(100, 100)
            m_DockPanel.Add(widget)

            widget = CreateDockedWidget(ENDockArea.Center)
            widget.PreferredSize = New NSize(300, 300)
            m_DockPanel.Add(widget)

            Return m_DockPanel
        End Function
        Public Function CreateExampleControls() As NWidget
            Dim stack As NStackPanel = New NStackPanel()

            ' properties stack
            Dim editors = NDesigner.GetDesigner(m_DockPanel).CreateMemberEditors(m_DockPanel, NInputElement.EnabledProperty, NWidget.HorizontalPlacementProperty, NWidget.VerticalPlacementProperty, NDockPanel.VerticalSpacingProperty, NDockPanel.HorizontalSpacingProperty, NDockPanel.UniformWidthsProperty, NDockPanel.UniformHeightsProperty)

            Dim propertiesStack As NStackPanel = New NStackPanel()
            For i = 0 To editors.Count - 1
                propertiesStack.Add(editors(i))
            Next

            stack.Add(New NGroupBox("Properties", New NUniSizeBoxGroup(propertiesStack)))

            ' items stack
            Dim itemsStack As NStackPanel = New NStackPanel()
            m_DockAreaCombo = New NComboBox()
            m_DockAreaCombo.Items.Add(New NComboBoxItem("Left"))
            m_DockAreaCombo.Items.Add(New NComboBoxItem("Top"))
            m_DockAreaCombo.Items.Add(New NComboBoxItem("Right"))
            m_DockAreaCombo.Items.Add(New NComboBoxItem("Bottom"))
            m_DockAreaCombo.Items.Add(New NComboBoxItem("Center"))
            m_DockAreaCombo.SelectedIndex = 1
            Dim dockAreaLabel As NLabel = New NLabel("Dock Area:")
            dockAreaLabel.VerticalPlacement = ENVerticalPlacement.Center
            itemsStack.Add(New NPairBox(dockAreaLabel, m_DockAreaCombo, True))

            Dim addSmallItemButton As NButton = New NButton("Add Small Item")
            addSmallItemButton.Click += AddressOf OnAddSmallItemButtonClick
            itemsStack.Add(addSmallItemButton)

            Dim addLargeItemButton As NButton = New NButton("Add Large Item")
            addLargeItemButton.Click += AddressOf OnAddLargeItemButtonClick
            itemsStack.Add(addLargeItemButton)

            Dim addRandomItemButton As NButton = New NButton("Add Random Item")
            addRandomItemButton.Click += AddressOf OnAddRandomItemButtonClick
            itemsStack.Add(addRandomItemButton)

            Dim removeAllItemsButton As NButton = New NButton("Remove All Items")
            removeAllItemsButton.Click += AddressOf OnRemoveAllItemsButtonClick
            itemsStack.Add(removeAllItemsButton)

            stack.Add(New NGroupBox("Items", itemsStack))
            Return New NUniSizeBoxGroup(stack)
        End Function
        Public Function GetExampleDescription() As String
            Return "

This example demonstrates how to create a dock layout panel and add widgets to it specifying the dock area each widget should be placed in.

" End Function ''' ''' Gets the relevant documentation links for the example ''' ''' Public Function GetDocumentationUrls() As (String, String)() Return {("ui-dock-panel", "https://www.nevron.com/docs/openvision/ui-dock-panel.html"), ("Panels Overview", "https://www.nevron.com/docs/openvision/ui-panels-overview.html")} End Function #End Region #Region "Implementation" ''' ''' Gets the currently selected dock area from the DockAreaCombo. ''' ''' Private Function GetCurrentDockArea() As ENDockArea Select Case m_DockAreaCombo.SelectedIndex Case 0 Return ENDockArea.Left Case 1 Return ENDockArea.Top Case 2 Return ENDockArea.Right Case 3 Return ENDockArea.Bottom Case 4 Return ENDockArea.Center Case Else Return ENDockArea.Top End Select End Function ''' ''' Sets the currently selected dock area to the specified widget. ''' ''' Private Sub SetCurrentDockArea(widget As NWidget) SetDockArea(widget, GetCurrentDockArea()) End Sub ''' ''' Sets a custom dock area to the specified widget and colors its background accordingly ''' ''' ''' Private Sub SetDockArea(widget As NWidget, dockArea As ENDockArea) NDockLayout.SetDockArea(widget, dockArea) Select Case dockArea Case ENDockArea.Bottom widget.BackgroundFill = New NColorFill(New NColor(0, 162, 232)) Case ENDockArea.Center widget.BackgroundFill = New NColorFill(New NColor(239, 228, 176)) Case ENDockArea.Left widget.BackgroundFill = New NColorFill(New NColor(34, 177, 76)) Case ENDockArea.Right widget.BackgroundFill = New NColorFill(New NColor(163, 73, 164)) Case ENDockArea.Top widget.BackgroundFill = New NColorFill(New NColor(237, 28, 36)) Case Else Throw New Exception("New ENDockArea?") End Select End Sub ''' ''' Creates the default docked widget for this example, that is docked to the current dock area. ''' ''' Private Function CreateDockedWidget() As NWidget Return CreateDockedWidget(GetCurrentDockArea()) End Function ''' ''' Creates the default docked widget for this example, that is docked to the specified area. ''' ''' ''' Private Function CreateDockedWidget(dockArea As ENDockArea) As NWidget Dim label As NLabel = New NLabel(dockArea.ToString() & "(" & m_DockPanel.Count.ToString() & ")") label.HorizontalPlacement = ENHorizontalPlacement.Center label.VerticalPlacement = ENVerticalPlacement.Center Dim widget As NWidget = New NContentHolder(label) widget.Border = NBorder.CreateFilledBorder(NColor.Black) widget.BorderThickness = New NMargins(1) SetDockArea(widget, dockArea) Return widget End Function #End Region #Region "Event Handlers" Private Sub OnAddSmallItemButtonClick(args As NEventArgs) Dim item As NWidget = CreateDockedWidget() item.MinSize = New NSize(20, 20) item.PreferredSize = New NSize(60, 60) m_DockPanel.Add(item) End Sub Private Sub OnAddLargeItemButtonClick(args As NEventArgs) Dim item As NWidget = CreateDockedWidget() item.MinSize = New NSize(40, 40) item.PreferredSize = New NSize(100, 100) m_DockPanel.Add(item) End Sub Private Sub OnAddRandomItemButtonClick(args As NEventArgs) Dim range = 50 Dim rnd As Random = New Random() Dim item As NWidget = CreateDockedWidget() item.MinSize = New NSize(rnd.Next(range), rnd.Next(range)) item.PreferredSize = New NSize(rnd.Next(range) + range, rnd.Next(range) + range) m_DockPanel.Add(item) End Sub Private Sub OnRemoveAllItemsButtonClick(args As NEventArgs) m_DockPanel.Clear() End Sub #End Region #Region "Fields" Private m_DockAreaCombo As NComboBox Private m_DockPanel As NDockPanel #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) Dock Panel Example for WinForms Solution

Related Documentation Links