Top Level Window Auto Sizing 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.

Top Level Window Auto Sizing Example for WinForms Description

This example demonstrates how to create auto sizable and auto centered windows with expressions.


Top Level Window Auto Sizing Example

C# Top Level Window Auto Sizing Example Code for Nevron UserInterface for WinForms
Language: C#

using Nevron.Nov;
using Nevron.Nov.Dom;
using Nevron.Nov.Graphics;
using Nevron.Nov.Layout;
using Nevron.Nov.UI;
using Nevron.Nov.Windows.Forms;
using System.Windows.Forms;

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

		public Form1()
		{
			InitializeComponent();

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

			// 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()
		{
			NStackPanel stack = new NStackPanel();
			
			// Create the example's content
			NButton openYAutoSizeWindowButton = new NButton("Open Y auto sizable Window...");
			openYAutoSizeWindowButton.HorizontalPlacement = ENHorizontalPlacement.Left;
			openYAutoSizeWindowButton.VerticalPlacement = ENVerticalPlacement.Top;
			openYAutoSizeWindowButton.Click += OnOpenYAutoSizeWindowButtonClick;
			stack.Add(openYAutoSizeWindowButton);

			NButton openXAutoSizeWindowButton = new NButton("Open X auto sizable Window...");
			openXAutoSizeWindowButton.HorizontalPlacement = ENHorizontalPlacement.Left;
			openXAutoSizeWindowButton.VerticalPlacement = ENVerticalPlacement.Top;
			openXAutoSizeWindowButton.Click += OnOpenXAutoSizeWindowButtonClick;
			stack.Add(openXAutoSizeWindowButton);

			NButton openAutoSizeWindowButton = new NButton("Open X and Y auto sizable and auto centered Window...");
			openAutoSizeWindowButton.HorizontalPlacement = ENHorizontalPlacement.Left;
			openAutoSizeWindowButton.VerticalPlacement = ENVerticalPlacement.Top;
			openAutoSizeWindowButton.Click += OnOpenAutoSizeWindowButtonClick;
			stack.Add(openAutoSizeWindowButton);

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

This example demonstrates how to create auto sizable and auto centered windows with expressions.

"; } /// /// Gets the relevant documentation links for the example /// /// public (string Name, string Url)[] GetDocumentationUrls() { return new[] { ("Top Level Windows", "https://www.nevron.com/docs/openvision/ui-top-level-windows.html"), ("Windows and the Desktop", "https://www.nevron.com/docs/openvision/ui-windows-and-the-desktop.html"), }; } #endregion #region Implementation #endregion #region Event Handlers private void OnOpenXAutoSizeWindowButtonClick(NEventArgs arg) { NTopLevelWindow window = new NTopLevelWindow(); window.Modal = true; // allow the user to resize the Y window dimension window.AllowYResize = true; // bind the window Width to the desired width of the window NBindingFx bindingFx = new NBindingFx(window, NTopLevelWindow.DesiredWidthProperty); bindingFx.Guard = true; window.SetFx(NTopLevelWindow.WidthProperty, bindingFx); // create a wrap flow panel with Y direction NWrapFlowPanel wrapPanel = new NWrapFlowPanel(); wrapPanel.Direction = ENHVDirection.TopToBottom; window.Content = wrapPanel; for (int i = 0; i < 10; i++) { wrapPanel.Add(new NButton("Button" + i)); } // open the window DisplayWindow.Windows.Add(window); window.Open(); } private void OnOpenYAutoSizeWindowButtonClick(NEventArgs arg) { NTopLevelWindow window = new NTopLevelWindow(); window.Modal = true; // allow the user to resize the X window dimension window.AllowXResize = true; // bind the window Height to the desired height of the window NBindingFx bindingFx = new NBindingFx(window, NTopLevelWindow.DesiredHeightProperty); bindingFx.Guard = true; window.SetFx(NTopLevelWindow.HeightProperty, bindingFx); // create a wrap flow panel (by default flows from left to right) NWrapFlowPanel wrapPanel = new NWrapFlowPanel(); window.Content = wrapPanel; for (int i = 0; i < 10; i++) { wrapPanel.Add(new NButton("Button" + i)); } // open the window DisplayWindow.Windows.Add(window); window.Open(); } private void OnOpenAutoSizeWindowButtonClick(NEventArgs arg) { NTopLevelWindow window = new NTopLevelWindow(); window.Modal = true; // open the window in the center of its parent, window.StartPosition = ENWindowStartPosition.CenterOwnerWindow; // implement auto width and height sizing { // bind the window Width to the DefaultWidth of the window NBindingFx widthBindingFx = new NBindingFx(window, NTopLevelWindow.DefaultWidthProperty); widthBindingFx.Guard = true; window.SetFx(NTopLevelWindow.WidthProperty, widthBindingFx); // bind the window Height to the DefaultHeight of the window NBindingFx heightBindingFx = new NBindingFx(window, NTopLevelWindow.DesiredHeightProperty); heightBindingFx.Guard = true; window.SetFx(NTopLevelWindow.HeightProperty, heightBindingFx); } // implement auto center { // scratch X and Y define the window center // they are implemented by simply calculating the center X and Y via formulas window.SetFx(NScratchPropertyEx.XPropertyEx, "X+Width/2"); window.SetFx(NScratchPropertyEx.YPropertyEx, "Y+Height/2"); } // create a dummy tab that sizes to the currently selected page, // and add two pages with different sizes to the tab. NTab tab = new NTab(); window.Content = tab; tab.SizeToSelectedPage = true; NTabPage page1 = new NTabPage("Small Content"); NButton btn = new NButton("I am small"); page1.Content = btn; tab.TabPages.Add(page1); NTabPage page2 = new NTabPage("Large Content"); NButton btn2 = new NButton("I am LARGE"); btn2.PreferredSize = new NSize(200, 200); page2.Content = btn2; tab.TabPages.Add(page2); // open the window DisplayWindow.Windows.Add(window); window.Open(); } #endregion #region Fields #endregion #region Static Methods #endregion #region Constants #endregion #region Nested Types #endregion } }

Download a fully functional C# (.cs) Top Level Window Auto Sizing Example for WinForms Solution

VB.NET Top Level Window Auto Sizing Example Code for Nevron UserInterface for WinForms
Language: VB.NET

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

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

        ' 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 stack As NStackPanel = New NStackPanel()

            ' Create the example's content
            Dim openYAutoSizeWindowButton As NButton = New NButton("Open Y auto sizable Window...")
            openYAutoSizeWindowButton.HorizontalPlacement = ENHorizontalPlacement.Left
            openYAutoSizeWindowButton.VerticalPlacement = ENVerticalPlacement.Top
            openYAutoSizeWindowButton.Click += AddressOf OnOpenYAutoSizeWindowButtonClick
            stack.Add(openYAutoSizeWindowButton)

            Dim openXAutoSizeWindowButton As NButton = New NButton("Open X auto sizable Window...")
            openXAutoSizeWindowButton.HorizontalPlacement = ENHorizontalPlacement.Left
            openXAutoSizeWindowButton.VerticalPlacement = ENVerticalPlacement.Top
            openXAutoSizeWindowButton.Click += AddressOf OnOpenXAutoSizeWindowButtonClick
            stack.Add(openXAutoSizeWindowButton)

            Dim openAutoSizeWindowButton As NButton = New NButton("Open X and Y auto sizable and auto centered Window...")
            openAutoSizeWindowButton.HorizontalPlacement = ENHorizontalPlacement.Left
            openAutoSizeWindowButton.VerticalPlacement = ENVerticalPlacement.Top
            openAutoSizeWindowButton.Click += AddressOf OnOpenAutoSizeWindowButtonClick
            stack.Add(openAutoSizeWindowButton)

            Return stack
        End Function
        Public Function CreateExampleControls() As NWidget
            Return Nothing
        End Function
        Public Function GetExampleDescription() As String
            Return "

This example demonstrates how to create auto sizable and auto centered windows with expressions.

" End Function ''' ''' Gets the relevant documentation links for the example ''' ''' Public Function GetDocumentationUrls() As (String, String)() Return {("Top Level Windows", "https://www.nevron.com/docs/openvision/ui-top-level-windows.html"), ("Windows and the Desktop", "https://www.nevron.com/docs/openvision/ui-windows-and-the-desktop.html")} End Function #End Region #Region "Implementation" #End Region #Region "Event Handlers" Private Sub OnOpenXAutoSizeWindowButtonClick(arg As NEventArgs) Dim window As NTopLevelWindow = New NTopLevelWindow() window.Modal = True ' allow the user to resize the Y window dimension window.AllowYResize = True ' bind the window Width to the desired width of the window Dim bindingFx As NBindingFx = New NBindingFx(window, NBoxElement.DesiredWidthProperty) bindingFx.Guard = True window.SetFx(NBoxElement.WidthProperty, bindingFx) ' create a wrap flow panel with Y direction Dim wrapPanel As NWrapFlowPanel = New NWrapFlowPanel() wrapPanel.Direction = ENHVDirection.TopToBottom window.Content = wrapPanel For i = 0 To 9 wrapPanel.Add(New NButton("Button" & i.ToString())) Next ' open the window DisplayWindow.Windows.Add(window) window.Open() End Sub Private Sub OnOpenYAutoSizeWindowButtonClick(arg As NEventArgs) Dim window As NTopLevelWindow = New NTopLevelWindow() window.Modal = True ' allow the user to resize the X window dimension window.AllowXResize = True ' bind the window Height to the desired height of the window Dim bindingFx As NBindingFx = New NBindingFx(window, NBoxElement.DesiredHeightProperty) bindingFx.Guard = True window.SetFx(NBoxElement.HeightProperty, bindingFx) ' create a wrap flow panel (by default flows from left to right) Dim wrapPanel As NWrapFlowPanel = New NWrapFlowPanel() window.Content = wrapPanel For i = 0 To 9 wrapPanel.Add(New NButton("Button" & i.ToString())) Next ' open the window DisplayWindow.Windows.Add(window) window.Open() End Sub Private Sub OnOpenAutoSizeWindowButtonClick(arg As NEventArgs) Dim window As NTopLevelWindow = New NTopLevelWindow() window.Modal = True ' open the window in the center of its parent, window.StartPosition = ENWindowStartPosition.CenterOwnerWindow ' implement auto width and height sizing If True Then ' bind the window Width to the DefaultWidth of the window Dim widthBindingFx As NBindingFx = New NBindingFx(window, NBoxElement.DefaultWidthProperty) widthBindingFx.Guard = True window.SetFx(NBoxElement.WidthProperty, widthBindingFx) ' bind the window Height to the DefaultHeight of the window Dim heightBindingFx As NBindingFx = New NBindingFx(window, NBoxElement.DesiredHeightProperty) heightBindingFx.Guard = True window.SetFx(NBoxElement.HeightProperty, heightBindingFx) End If ' implement auto center If True Then ' scratch X and Y define the window center ' they are implemented by simply calculating the center X and Y via formulas window.SetFx(NScratchPropertyEx.XPropertyEx, "X+Width/2") window.SetFx(NScratchPropertyEx.YPropertyEx, "Y+Height/2") End If ' create a dummy tab that sizes to the currently selected page, ' and add two pages with different sizes to the tab. Dim tab As NTab = New NTab() window.Content = tab tab.SizeToSelectedPage = True Dim page1 As NTabPage = New NTabPage("Small Content") Dim btn As NButton = New NButton("I am small") page1.Content = btn tab.TabPages.Add(page1) Dim page2 As NTabPage = New NTabPage("Large Content") Dim btn2 As NButton = New NButton("I am LARGE") btn2.PreferredSize = New NSize(200, 200) page2.Content = btn2 tab.TabPages.Add(page2) ' open the window DisplayWindow.Windows.Add(window) window.Open() End Sub #End Region #Region "Fields" #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) Top Level Window Auto Sizing Example for WinForms Solution

Related Documentation Links