TextBox Indexof 和 LastIndexOf

TextBox Indexof and LastIndexOf

因为 TextBox 没有查找功能,所以我根据需要创建并修改了我自己的版本。我已经创建了功能。一个用于搜索下一个,另一个用于搜索上一个

我的问题是:

  1. 如果我的搜索字词长度超过 1 个字符并且我已经搜索过 学期四次,如果在第 4 个学期我决定点击 搜索上一个它会返回到上一个搜索词,然后 工作正常。现在因为我点击了前 4 次搜索然后我 使用上一个搜索词转到第三个搜索词,如果我决定去 第四学期再次使用“查找下一个”,我必须双击“查找下一个” 然后 然后 它选择第 4 项。

  2. 如果我的搜索字词长度为 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;
    }

恕我直言,您应该在用户第一次尝试查找内容时找到所有匹配项,然后保留一个索引,指示哪个匹配项是 selected/highlighted。

例如:

private List<int> _matches;
private string _textToFind;
private bool _matchCase;
private int _matchIndex;

private void MoveToNextMatch(string textToFind, bool matchCase, bool forward)
{
    if (_matches == null ||
        _textToFind != textToFind ||
        _matchCase != matchCase)
    {
        int startIndex = 0, matchIndex;
        StringComparison mode = matchCase ?
            StringComparison.CurrentCulture :
            StringComparison.CurrentCultureIgnoreCase;

        _matches = new List();
        while (startIndex < TheTextBox.Text.Length &&
            (matchIndex = TheTextBox.Text.IndexOf(textToFind, startIndex, mode)) >= 0)
        {
            _matches.Add(matchIndex);
            startIndex = matchIndex + textToFind.Length;
        }

        _textToFind = textToFind;
        _matchCase = matchCase;
        _matchIndex = forward ? 0 : _matches.Count - 1;
    }
    else
    {
        _matchIndex += forward ? 1 : -1;
        if (_matchIndex < 0)
        {
            _matchIndex = _matches.Count - 1;
        }
        else if (_matchIndex >= _matches.Count)
        {
            _matchIndex = 0;
        }
    }

    if (_matches.Count > 0)
    {
        TheTextBox.SelectionStart = _matches[_matchIndex];
        TheTextBox.SelectionLength = textToFind.Length;
        TheTextBox.Focus();
    }
    else
    {
       MessageBox.Show(string.Format(
           "Could not find '{0}' in the document.", TextToFind),
           ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);

    }
}

public bool FindAndSelectNext(string textToFind, bool matchCase)
{
    MoveToNextMatch(textToFind, matchCase, true);
}

public bool FindAndSelectPrevious(string textToFind, bool matchCase)
{
    MoveToNextMatch(textToFind, matchCase, false);
}