[英]How can I search for a string value on a selected row in the datagridview?
[英]How can I search only in users selected Selection
我知道如何搜索整个文档。 我正在使用 foreach(doc.Sections 中的 Word.Section 段落)。 但是如果我只想在选择中搜索呢? 假设用户将 select 文本然后进行搜索。 然后我们只想在选择中搜索。 我怎么能用 Foreach sicle 做到这一点? Rigt 现在我有这个代码:
app.Selection.HomeKey(Unit: WdUnits.wdStory);
while (WordsCount >= 1)
foreach (Word.Section paragraph in doc.Sections)
{
Word.Selection rngFound = FindAndReplace(app.Selection, searchTerm, ""); //searching and wrapping.
if (rngFound != null)
{
string foundNr = rngFound.Text;
string hyperlinkNr = foundNr.Replace((char)30, (char)45);
rngFound.Range.Hyperlinks.Add(rngFound.Range, hyperlink + hyperlinkNr);
}
WordsCount--;
我知道有多少字(我在找什么)。 但是我怎么能说搜索 function 我们只需要在选择中搜索。 或该选择的范围。
这是我的搜索 function:
Word.Selection FindAndReplace(Word.Selection rngToSearch, object findText, object replaceWithText) //Find function
{
bool found = false;
//options
object matchCase = false;
object matchWholeWord = true;
object matchWildCards = true;
object matchSoundsLike = false;
object matchAllWordForms = false;
object forward = true;
object format = false;
object matchKashida = false;
object matchDiacritics = false;
object matchAlefHamza = false;
object matchControl = false;
object read_only = false;
object visible = true;
object replace = false;
object wrap = Word.WdFindWrap.wdFindStop; ;
//execute find and replace
found = rngToSearch.Find.Execute(ref findText, ref matchCase, ref matchWholeWord,
ref matchWildCards, ref matchSoundsLike, ref matchAllWordForms, ref forward, ref wrap, ref format, ref replaceWithText, ref replace,
ref matchKashida, ref matchDiacritics, ref matchAlefHamza, ref matchControl);
if (!found)
{
rngToSearch = null;
}
return rngToSearch;
}
谢谢辛迪。 我正在使用选择,因为如果我将使用范围,那么它将找到第一个搜索词并在其上循环。 我有 object forward = true; 在我的搜索 function 中。 (所以这种行为对我来说很奇怪)
private void button4_Click(object sender, RibbonControlEventArgs e)
{
string hyperlink = "https://Test";
string searchTerm =
@"<([1-5])[-^~]([0-9]{2})[-^~]([0-9]{1;6})/([0-9]{1;4})>"; // testing with 2-19-1023/47 2-19-1023/49
Word.Application app = Globals.ThisAddIn.Application;
Word.Range rngSel = app.Selection.Range;
Word.Range rngSearch = rngSel.Duplicate;
Word.Range rngFound = FindAndReplace(rngSearch, searchTerm, "");
while (rngFound != null)
{
string foundNr = rngFound.Text;
string hyperlinkNr = foundNr.Replace((char)30, (char)45);
rngFound.Hyperlinks.Add(rngFound, hyperlink + hyperlinkNr);
rngSearch.Start = rngFound.End;
rngSearch.End = rngSel.End;
rngFound = FindAndReplace(rngSearch, searchTerm, "");
File.AppendAllText(@"C:\install\CSharp\tulemus.txt", $"File name is: {rngFound.Text}" + Environment.NewLine);
}
而且我在测试文本文件中得到了无穷无尽的第一个可搜索字符串。 你知道我该如何克服吗?
只要每个搜索周期都限制在特定范围内,就应该只在该范围内执行搜索。 这是通过将选择分配给Range
object 并使用其他Range
对象来完成的
在以下示例中,根据问题中的代码,使用当前选择声明并实例化Range
object。 然后将第二个Range
设置为第一个 Range 的Duplicate
属性(这将创建一个“浅副本”,没有指向原始范围的链接)。
然后将第二个范围传递给 function FindAndReplace
,它返回包含找到的内容的第三个Range
。 然后对其进行处理。
最后,将搜索范围的起点设置为找到范围的终点(这样搜索从最后一个“命中”的末尾开始),并将搜索Range
的终点设置为终点原始Range
和重复搜索。
注意整个变化,调整为使用Range
而不是Selection
对象。
Word.Range rngSel = app.Selection;
Word.Range rngSearch = rngSel.Duplicate;
Word.Range rngFound = FindAndReplace(rngSearch, searchTerm, "");
while (rngFound != null)
{
string foundNr = rngFound.Text;
string hyperlinkNr = foundNr.Replace((char)30, (char)45);
rngFound.Hyperlinks.Add(rngFound, hyperlink + hyperlinkNr);
rngSearch.Start = rngFound.End
rngSearch.End = rngSel.End;
rngFound = FindAndReplace(rngSearch, searchTerm, "");
}
Word.Range FindAndReplace(Word.Range rngToSearch, object findText, object replaceWithText) //Find function
{
我解决了。 1.第一件事,我不知道的是,如果我们正在替换,搜索和替换 function 会向前推进。 但是,如果我们不替换,而只是搜索,那么使用范围属性将无法正常工作。 2. 我设法使用 Rgegex 制作了自己的方法。 我没有设置我的选择的起点和终点,而只是计算可搜索的单词。 也许这不是最好的解决方案,但它对我有用。 这是代码:
private void button5_Click(object sender, RibbonControlEventArgs e)
{
int WordsCount2 = 0;
string hyperlink = "https://Test";
string searchTerm =
@"<([1-5])[-^~]([0-9]{2})[-^~]([0-9]{1;6})/([0-9]{1;4})>"; // testing with 2-19-1023/47 2-19-1023/49
Word.Application app = Globals.ThisAddIn.Application;
Word.Range rngSel = app.Selection.Range;
string s = rngSel.Text;
Word.Range rngSearch = rngSel.Duplicate;
Regex regex2 = new Regex(@"\d*(-|\036)\d*(-|\036)\d*/\d*");
if (regex2.Matches(s).Count != 0)
{
WordsCount2 = regex2.Matches(s).Count;
while (WordsCount2 >= 1)
{
Word.Range rngFound = FindAndReplace(rngSel, searchTerm, "");
if (rngFound != null) {
string foundNr = rngFound.Text;
string hyperlinkNr = foundNr.Replace((char)30, (char)45);
rngFound.Hyperlinks.Add(rngFound, hyperlink + hyperlinkNr);
if (rngFound != null)
{
FindAndReplace2(rngSel, searchTerm, rngFound.Text.Replace((char)45, (char)30).Replace((char)32, (char)160));
// File.AppendAllText(@"C:\install\CSharp\tulemus.txt", $"File name is: {rngFound.Text}" + Environment.NewLine)
}
WordsCount2--;
}}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.