|
using System;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Collections.Generic;
namespace FastColoredTextBoxNS
{
public partial class FindForm : Form
{
bool firstSearch = true;
Place startPlace;
FastColoredTextBox tb;
public FindForm(FastColoredTextBox tb)
{
InitializeComponent();
this.tb = tb;
}
private void btClose_Click(object sender, EventArgs e)
{
Close();
}
private void btFindNext_Click(object sender, EventArgs e)
{
FindNext(tbFind.Text);
}
public void FindNext(string pattern)
{
try
{
RegexOptions opt = cbMatchCase.Checked ? RegexOptions.None : RegexOptions.IgnoreCase;
if (!cbRegex.Checked)
pattern = Regex.Escape(pattern);
if (cbWholeWord.Checked)
pattern = "\\b" + pattern + "\\b";
//
Range range = tb.Selection.Clone();
range.Normalize();
//
if (firstSearch)
{
startPlace = range.Start;
firstSearch = false;
}
//
range.Start = range.End;
if (range.Start >= startPlace)
range.End = new Place(tb.GetLineLength(tb.LinesCount - 1), tb.LinesCount - 1);
else
range.End = startPlace;
//
foreach (var r in range.GetRangesByLines(pattern, opt))
{
tb.Selection = r;
tb.DoSelectionVisible();
tb.Invalidate();
return;
}
//
if (range.Start >= startPlace && startPlace > Place.Empty)
{
tb.Selection.Start = new Place(0, 0);
FindNext(pattern);
return;
}
MessageBox.Show("Not found");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void tbFind_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '\r')
{
btFindNext.PerformClick();
e.Handled = true;
return;
}
if (e.KeyChar == '\x1b')
{
Hide();
e.Handled = true;
return;
}
}
private void FindForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
Hide();
}
}
protected override void OnActivated(EventArgs e)
{
tbFind.Focus();
ResetSerach();
}
void ResetSerach()
{
firstSearch = true;
}
private void cbMatchCase_CheckedChanged(object sender, EventArgs e)
{
ResetSerach();
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I am Pavеl Tоrgаshоv, and I live in Kyiv, Ukraine.
I've been developing software since 1998.
Main activities: processing of large volumes of data, statistics, computer vision and graphics.