因为 TextBox 没有查找功能,所以我根据需要创建并修改了自己的版本。我已经创建了函数。一个用于搜索下一个,另一个用于搜索上一个
我的问题是:
如果我的搜索词的长度超过 1 个字符并且我已经搜索了该词四次,如果在第 4 个词我决定点击搜索之前它会返回到上一个搜索词并且效果很好。现在因为我点击了前 4 次搜索并使用上一个搜索词进入了第 3 个搜索词,如果我决定使用 Find Next 再次搜索第 4 个搜索词,我必须双击 find next然后它选择第 4 个词。
如果我的搜索词是 1 个字符长并且我想搜索一个字符,我输入字符,例如“o”,它会遍历文本框中的每个字符,但是一旦我决定使用上一个搜索返回,我必须双击搜索上一个按钮,然后返回,如果我决定搜索下一个,我必须再次双击,然后它会搜索下一个。
这可能有助于理解双击和单击:
http://media.giphy.com/media/3xz2BJgF2DrtcCnP1e/giphy.gif
我一直试图让它工作很长一段时间,但我没有运气。我不知道我要去哪里,在我迷惑自己之前,如果有人能帮助我,那就太好了。
我的代码:
变量
public int startPostion = 0;
boolean passedNext;
public int pos = 0;
搜索下一个
public bool FindAndSelectNext(string TextToFind, bool MatchCase)
{
try
{
var mode = MatchCase ? StringComparison.CurrentCulture : StringComparison.CurrentCultureIgnoreCase;
int position = TheTextBox.Text.IndexOf(TextToFind, startPostion, mode);
pos = position;
if (position == -1)
{
var TheString = TheTextBox.Text;
var foundposition2 = TheString.IndexOf(TextToFind, mode);
TheTextBox.SelectionStart = foundposition2;
TheTextBox.SelectionLength = TextToFind.Length;
startPostion = foundposition2 + TextToFind.Length;
TheTextBox.Focus();
passedNext = false;
Debug.WriteLine("1");
return true;
}
else
{
TheTextBox.SelectionStart = position;
TheTextBox.SelectionLength = TextToFind.Length;
startPostion = position + TextToFind.Length;
TheTextBox.Focus();
passedNext = true;
Debug.WriteLine("2");
return true;
}
}
catch (Exception ex)
{
MessageBox.Show(string.Format("Could not find '{0}' in the document.", TextToFind), ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
return true;
}
搜索上一个
public bool FindAndSelectPrevious(string TextToFind, bool MatchCase)
{
StringComparison mode = MatchCase ? StringComparison.CurrentCulture : StringComparison.CurrentCultureIgnoreCase;
if (passedNext == true)
{
int foundPosition = startPostion < 0 ? TheTextBox.Text.Length : startPostion - 1;
foundPosition = TheTextBox.Text.LastIndexOf(TextToFind, pos, mode);
passedNext = false;
if (foundPosition < 0)
{
if (startPostion < 0)
{
MessageBox.Show(string.Format("Could not find '{0}' in the document.", TextToFind), ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
return false;
}
foundPosition = TheTextBox.Text.LastIndexOf(TextToFind, mode);
Debug.WriteLine("1p");
}
TheTextBox.SelectionStart = foundPosition;
TheTextBox.SelectionLength = TextToFind.Length;
startPostion = foundPosition;
TheTextBox.Focus();
Debug.WriteLine("2p");
passedNext = false;
}
else
{
int foundPosition = startPostion < 0 ? TheTextBox.Text.Length : startPostion;
foundPosition = TheTextBox.Text.LastIndexOf(TextToFind, foundPosition, mode);
if (foundPosition < 0)
{
if (startPostion < 0)
{
MessageBox.Show(string.Format("Could not find '{0}' in the document.", TextToFind), ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
return false;
}
foundPosition = TheTextBox.Text.LastIndexOf(TextToFind, mode);
}
if (!(foundPosition == -1))
{
try
{
int foundPositionz = startPostion < 0 ? TheTextBox.Text.Length : startPostion - 1;
foundPositionz = TheTextBox.Text.LastIndexOf(TextToFind, foundPositionz, mode);
TheTextBox.SelectionStart = foundPositionz;
TheTextBox.SelectionLength = TextToFind.Length;
startPostion = foundPositionz;
TheTextBox.Focus();
}
catch (Exception ex)
{
var TheString = TheTextBox.Text;
var foundposition2 = TheString.LastIndexOf(TextToFind, mode);
TheTextBox.SelectionStart = foundposition2;
TheTextBox.SelectionLength = TextToFind.Length;
startPostion = foundposition2;
TheTextBox.Focus();
Debug.WriteLine("12p");
}
}
else
{
MessageBox.Show(string.Format("Could not find '{0}' in the document.", TextToFind), ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
return true;
}