C# 在Windows应用商店应用程序中查找下一个函数
我正在尝试在我的windows应用商店应用程序中使用“查找/查找下一个”功能。 我要搜索和选择的单词位于名为“tboxFind”的文本框中。 文本框“EditorWindow”包含我的所有文本 只有当“editorWindow”中有一行文本时,我的函数才能正常工作。 否则,所选内容将按新行数向前移动 如何修复它 有没有简单的方法来创建find next函数C# 在Windows应用商店应用程序中查找下一个函数,c#,.net,C#,.net,我正在尝试在我的windows应用商店应用程序中使用“查找/查找下一个”功能。 我要搜索和选择的单词位于名为“tboxFind”的文本框中。 文本框“EditorWindow”包含我的所有文本 只有当“editorWindow”中有一行文本时,我的函数才能正常工作。 否则,所选内容将按新行数向前移动 如何修复它 有没有简单的方法来创建find next函数 private void btnFind_Click(object sender, Windows.UI.Xaml.RoutedEventA
private void btnFind_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
if ((tmpPos) == pos && tmpWord == tboxFind.Text && !String.IsNullOrEmpty(editorWindow.Text))
{
string tmpString = editorWindow.Text.Substring(pos + tboxFind.Text.Length);
tmpPos = tmpString.ToLower().IndexOf(tboxFind.Text.ToLower());
if (tmpPos != -1)
{
editorWindow.Focus(Windows.UI.Xaml.FocusState.Keyboard);
editorWindow.SelectionStart = pos + tmpPos + tboxFind.Text.Length;
editorWindow.SelectionLength = tboxFind.Text.Length;
pos = pos + tmpPos + tboxFind.Text.Length;
}
}
tmpWord = tboxFind.Text;
tmpPos = pos;
}
//编辑:
我找到了一种不同的方法来创建该函数。这是我的密码:
private void btnFind_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
numOfNewLines = 0;
pos = (tmpWord == tboxFind.Text) ? editorWindow.Text.ToLower().IndexOf(tboxFind.Text, pos + tboxFind.Text.Length)
: editorWindow.Text.ToLower().IndexOf(tboxFind.Text);
if (pos != -1)
{
foreach (char s in editorWindow.Text.Substring(0, pos))
{
if (s == '\n')
{
numOfNewLines++;
}
}
pos -= numOfNewLines;
editorWindow.Focus(Windows.UI.Xaml.FocusState.Keyboard);
//tmpPos = editorWindow.Text.ToLower().IndexOf(tboxFind.Text);
editorWindow.Select(pos, tboxFind.Text.Length);
pos += numOfNewLines;
}
tmpWord = tboxFind.Text;
}
我不确定你的代码有什么问题,因为我不能完全复制它,但是在一个基本的Windows应用程序中考虑下面的SCSCE:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
foreach (var i in FindIndicies("text"))
{
this.textBox1.SelectionStart = i;
this.textBox1.SelectionLength = "text".Length;
var result = MessageBox.Show(
"Move to the next index?",
"Next?",
MessageBoxButtons.YesNo);
if (result == System.Windows.Forms.DialogResult.No) { break; }
}
}
private List<int> FindIndicies(string textToFind)
{
var indicies = new List<int>();
var offset = 0;
var i = 0;
while ((i = this.textBox1.Text.IndexOf(
textToFind,
offset,
StringComparison.CurrentCultureIgnoreCase)) > 0)
{
indicies.Add(i);
offset = (i + textToFind.Length);
}
return indicies;
}
}
它正确地查找每个索引,并正确地选择它们
我会考虑用我写的方法在前面找到所有的指示符,然后按要求简单地迭代它们。在我的例子中,我使用一个消息框来确定何时移动到下一个索引,但您将使用不同的方法
Here is a set of text
and I'm going to find the word text
Even when there are multiple lines of text.