【发布时间】:2015-03-24 17:57:20
【问题描述】:
因为 TextBox 没有查找功能,所以我根据需要创建并修改了自己的版本。我已经创建了函数。一个用于Search Next,另一个用于Search Previous
我的问题是:
如果我的搜索词超过 1 个字符并且我已经搜索过 学期四次,如果在第四学期我决定点击 搜索上一个它会返回到上一个搜索词,并且 工作正常。现在因为我之前点击了 4 次搜索,我 如果我决定去 第四学期再次使用查找下一个,我必须双击查找下一个 然后它选择第 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;
}
【问题讨论】:
-
您需要做的是,当您从一个搜索更改为另一个搜索时,按搜索词的长度递增或递减 StartPosition。他们只是使 FindAndSelectPrevious 与其他搜索功能相反。
-
@Pedro.The.Kid 感谢您的快速响应,但我不明白您所说的递增或递减 startPosition 是什么意思?介意详细说明吗?
-
进行搜索之前的第一件事 if(
) startPostion += TextToFind.Length -
你为什么使用
dynamic? -
@Dai 它只是将代码从 vb.net 转换为 C#。我会解决的。
标签: c# textbox indexof lastindexof