TextBox Indexof和LastIndexOf

时间:2015-01-26 10:13:02

标签: c# textbox indexof lastindexof

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

我的问题是:

  1. 如果我的搜索字词长度超过1个字符且我已搜索过 对于这个术语四次,如果在第四学期我决定点击 之前搜索它会回到上一个搜索词 工作良好。现在,因为我点击了前4次搜索和我 使用之前的第3个搜索词,如果我决定去 第四学期再次使用Find Next,我必须双击find next 然后然后选择第4个词。

  2. 如果我的搜索字词长度为1个字符,我想搜索一个字符 字符,我输入字符,例如'o',它贯穿每一个 文本框中的字符,但一旦我决定使用搜索返回 以前,我必须双击搜索上一个按钮,然后它 回去,如果我决定接下来搜索,我必须双击 然后它再次搜索。

  3. 这可能有助于理解双击和单击:

    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;
        }
    

1 个答案:

答案 0 :(得分:2)

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

例如:

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);
}