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

| 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
}
}