Selection Events Example for WinForms

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

Selection Events Example for WinForms Description

The example demonstrates how to track the selection changed event as well as how to query the selection object.


Selection Changed Example

C# Selection Events Example Code for Nevron RichTextEditor for WinForms
Language: C#

using Nevron.Nov;
using Nevron.Nov.DataStructures;
using Nevron.Nov.Dom;
using Nevron.Nov.Graphics;
using Nevron.Nov.Layout;
using Nevron.Nov.Text;
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 - NSelectionChangedExample";

			// 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 the rich text
			m_RichText = new NRichTextView();
			m_RichText.AcceptsTab = true;
			m_RichText.Content.Sections.Clear();

			NSection section = new NSection();
			section.Blocks.Add(new NParagraph("The example demonstrates how to track the selection changed event."));
			section.Blocks.Add(new NParagraph("It also shows how to identify different text elements depending on the current selection."));
			section.Blocks.Add(new NParagraph("Move the selection and the control will highlight the currently selected words as well as blocks inside the text document."));


			NParagraph paragraph = new NParagraph();
			paragraph.Inlines.Add(new NTextInline("You can also detect the inline elements "));

			NTextInline inline1 = new NTextInline("that have different");
			paragraph.Inlines.Add(inline1);

			NTextInline inline2 = new NTextInline(" Font Style");
			inline2.FontStyle = ENFontStyle.Bold;
			paragraph.Inlines.Add(inline2);

			NTextInline inline3 = new NTextInline(" Font Size");
			inline3.FontSize = 14;
			paragraph.Inlines.Add(inline3);

			NTextInline inline4 = new NTextInline(" and / or other attributes");
			inline4.FontStyle = ENFontStyle.Italic | ENFontStyle.Underline;
			paragraph.Inlines.Add(inline4);

			section.Blocks.Add(paragraph);

			m_RichText.Content.Sections.Add(section);

			m_RichText.Content.Layout = ENTextLayout.Web;
            m_RichText.Selection.SelectionChanged += Selection_SelectionChanged;

			return m_RichText;
		}

        private void Selection_SelectionChanged(NEventArgs arg)
        {
			ClearHighlights();

			// highlight selected blocks
			NList selectedBlocks = m_RichText.Selection.GetSelectedBlocks();

			for (int i = 0; i < selectedBlocks.Count; i++)
            {
				selectedBlocks[i].BackgroundFill = new NColorFill(NColor.LightBlue);
            }

			NList selectedInlines = m_RichText.Selection.GetSelectedInlines(false);

			for (int i = 0; i < selectedInlines.Count; i++)
            {
				selectedInlines[i].BackgroundHighlightFill = new NColorFill(NColor.FromColor(NColor.Yellow, 125));
			}
        }

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

The example demonstrates how to track the selection changed event as well as how to query the selection object.

"; } /// /// Gets the relevant documentation links for the example /// /// public (string Name, string Url)[] GetDocumentationUrls() { return new[] { ("Rich Text Documents - Selection", "https://www.nevron.com/docs/openvision/rich-text-documents-selection.html"), ("Rich Text Range Elements", "https://www.nevron.com/docs/openvision/rich-text-range-elements.html"), }; } #endregion #region Implementation private void ClearHighlights() { NList blocks = m_RichText.Document.GetDescendants(NBlock.NBlockSchema); for (int i = 0; i < blocks.Count; i++) { ((NBlock)blocks[i]).BackgroundFill = null; } NList inlines = m_RichText.Document.GetDescendants(NInline.NInlineSchema); for (int i = 0; i < inlines.Count; i++) { ((NInline)inlines[i]).BackgroundHighlightFill = null; } } #endregion #region Event Handlers #endregion #region Fields private NRichTextView m_RichText; #endregion #region Static Methods #endregion #region Constants #endregion #region Nested Types #endregion } }

Download a fully functional C# (.cs) Selection Events Example for WinForms Solution

VB.NET Selection Events Example Code for Nevron RichTextEditor for WinForms
Language: VB.NET

Imports Nevron.Nov
Imports Nevron.Nov.DataStructures
Imports Nevron.Nov.Dom
Imports Nevron.Nov.Graphics
Imports Nevron.Nov.Layout
Imports Nevron.Nov.Text
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 - NSelectionChangedExample"

        ' 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 the rich text
            m_RichText = New NRichTextView()
            m_RichText.AcceptsTab = True
            m_RichText.Content.Sections.Clear()

            Dim section As NSection = New NSection()
            section.Blocks.Add(New NParagraph("The example demonstrates how to track the selection changed event."))
            section.Blocks.Add(New NParagraph("It also shows how to identify different text elements depending on the current selection."))
            section.Blocks.Add(New NParagraph("Move the selection and the control will highlight the currently selected words as well as blocks inside the text document."))


            Dim paragraph As NParagraph = New NParagraph()
            paragraph.Inlines.Add(New NTextInline("You can also detect the inline elements "))

            Dim inline1 As NTextInline = New NTextInline("that have different")
            paragraph.Inlines.Add(inline1)

            Dim inline2 As NTextInline = New NTextInline(" Font Style")
            inline2.FontStyle = ENFontStyle.Bold
            paragraph.Inlines.Add(inline2)

            Dim inline3 As NTextInline = New NTextInline(" Font Size")
            inline3.FontSize = 14
            paragraph.Inlines.Add(inline3)

            Dim inline4 As NTextInline = New NTextInline(" and / or other attributes")
            inline4.FontStyle = ENFontStyle.Italic Or ENFontStyle.Underline
            paragraph.Inlines.Add(inline4)

            section.Blocks.Add(paragraph)

            m_RichText.Content.Sections.Add(section)

            m_RichText.Content.Layout = ENTextLayout.Web
            Me.m_RichText.Selection.SelectionChanged += AddressOf Selection_SelectionChanged

            Return m_RichText
        End Function

        Private Sub Selection_SelectionChanged(arg As NEventArgs)
            ClearHighlights()

            ' highlight selected blocks
            Dim selectedBlocks As NList(Of NBlock) = m_RichText.Selection.GetSelectedBlocks()

            For i = 0 To selectedBlocks.Count - 1
                selectedBlocks(i).BackgroundFill = New NColorFill(NColor.LightBlue)
            Next

            Dim selectedInlines = m_RichText.Selection.GetSelectedInlines(False)

            For i = 0 To selectedInlines.Count - 1
                selectedInlines(i).BackgroundHighlightFill = New NColorFill(NColor.FromColor(NColor.Yellow, 125))
            Next
        End Sub

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

The example demonstrates how to track the selection changed event as well as how to query the selection object.

" End Function ''' ''' Gets the relevant documentation links for the example ''' ''' Public Function GetDocumentationUrls() As (String, String)() Return {("Rich Text Documents - Selection", "https://www.nevron.com/docs/openvision/rich-text-documents-selection.html"), ("Rich Text Range Elements", "https://www.nevron.com/docs/openvision/rich-text-range-elements.html")} End Function #End Region #Region "Implementation" Private Sub ClearHighlights() Dim blocks = m_RichText.Document.GetDescendants(NBlock.NBlockSchema) For i = 0 To blocks.Count - 1 CType(blocks(i), NBlock).BackgroundFill = Nothing Next Dim inlines = m_RichText.Document.GetDescendants(NInline.NInlineSchema) For i = 0 To inlines.Count - 1 CType(inlines(i), NInline).BackgroundHighlightFill = Nothing Next End Sub #End Region #Region "Event Handlers" #End Region #Region "Fields" Private m_RichText As NRichTextView #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) Selection Events Example for WinForms Solution