Reputation: 2619
I am trying to implement search routines: Find Next and Find Previous in Webbrowser. I know that there is Build-in Find dialog inside, but I do not want to use it. Instead I am trying hard to write my own routines, but I can not get through. Following code implements "Find next" nicely:
using mshtml;
....
private void toolStripButton5_Click(object sender, EventArgs e)
{
IHTMLDocument2 htmlDocument = WB.Document.DomDocument as IHTMLDocument2;
IHTMLSelectionObject sel = (IHTMLSelectionObject)htmlDocument.selection;
IHTMLTxtRange tr = (IHTMLTxtRange)sel.createRange() as IHTMLTxtRange;
string s = toolStripTextBox1.Text;
tr.collapse(false);
tr.findText(s, 1, 0); // positive value, should search forward, negative backward
tr.select();
}
I am totaly helpless making "Find previous" routine. Can any genius forge it? (it will be probably very similar to that above).
Thanks very much
Upvotes: 2
Views: 1858
Reputation: 1
IHTMLTxtRange tmprange; private void Find(int Direction, bool Collapse) {
IHTMLDocument2 htmlDocument = browser.Document.DomDocument as IHTMLDocument2;
IHTMLSelectionObject sel = (IHTMLSelectionObject)htmlDocument.selection;
IHTMLTxtRange tr = (IHTMLTxtRange)sel.createRange() as IHTMLTxtRange;
string s = textBoxFindData.Text;
if (tmprange == null)
{
tr.collapse(Collapse);
tmprange = tr;
tr.findText(s, Direction, 0); // positive value, should search forward, negative backward
if (tr.text == null)
{
MessageBox.Show("Can not find string " + "[" + s + "]", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
tr.select();
}
else
{
tmprange.collapse(Collapse);
tmprange.findText(s, Direction, 0); // positive value, should search forward, negative backward
if (tmprange.text == null)
{
MessageBox.Show("Can not find string " + "[" + s + "]", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
tmprange.select();
}
}
private void buttonPrevious_Click(object sender, EventArgs e)
{
Find(-1, true);
}
Upvotes: 0
Reputation: 191
For "Find previous" routine use the same code with 2 modifications: 1) change argument of tr.collapse from false to true; 2) change the second parameter of tr.findText from 1 to -1.
So these line will be the following:
tr.collapse(true); // use true to move caret at the beginning of the current selection
tr.findText(s, -1, 0); // positive value, should search forward, negative backward
Upvotes: 1