Top Level Window Auto Sizing Example for Blazor

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

Top Level Window Auto Sizing Example for Blazor 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 Blazor
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 Blazor Solution

Related Documentation Links