C# 有没有办法突出显示文本框中的特定文本?
Windows Phone问题 C 我有一个名为搜索的文本框和另一个名为内容的文本框。如果用户将文本添加到内容文本框中,是否有办法使用搜索文本框搜索用户在内容文本框中键入的内容并突出显示该特定文本 e、 g.当用户想要从手机的应用程序列表中搜索应用程序时,它会突出显示并显示该特定应用程序或包含该文本的任何应用程序 如果有人有办法C# 有没有办法突出显示文本框中的特定文本?,c#,search,windows-phone-8,C#,Search,Windows Phone 8,Windows Phone问题 C 我有一个名为搜索的文本框和另一个名为内容的文本框。如果用户将文本添加到内容文本框中,是否有办法使用搜索文本框搜索用户在内容文本框中键入的内容并突出显示该特定文本 e、 g.当用户想要从手机的应用程序列表中搜索应用程序时,它会突出显示并显示该特定应用程序或包含该文本的任何应用程序 如果有人有办法 请让我知道:要在文本框中选择文本,请提供答案,基本上: //To select all text textbox.SelectionStart = 0; textbox
请让我知道:要在文本框中选择文本,请提供答案,基本上:
//To select all text
textbox.SelectionStart = 0;
textbox.SelectionLength = textbox.Text.Length;
为了达到你的目的,你只需要运用更多的逻辑。首先从内容文本框中获取输入,并在搜索文本框中的文本中找到该值的索引。如果存在该值,因此索引大于-1,则可以设置SelectionStart和SelectionLength
更新:
我在WPF解决方案中尝试了以下代码,它正常工作,这对于Windows Phone也应该有效
SearchTextBox是一个文本框
内容是一个文本块
只是您知道代码中的所有内容:
var regex = new Regex("(" + SearchTextBox.Text + ")", RegexOptions.IgnoreCase);
if (SearchTextBox.Text.Length == 0)
{
string str = Content.Text;
Content.Inlines.Clear();
Content.Inlines.Add(str);
}
else
{
//get all the words from the 'content'
string[] substrings = regex.Split(Content.Text);
Content.Inlines.Clear();
foreach (var item in substrings)
{
//if a word from the content matches the search-term
if (regex.Match(item).Success)
{
//create a 'Run' and add it to the TextBlock
Run run = new Run(item);
run.Foreground = Brushes.Red;
Content.Inlines.Add(run);
}
else //if no match, just add the text again
Content.Inlines.Add(item);
}
}
看看TextBlock和Hey Abbas的多个RunsDuplicate,谢谢,但我似乎无法让它工作。我试着在一个按钮点击事件中添加它,但什么也没发生,甚至试着添加一个焦点方法来聚焦文本框,然后进行选择,但仍然没有成功。有什么想法吗?请查看我编辑的答案,以了解其他方法:非常感谢。我从这里得到的。
var regex = new Regex("(" + SearchTextBox.Text + ")", RegexOptions.IgnoreCase);
if (SearchTextBox.Text.Length == 0)
{
string str = Content.Text;
Content.Inlines.Clear();
Content.Inlines.Add(str);
}
else
{
//get all the words from the 'content'
string[] substrings = regex.Split(Content.Text);
Content.Inlines.Clear();
foreach (var item in substrings)
{
//if a word from the content matches the search-term
if (regex.Match(item).Success)
{
//create a 'Run' and add it to the TextBlock
Run run = new Run(item);
run.Foreground = Brushes.Red;
Content.Inlines.Add(run);
}
else //if no match, just add the text again
Content.Inlines.Add(item);
}
}