Label 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.

Label Example for Blazor Description

This example demonstrates how to create and use labels. The controls on the right let you change the label's font, alignment, placement, etc.


Label Example

C# Label Example Code for Nevron UserInterface for Blazor
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 - NLabelExample";

			// 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()
		{
			m_Label = new NLabel();
			m_Label.SetBorder(1, NColor.Red);

            return m_Label;
		}
		public NWidget CreateExampleControls()
		{
			NStackPanel stack = new NStackPanel();
			
			m_SampleTextComboBox = new NComboBox();

			for (int i = 0; i < 2; i++)
			{
				m_SampleTextComboBox.Items.Add(new NComboBoxItem("Sample " + i.ToString()));
			}

			m_SampleTextComboBox.SelectedIndexChanged += OnSampleTextChanged;
			m_SampleTextComboBox.SelectedIndex = 0;

			stack.Add(new NLabel("Sample Text:"));
			stack.Add(m_SampleTextComboBox);

			// font families
			stack.Add(new NLabel("Font Family:"));
			m_FontFamiliesComboBox = new NComboBox();

			string[] fontFamilies = NApplication.FontService.InstalledFontsMap.FontFamilies;
			int selectedIndex = 0;

			for (int i = 0; i < fontFamilies.Length; i++)
			{
				m_FontFamiliesComboBox.Items.Add(new NComboBoxItem(fontFamilies[i]));

				if (fontFamilies[i] == NFontDescriptor.DefaultSansFamilyName)
				{
					selectedIndex = i;
				}
			}

			m_FontFamiliesComboBox.SelectedIndex = selectedIndex;
			m_FontFamiliesComboBox.SelectedIndexChanged += OnFontStyleChanged;
			stack.Add(m_FontFamiliesComboBox);

			// font sizes
			stack.Add(new NLabel("Font Size:"));
			m_FontSizeComboBox = new NComboBox();
			for (int i = 5; i < 72; i++)
			{
				m_FontSizeComboBox.Items.Add(new NComboBoxItem(i.ToString()));
			}

			m_FontSizeComboBox.SelectedIndex = 4;
			m_FontSizeComboBox.SelectedIndexChanged += OnFontStyleChanged;
			stack.Add(m_FontSizeComboBox);

			// add style controls
			m_BoldCheckBox = new NCheckBox();
			m_BoldCheckBox.Content = new NLabel("Bold");
			m_BoldCheckBox.CheckedChanged += OnFontStyleChanged;
			stack.Add(m_BoldCheckBox);

			m_ItalicCheckBox = new NCheckBox();
			m_ItalicCheckBox.Content = new NLabel("Italic");
			m_ItalicCheckBox.CheckedChanged += OnFontStyleChanged;
			stack.Add(m_ItalicCheckBox);

			m_UnderlineCheckBox = new NCheckBox();
			m_UnderlineCheckBox.Content = new NLabel("Underline");
			m_UnderlineCheckBox.CheckedChanged += OnFontStyleChanged;
			stack.Add(m_UnderlineCheckBox);

			m_StrikeThroughCheckBox = new NCheckBox();
			m_StrikeThroughCheckBox.Content = new NLabel("Strikethrough");
			m_StrikeThroughCheckBox.CheckedChanged += OnFontStyleChanged;
			stack.Add(m_StrikeThroughCheckBox); 

			// properties
			NList editors = NDesigner.GetDesigner(m_Label).CreateMemberEditors(
				m_Label,
				NLabel.EnabledProperty,
				NLabel.HorizontalPlacementProperty,
				NLabel.VerticalPlacementProperty,
				NLabel.TextAlignmentProperty,
				NLabel.TextWrapModeProperty);

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

			// make sure font style is updated
			OnFontStyleChanged(null);

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

This example demonstrates how to create and use labels. The controls on the right let you change the label's font, alignment, placement, etc.

"; } /// /// Gets the relevant documentation links for the example /// /// public (string Name, string Url)[] GetDocumentationUrls() { return new[] { ("Label", "https://www.nevron.com/docs/openvision/ui-label.html"), ("Text Widgets Overview", "https://www.nevron.com/docs/openvision/ui-text-widgets-overview.html"), }; } #endregion #region Implementation #endregion #region Event Handlers private void OnSampleTextChanged(NValueChangeEventArgs args) { switch (m_SampleTextComboBox.SelectedIndex) { case 0: m_Label.Text = "The quick brown fox jumps over the lazy dog."; break; case 1: m_Label.Text = "This is the first line of a multi line label.\nThis is the second line."; break; } } private void OnFontStyleChanged(NValueChangeEventArgs args) { string fontFamily = (m_FontFamiliesComboBox.SelectedItem.Content as NLabel).Text; int fontSize = Int32.Parse((m_FontSizeComboBox.SelectedItem.Content as NLabel).Text); ENFontStyle fontStyle = ENFontStyle.Regular; if (m_BoldCheckBox.Checked) { fontStyle |= ENFontStyle.Bold; } if (m_ItalicCheckBox.Checked) { fontStyle |= ENFontStyle.Italic; } if (m_UnderlineCheckBox.Checked) { fontStyle |= ENFontStyle.Underline; } if (m_StrikeThroughCheckBox.Checked) { fontStyle |= ENFontStyle.Strikethrough; } m_Label.Font = new NFont(fontFamily, fontSize, fontStyle, ENFontRasterizationMode.Antialiased); } #endregion #region Fields private NLabel m_Label; private NComboBox m_SampleTextComboBox; private NComboBox m_FontFamiliesComboBox; private NComboBox m_FontSizeComboBox; private NCheckBox m_BoldCheckBox; private NCheckBox m_ItalicCheckBox; private NCheckBox m_UnderlineCheckBox; private NCheckBox m_StrikeThroughCheckBox; #endregion #region Static Methods #endregion #region Constants #endregion #region Nested Types #endregion } }

Download a fully functional C# (.cs) Label Example for Blazor Solution

Related Documentation Links