Find and Replace Example for WPF

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

Find and Replace Example for WPF Description

This example demonstrates how to find and replace text.

Press the "Find All" button to highlight all occurrences of "Find".

Press the "Replace All" button to replace and highlight all occurrences of "Find" with "Replace"

Press the "Clear Highlight" button to clear all highlighting


Find and Replace Example

C# Find and Replace Example Code for Nevron Diagram for WPF
Language: C#

using Nevron.Nov;
using Nevron.Nov.DataStructures;
using Nevron.Nov.Diagram;
using Nevron.Nov.Diagram.Shapes;
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 - NFindAndReplaceExample";

			// 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 a simple drawing
			NDrawingViewWithRibbon drawingViewWithRibbon = new NDrawingViewWithRibbon();
			m_DrawingView = drawingViewWithRibbon.View;

			m_DrawingView.Document.HistoryService.Pause();
			try
			{
				InitDiagram(m_DrawingView.Document);
			}
			finally
			{
				m_DrawingView.Document.HistoryService.Resume();
			}

			return drawingViewWithRibbon;
		}
		public NWidget CreateExampleControls()
		{
			NStackPanel stack = new NStackPanel();
			
			m_FindTextBox = new NTextBox();
			m_FindTextBox.Text = "quick";
			stack.Add(new NPairBox(new NLabel("Find:"), m_FindTextBox, ENPairBoxRelation.Box1AboveBox2));

			m_ReplaceTextBox = new NTextBox();
			m_ReplaceTextBox.Text = "slow";
			stack.Add(new NPairBox(new NLabel("Replace:"), m_ReplaceTextBox, ENPairBoxRelation.Box1AboveBox2));

			NButton findAllButton = new NButton("Find All");
			findAllButton.Click += OnFindAllButtonClick;
			stack.Add(findAllButton);

			NButton replaceAllButton = new NButton("Replace All");
			replaceAllButton.Click += OnReplaceAllButtonClick;
			stack.Add(replaceAllButton);

			NButton clearHighlightButton = new NButton("Clear Highlight");
			clearHighlightButton.Click += OnClearHighlightButtonClick;
			stack.Add(clearHighlightButton);

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

This example demonstrates how to find and replace text.

Press the ""Find All"" button to highlight all occurrences of ""Find"".

Press the ""Replace All"" button to replace and highlight all occurrences of ""Find"" with ""Replace""

Press the ""Clear Highlight"" button to clear all highlighting

"; } /// /// Gets the relevant documentation links for the example /// /// public (string Name, string Url)[] GetDocumentationUrls() { return new[] { ("Rich Text - Finding And Replacing Text", "https://www.nevron.com/docs/openvision/rich-text-finding-and-replacing-text.html"), ("Text Block", "https://www.nevron.com/docs/openvision/diagram-text-block.html"), }; } private void InitDiagram(NDrawingDocument drawingDocument) { NDrawing drawing = drawingDocument.Content; // hide the grid drawing.ScreenVisibility.ShowGrid = false; double padding = 10; double sizeX = 160; double sizeY = 160; NBasicShapeFactory factory = new NBasicShapeFactory(); for (int x = 0; x < 4; x++) { for (int y = 0; y < 4; y++) { NShape shape1 = factory.CreateShape(ENBasicShape.Rectangle); shape1.SetBounds(padding + x * (padding + sizeX), padding + y * (padding + sizeY), sizeX, sizeY); shape1.TextBlock = new NTextBlock(); shape1.TextBlock.Padding = new NMargins(20); shape1.TextBlock.Text = "The quick brown fox jumps over the lazy dog"; drawing.ActivePage.Items.Add(shape1); } } } #endregion #region Implementation #endregion #region Event Handlers /// /// Called when the user presses the find all button /// /// void OnFindAllButtonClick(NEventArgs arg) { // init find settings NFindTextSettings settings = new NFindTextSettings(); settings.FindWhat = m_FindTextBox.Text; settings.SearchDirection = ENDiagramTextSearchDirection.ForwardReading; // loop through all occurrences NTextSearcher searcher = new NTextSearcher(m_DrawingView, settings); searcher.ActivateEditor = false; NShapeTextSearchState state; while (searcher.FindNext(out state)) { state.Shape.GetTextSelection().SetHighlightFillToSelectedInlines(new NColorFill(ENNamedColor.Red)); } } /// /// Called when the user presses the replace all button /// /// void OnReplaceAllButtonClick(NEventArgs arg) { // init find settings NFindTextSettings settings = new NFindTextSettings(); settings.FindWhat = m_FindTextBox.Text; settings.SearchDirection = ENDiagramTextSearchDirection.ForwardReading; // find all occurrences NTextSearcher searcher = new NTextSearcher(m_DrawingView, settings); searcher.ActivateEditor = false; NShapeTextSearchState state; while (searcher.FindNext(out state)) { NSelection selection = state.Shape.GetTextSelection(); // replace NRangeI selectedRange = selection.SelectedRange; selection.InsertText(m_ReplaceTextBox.Text); if (m_ReplaceTextBox.Text.Length > 0) { selection.SelectRange(new NRangeI(selectedRange.Begin, selectedRange.Begin + m_ReplaceTextBox.Text.Length - 1)); selection.SetHighlightFillToSelectedInlines(new NColorFill(ENNamedColor.LimeGreen)); } } } /// /// Called when the user presses clear highlight button /// /// void OnClearHighlightButtonClick(NEventArgs arg) { NList shapes = m_DrawingView.Content.GetDescendants(NShape.NShapeSchema); for (int i = 0; i < shapes.Count; i++) { NShape shape = (NShape)shapes[i]; NRangeTextElement rootTextElement = (NRangeTextElement)shape.GetTextBlockContentNoCreate(); if (rootTextElement == null) continue; rootTextElement.VisitRanges(delegate (NRangeTextElement range) { NInline inline = range as NInline; if (inline != null) { inline.ClearLocalValue(NInline.BackgroundHighlightFillProperty); } }); } } #endregion #region Fields private NDrawingView m_DrawingView; private NTextBox m_FindTextBox; private NTextBox m_ReplaceTextBox; #endregion #region Static Methods #endregion #region Constants #endregion #region Nested Types #endregion } }

Download a fully functional C# (.cs) Find and Replace Example for WPF Solution

VB.NET Find and Replace Example Code for Nevron Diagram for WPF
Language: VB.NET

Imports Nevron.Nov
Imports Nevron.Nov.Diagram
Imports Nevron.Nov.Diagram.Shapes
Imports Nevron.Nov.Dom
Imports Nevron.Nov.Graphics
Imports Nevron.Nov.Layout
Imports Nevron.Nov.Text
Imports Nevron.Nov.UI
Imports Nevron.Nov.Windows.Wpf

Class MainWindow

#Region "Constructors"

	Public Sub New()
		' This call is required by the designer.
		InitializeComponent()

		' Add any initialization after the InitializeComponent() call.

		' Set form title
		Title = "NOV WPF - NFindAndReplaceExample"

		' 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)
		Content = host
	End Sub


#End Region

#Region "Example"

        Public Function CreateExampleContent() As NWidget
            ' Create a simple drawing
            Dim drawingViewWithRibbon As NDrawingViewWithRibbon = New NDrawingViewWithRibbon()
            m_DrawingView = drawingViewWithRibbon.View

            m_DrawingView.Document.HistoryService.Pause()
            Try
                InitDiagram(m_DrawingView.Document)
            Finally
                m_DrawingView.Document.HistoryService.Resume()
            End Try

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

            m_FindTextBox = New NTextBox()
            m_FindTextBox.Text = "quick"
            stack.Add(New NPairBox(New NLabel("Find:"), m_FindTextBox, ENPairBoxRelation.Box1AboveBox2))

            m_ReplaceTextBox = New NTextBox()
            m_ReplaceTextBox.Text = "slow"
            stack.Add(New NPairBox(New NLabel("Replace:"), m_ReplaceTextBox, ENPairBoxRelation.Box1AboveBox2))

            Dim findAllButton As NButton = New NButton("Find All")
            findAllButton.Click += AddressOf OnFindAllButtonClick
            stack.Add(findAllButton)

            Dim replaceAllButton As NButton = New NButton("Replace All")
            replaceAllButton.Click += AddressOf OnReplaceAllButtonClick
            stack.Add(replaceAllButton)

            Dim clearHighlightButton As NButton = New NButton("Clear Highlight")
            clearHighlightButton.Click += AddressOf OnClearHighlightButtonClick
            stack.Add(clearHighlightButton)

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

This example demonstrates how to find and replace text.

Press the ""Find All"" button to highlight all occurrences of ""Find"".

Press the ""Replace All"" button to replace and highlight all occurrences of ""Find"" with ""Replace""

Press the ""Clear Highlight"" button to clear all highlighting

" End Function ''' ''' Gets the relevant documentation links for the example ''' ''' Public Function GetDocumentationUrls() As (String, String)() Return {("Rich Text - Finding And Replacing Text", "https://www.nevron.com/docs/openvision/rich-text-finding-and-replacing-text.html"), ("Text Block", "https://www.nevron.com/docs/openvision/diagram-text-block.html")} End Function Private Sub InitDiagram(drawingDocument As NDrawingDocument) Dim drawing = drawingDocument.Content ' hide the grid drawing.ScreenVisibility.ShowGrid = False Dim padding As Double = 10 Dim sizeX As Double = 160 Dim sizeY As Double = 160 Dim factory As NBasicShapeFactory = New NBasicShapeFactory() For x = 0 To 3 For y = 0 To 3 Dim shape1 = factory.CreateShape(ENBasicShape.Rectangle) shape1.SetBounds(padding + x * (padding + sizeX), padding + y * (padding + sizeY), sizeX, sizeY) shape1.TextBlock = New NTextBlock() shape1.TextBlock.Padding = New NMargins(20) shape1.TextBlock.Text = "The quick brown fox jumps over the lazy dog" drawing.ActivePage.Items.Add(shape1) Next Next End Sub #End Region #Region "Implementation" #End Region #Region "Event Handlers" ''' ''' Called when the user presses the find all button ''' ''' Private Sub OnFindAllButtonClick(arg As NEventArgs) ' init find settings Dim settings As NFindTextSettings = New NFindTextSettings() settings.FindWhat = m_FindTextBox.Text settings.SearchDirection = ENDiagramTextSearchDirection.ForwardReading ' loop through all occurrences Dim searcher As NTextSearcher = New NTextSearcher(m_DrawingView, settings) searcher.ActivateEditor = False Dim state As NShapeTextSearchState While searcher.FindNext(state) state.Shape.GetTextSelection().SetHighlightFillToSelectedInlines(New NColorFill(ENNamedColor.Red)) End While End Sub ''' ''' Called when the user presses the replace all button ''' ''' Private Sub OnReplaceAllButtonClick(arg As NEventArgs) ' init find settings Dim settings As NFindTextSettings = New NFindTextSettings() settings.FindWhat = m_FindTextBox.Text settings.SearchDirection = ENDiagramTextSearchDirection.ForwardReading ' find all occurrences Dim searcher As NTextSearcher = New NTextSearcher(m_DrawingView, settings) searcher.ActivateEditor = False Dim state As NShapeTextSearchState While searcher.FindNext(state) Dim selection As NSelection = state.Shape.GetTextSelection() ' replace Dim selectedRange As NRangeI = selection.SelectedRange selection.InsertText(m_ReplaceTextBox.Text) If m_ReplaceTextBox.Text.Length > 0 Then selection.SelectRange(New NRangeI(selectedRange.Begin, selectedRange.Begin + m_ReplaceTextBox.Text.Length - 1)) selection.SetHighlightFillToSelectedInlines(New NColorFill(ENNamedColor.LimeGreen)) End If End While End Sub ''' ''' Called when the user presses clear highlight button ''' ''' Private Sub OnClearHighlightButtonClick(arg As NEventArgs) Dim shapes = m_DrawingView.Content.GetDescendants(NShape.NShapeSchema) For i = 0 To shapes.Count - 1 Dim shape = CType(shapes(i), NShape) Dim rootTextElement As NRangeTextElement = CType(shape.GetTextBlockContentNoCreate(), NRangeTextElement) If rootTextElement Is Nothing Then Continue For rootTextElement.VisitRanges(Sub(range As NRangeTextElement) Dim inline As NInline = TryCast(range, NInline) If inline IsNot Nothing Then inline.ClearLocalValue(NTextElement.BackgroundHighlightFillProperty) End If End Sub) Next End Sub #End Region #Region "Fields" Private m_DrawingView As NDrawingView Private m_FindTextBox As NTextBox Private m_ReplaceTextBox As NTextBox #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) Find and Replace Example for WPF Solution