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.Dom;
using Nevron.Nov.Graphics;
using Nevron.Nov.Layout;
using Nevron.Nov.Text;
using Nevron.Nov.Text.Formats;
using Nevron.Nov.UI;
using Nevron.Nov.Windows.Forms;
using System.IO;
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 the rich text
NRichTextViewWithRibbon richTextWithRibbon = new NRichTextViewWithRibbon();
m_RichText = richTextWithRibbon.View;
m_RichText.AcceptsTab = true;
m_RichText.Content.Sections.Clear();
// Populate the rich text
PopulateRichText();
return richTextWithRibbon;
}
public NWidget CreateExampleControls()
{
NStackPanel stack = new NStackPanel();
m_FindTextBox = new NTextBox();
m_FindTextBox.Text = "princess";
stack.Add(new NPairBox(new NLabel("Find:"), m_FindTextBox, ENPairBoxRelation.Box1AboveBox2));
m_ReplaceTextBox = new NTextBox();
m_ReplaceTextBox.Text = "queen";
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"),
("Rich Text Documents - Selection", "https://www.nevron.com/docs/openvision/rich-text-documents-selection.html"),
};
}
private void PopulateRichText()
{
NSection section = new NSection();
m_RichText.Content.Sections.Add(section);
section.Blocks.Add(GetDescriptionBlock("Find and Replace Text", "The example demonstrates how to work find and replace text.", 1));
for (int i = 0; i < 10; i++)
{
section.Blocks.Add(new NParagraph("Now it so happened that on one occasion the princess's golden ball did not fall into the little hand which she was holding up for it, but on to the ground beyond, and rolled straight into the water. She followed it with her eyes, but it vanished, and the well was deep, so deep that the bottom could not be seen. At this she began to cry, and cried louder and louder, and could not be comforted. And as she thus lamented someone said to her, \"What ails you? You weep so that even a stone would show pity.\""));
}
}
#endregion
#region Implementation
#endregion
#region Event Handlers
///
/// Called when the user presses the find all button
///
///
void OnFindAllButtonClick(NEventArgs arg)
{
// init find settings
NFindSettings settings = new NFindSettings();
settings.FindWhat = m_FindTextBox.Text;
settings.SearchDirection = ENSearchDirection.Forward;
// loop through all occurances
NRangeI textRange = NRangeI.Zero;
while (m_RichText.EditingRoot.FindNext(settings, ref textRange))
{
m_RichText.Selection.SelectRange(textRange);
m_RichText.Selection.SetHighlightFillToSelectedInlines(new NColorFill(ENNamedColor.Red));
}
// move caret to beginning of document
m_RichText.Selection.MoveCaret(ENCaretMoveDirection.DocumentBegin, false);
}
///
/// Called when the user presses the replace all button
///
///
void OnReplaceAllButtonClick(NEventArgs arg)
{
// init find settings
NFindSettings settings = new NFindSettings();
settings.FindWhat = m_FindTextBox.Text;
settings.SearchDirection = ENSearchDirection.Forward;
// find all occurances
NRangeI textRange = NRangeI.Zero;
NSelection selection = m_RichText.EditingRoot.Selection;
while (m_RichText.EditingRoot.FindNext(settings, ref textRange))
{
// replace dog with cat
selection.SelectRange(textRange);
selection.InsertText(m_ReplaceTextBox.Text);
if (m_ReplaceTextBox.Text.Length > 0)
{
selection.SelectRange(new NRangeI(textRange.Begin, textRange.Begin + m_ReplaceTextBox.Text.Length - 1));
selection.SetHighlightFillToSelectedInlines(new NColorFill(ENNamedColor.LimeGreen));
}
}
// move caret to beginning of document
selection.MoveCaret(ENCaretMoveDirection.DocumentBegin, false);
}
///
/// Called when the user presses clear highlight button
///
///
void OnClearHighlightButtonClick(NEventArgs arg)
{
((Nevron.Nov.Text.NBlock)m_RichText.EditingRoot).VisitRanges(delegate(NRangeTextElement range)
{
NInline inline = range as NInline;
if (inline != null)
{
inline.ClearLocalValue(NInline.BackgroundHighlightFillProperty);
}
});
}
#endregion
#region Fields
private NRichTextView m_RichText;
private NTextBox m_FindTextBox;
private NTextBox m_ReplaceTextBox;
#endregion
#region Static Methods
private static NParagraph GetDescriptionParagraph(string text)
{
return new NParagraph(text);
}
private static NParagraph GetTitleParagraphNoBorder(string text, int level)
{
double fontSize = 10;
ENFontStyle fontStyle = ENFontStyle.Regular;
switch (level)
{
case 1:
fontSize = 16;
fontStyle = ENFontStyle.Bold;
break;
case 2:
fontSize = 10;
fontStyle = ENFontStyle.Bold;
break;
}
NParagraph paragraph = new NParagraph();
paragraph.HorizontalAlignment = ENAlign.Left;
paragraph.FontSize = fontSize;
paragraph.FontStyle = fontStyle;
NTextInline textInline = new NTextInline(text);
textInline.FontStyle = fontStyle;
textInline.FontSize = fontSize;
paragraph.Inlines.Add(textInline);
return paragraph;
}
private static NGroupBlock GetDescriptionBlock(string title, string description, int level)
{
NColor color = NColor.Black;
NParagraph paragraph = GetTitleParagraphNoBorder(title, level);
NGroupBlock groupBlock = new NGroupBlock();
groupBlock.ClearMode = ENClearMode.All;
groupBlock.Blocks.Add(paragraph);
groupBlock.Blocks.Add(GetDescriptionParagraph(description));
groupBlock.Border = CreateLeftTagBorder(color);
groupBlock.BorderThickness = defaultBorderThickness;
return groupBlock;
}
///
/// Creates a left tag border with the specified border
///
///
///
private static NBorder CreateLeftTagBorder(NColor color)
{
NBorder border = new NBorder();
border.LeftSide = new NBorderSide();
border.LeftSide.Fill = new NColorFill(color);
return border;
}
#endregion
#region Constants
private static readonly NMargins defaultBorderThickness = new NMargins(5.0, 0.0, 0.0, 0.0);
#endregion
#region Nested Types
#endregion
}
}