Flex Box 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.

Flex Box Panel Example for WinForms Description

This example demonstrates how to create a flexbox layout panel and add widgets to it. You can control the parameters of the layout algorithm using the controls on the right. The Grow and Shrink factors of the widgets determine the portion of area to use for growing/shrinking. They are by default set to 0, which means that the widget will not grow or shrink.

To test how the Grow and Shrink factors affect the layout, click the Remove All Items button on the right, then set Grow and Shrink factor and click any of the Add ... Item buttons. Then update the Grow and Shrink factor if you want, click any of the Add ... Item buttons again and so on.


Flex Box Panel Example

C# Flex Box 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 - NFlexBoxPanelExample";

			// 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()
		{
			NMargins labelPadding = new NMargins(2);

			m_FlexBoxPanel = new NFlexBoxPanel();
			m_FlexBoxPanel.HorizontalPlacement = ENHorizontalPlacement.Left;
			m_FlexBoxPanel.VerticalPlacement = ENVerticalPlacement.Top;
			m_FlexBoxPanel.PreferredHeight = 200;

			// Set the Grow and Shrink extended properties of the first label explicitly
			NLabel label1 = new NLabel("Label 1 - Grow: 1, Shrink: 1");
			label1.BackgroundFill = new NColorFill(NColor.Gold);
			label1.Padding = labelPadding;
			NFlexBoxLayout.SetGrow(label1, 1);
			NFlexBoxLayout.SetShrink(label1, 1);
			m_FlexBoxPanel.Add(label1);

			// Pass the values of the Grow and Shrink extended properties of the second label
			// to the Add method of the panel
			NLabel label2 = new NLabel("Label 2 - Grow: 3, Shrink: 3");
			label2.BackgroundFill = new NColorFill(NColor.Orange);
			label2.Padding = labelPadding;
			m_FlexBoxPanel.Add(label2, 3, 3);

			// The third label will have the default values for Grow and Shrink - 0
			NLabel label3 = new NLabel("Label 3 - Grow: 0, Shrink: 0");
			label3.BackgroundFill = new NColorFill(NColor.Red);
			label3.Padding = labelPadding;
			m_FlexBoxPanel.Add(label3);

			return m_FlexBoxPanel;
		}
		public NWidget CreateExampleControls()
		{
			NStackPanel stack = new NStackPanel();
			
			// properties stack
			NList editors = NDesigner.GetDesigner(m_FlexBoxPanel).CreateMemberEditors(
				m_FlexBoxPanel,
				NFlexBoxPanel.EnabledProperty,
				NFlexBoxPanel.HorizontalPlacementProperty,
				NFlexBoxPanel.VerticalPlacementProperty,
				NFlexBoxPanel.DirectionProperty,
				NFlexBoxPanel.VerticalSpacingProperty,
				NFlexBoxPanel.HorizontalSpacingProperty,
				NFlexBoxPanel.UniformWidthsProperty,
				NFlexBoxPanel.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_GrowUpDown = new NNumericUpDown(0, 100, 0);
			itemsStack.Add(NPairBox.Create("Grow Factor: ", m_GrowUpDown));

			m_ShrinkUpDown = new NNumericUpDown(0, 100, 0);
			itemsStack.Add(NPairBox.Create("Shrink Factor: ", m_ShrinkUpDown));

			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", new NUniSizeBoxGroup(itemsStack)));

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

This example demonstrates how to create a flexbox layout panel and add widgets to it. You can control the parameters of the layout algorithm using the controls on the right. The Grow and Shrink factors of the widgets determine the portion of area to use for growing/shrinking. They are by default set to 0, which means that the widget will not grow or shrink.

To test how the Grow and Shrink factors affect the layout, click the Remove All Items button on the right, then set Grow and Shrink factor and click any of the Add ... Item buttons. Then update the Grow and Shrink factor if you want, click any of the Add ... Item buttons again and so on.

"; } /// /// Gets the relevant documentation links for the example /// /// public (string Name, string Url)[] GetDocumentationUrls() { return new[] { ("Flex Box Panel", "https://www.nevron.com/docs/openvision/ui-flex-box-panel.html"), ("Panels Overview", "https://www.nevron.com/docs/openvision/ui-panels-overview.html"), }; } #endregion #region Implementation private void SetGrowAndShrink(NWidget widget) { double grow = m_GrowUpDown.Value; if (grow != 0) { NFlexBoxLayout.SetGrow(widget, grow); } double shrink = m_ShrinkUpDown.Value; if (shrink != 0) { NFlexBoxLayout.SetShrink(widget, shrink); } } #endregion #region Event Handlers private void OnAddSmallItemButtonClick(NEventArgs args) { NButton item = new NButton("Small " + m_FlexBoxPanel.Count.ToString()); item.MinSize = new NSize(5, 5); item.PreferredSize = new NSize(25, 25); SetGrowAndShrink(item); m_FlexBoxPanel.Add(item); } private void OnAddLargeItemButtonClick(NEventArgs args) { NButton item = new NButton("Large " + m_FlexBoxPanel.Count.ToString()); item.MinSize = new NSize(20, 20); item.PreferredSize = new NSize(60, 60); SetGrowAndShrink(item); m_FlexBoxPanel.Add(item); } private void OnAddRandomItemButtonClick(NEventArgs args) { int range = 30; Random rnd = new Random(); NButton item = new NButton("Random " + m_FlexBoxPanel.Count.ToString()); item.MinSize = new NSize(rnd.Next(range), rnd.Next(range)); item.PreferredSize = new NSize(rnd.Next(range) + range, rnd.Next(range) + range); SetGrowAndShrink(item); m_FlexBoxPanel.Add(item); } private void OnRemoveAllItemsButtonClick(NEventArgs args) { m_FlexBoxPanel.Clear(); } #endregion #region Fields private NFlexBoxPanel m_FlexBoxPanel; private NNumericUpDown m_GrowUpDown; private NNumericUpDown m_ShrinkUpDown; #endregion #region Static Methods #endregion #region Constants #endregion #region Nested Types #endregion } }

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

VB.NET Flex Box 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 - NFlexBoxPanelExample"

        ' 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
            Dim labelPadding As NMargins = New NMargins(2)

            m_FlexBoxPanel = New NFlexBoxPanel()
            m_FlexBoxPanel.HorizontalPlacement = ENHorizontalPlacement.Left
            m_FlexBoxPanel.VerticalPlacement = ENVerticalPlacement.Top
            m_FlexBoxPanel.PreferredHeight = 200

            ' Set the Grow and Shrink extended properties of the first label explicitly
            Dim label1 As NLabel = New NLabel("Label 1 - Grow: 1, Shrink: 1")
            label1.BackgroundFill = New NColorFill(NColor.Gold)
            label1.Padding = labelPadding
            NFlexBoxLayout.SetGrow(label1, 1)
            NFlexBoxLayout.SetShrink(label1, 1)
            m_FlexBoxPanel.Add(label1)

            ' Pass the values of the Grow and Shrink extended properties of the second label
            ' to the Add method of the panel
            Dim label2 As NLabel = New NLabel("Label 2 - Grow: 3, Shrink: 3")
            label2.BackgroundFill = New NColorFill(NColor.Orange)
            label2.Padding = labelPadding
            m_FlexBoxPanel.Add(label2, 3, 3)

            ' The third label will have the default values for Grow and Shrink - 0
            Dim label3 As NLabel = New NLabel("Label 3 - Grow: 0, Shrink: 0")
            label3.BackgroundFill = New NColorFill(NColor.Red)
            label3.Padding = labelPadding
            m_FlexBoxPanel.Add(label3)

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

            ' properties stack
            Dim editors = NDesigner.GetDesigner(m_FlexBoxPanel).CreateMemberEditors(m_FlexBoxPanel, NInputElement.EnabledProperty, NWidget.HorizontalPlacementProperty, NWidget.VerticalPlacementProperty, NFlexBoxPanel.DirectionProperty, NFlexBoxPanel.VerticalSpacingProperty, NFlexBoxPanel.HorizontalSpacingProperty, NFlexBoxPanel.UniformWidthsProperty, NFlexBoxPanel.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_GrowUpDown = New NNumericUpDown(0, 100, 0)
            itemsStack.Add(NPairBox.Create("Grow Factor: ", m_GrowUpDown))

            m_ShrinkUpDown = New NNumericUpDown(0, 100, 0)
            itemsStack.Add(NPairBox.Create("Shrink Factor: ", m_ShrinkUpDown))

            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", New NUniSizeBoxGroup(itemsStack)))

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

This example demonstrates how to create a flexbox layout panel and add widgets to it. You can control the parameters of the layout algorithm using the controls on the right. The Grow and Shrink factors of the widgets determine the portion of area to use for growing/shrinking. They are by default set to 0, which means that the widget will not grow or shrink.

To test how the Grow and Shrink factors affect the layout, click the Remove All Items button on the right, then set Grow and Shrink factor and click any of the Add ... Item buttons. Then update the Grow and Shrink factor if you want, click any of the Add ... Item buttons again and so on.

" End Function ''' ''' Gets the relevant documentation links for the example ''' ''' Public Function GetDocumentationUrls() As (String, String)() Return {("Flex Box Panel", "https://www.nevron.com/docs/openvision/ui-flex-box-panel.html"), ("Panels Overview", "https://www.nevron.com/docs/openvision/ui-panels-overview.html")} End Function #End Region #Region "Implementation" Private Sub SetGrowAndShrink(widget As NWidget) Dim grow = m_GrowUpDown.Value If grow <> 0 Then NFlexBoxLayout.SetGrow(widget, grow) End If Dim shrink = m_ShrinkUpDown.Value If shrink <> 0 Then NFlexBoxLayout.SetShrink(widget, shrink) End If End Sub #End Region #Region "Event Handlers" Private Sub OnAddSmallItemButtonClick(args As NEventArgs) Dim item As NButton = New NButton("Small " & m_FlexBoxPanel.Count.ToString()) item.MinSize = New NSize(5, 5) item.PreferredSize = New NSize(25, 25) SetGrowAndShrink(item) m_FlexBoxPanel.Add(item) End Sub Private Sub OnAddLargeItemButtonClick(args As NEventArgs) Dim item As NButton = New NButton("Large " & m_FlexBoxPanel.Count.ToString()) item.MinSize = New NSize(20, 20) item.PreferredSize = New NSize(60, 60) SetGrowAndShrink(item) m_FlexBoxPanel.Add(item) End Sub Private Sub OnAddRandomItemButtonClick(args As NEventArgs) Dim range = 30 Dim rnd As Random = New Random() Dim item As NButton = New NButton("Random " & m_FlexBoxPanel.Count.ToString()) item.MinSize = New NSize(rnd.Next(range), rnd.Next(range)) item.PreferredSize = New NSize(rnd.Next(range) + range, rnd.Next(range) + range) SetGrowAndShrink(item) m_FlexBoxPanel.Add(item) End Sub Private Sub OnRemoveAllItemsButtonClick(args As NEventArgs) m_FlexBoxPanel.Clear() End Sub #End Region #Region "Fields" Private m_FlexBoxPanel As NFlexBoxPanel Private m_GrowUpDown As NNumericUpDown Private m_ShrinkUpDown As NNumericUpDown #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) Flex Box Panel Example for WinForms Solution

Related Documentation Links