Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# UWP文本框-关注选择_C#_.net_String_Textbox_Uwp - Fatal编程技术网

C# UWP文本框-关注选择

C# UWP文本框-关注选择,c#,.net,string,textbox,uwp,C#,.net,String,Textbox,Uwp,我有一个包含大量文本的.NET UWP文本框,我想在其中搜索一个单词。当我点击按钮开始搜索时,它会找到这个单词的第一个匹配项。当我再次单击时,它将找到第二个(如记事本中的ctrl+f) 我想把注意力集中在发现的世界上,但是当文本足够长,以至于有一个滚动条时,它就不会把发现的单词带到视图中 这是在这种状态下的屏幕截图,显示了如何调整窗口大小以查看找到的单词 这是我的搜索代码(textarea类型为TextBox): 我已经尝试过将textarea.Focus(FocusState.program

我有一个包含大量文本的.NET UWP文本框,我想在其中搜索一个单词。当我点击按钮开始搜索时,它会找到这个单词的第一个匹配项。当我再次单击时,它将找到第二个(如记事本中的ctrl+f)

我想把注意力集中在发现的世界上,但是当文本足够长,以至于有一个滚动条时,它就不会把发现的单词带到视图中

这是在这种状态下的屏幕截图,显示了如何调整窗口大小以查看找到的单词

这是我的搜索代码(
textarea
类型为TextBox):

我已经尝试过将
textarea.Focus(FocusState.programmabled)放在以及
textarea.Focus(FocusState.Pointer),但两者都没有帮助

更新:

我发现它的聚焦正确,但指向最后找到的单词(定位,在找到下一个单词之前光标在哪里),而不是当前找到的单词


因此,我需要将焦点更新到当前的
选择开始
,而不是最后一次。有什么想法吗?我已经尝试过再次更改
选择开始
,替换文本并更新布局-没有任何帮助。

您可以做的是测量文本的高度直到索引,并相应地调整文本框的大小

private static float GetTextHeightUntilIndex(TextBox textBox, int index)
    {
        var height = 0;
        var textBuffer = textBox.Text;

        // Remove everything after `index` in order to measure its size
        textBox.Text = textBuffer.Substring(0, index);
        textBox.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
        var height = textBox.DesiredSize().Height;

        // Put the full text back
        textBox.Text = textBuffer;

        return height;
    }

private void Find(string text)
    {
        textarea.Focus(FocusState.Programmatic);
        var start = textarea.SelectionStart + textarea.SelectionLength;
        var found =  (bool)checkboxFindCaseSensitive.IsChecked ? textarea.Text.IndexOf(text, start) : textarea.Text.IndexOf(text, start, StringComparison.CurrentCultureIgnoreCase);
        if (found == -1)
        {
            textarea.SelectionStart = 0;
            found = (bool)checkboxFindCaseSensitive.IsChecked ? textarea.Text.IndexOf(text, start) : textarea.Text.IndexOf(text, start, StringComparison.CurrentCultureIgnoreCase);
            if (found == -1) return;
        }
        textarea.SelectionStart = found;
        textarea.SelectionLength = text.Length;

        // -------------------

        var cursorPosInPx = GetTextHeightUntilIndex(textarea, found);

        // First method: resize your textbox to the selected word
        textarea.Height = cursorPosInPx; 

        // Second method: scroll the textbox
        var grid = (Grid)VisualTreeHelper.GetChild(textarea, 0);
        for (var i = 0; i <= VisualTreeHelper.GetChildrenCount(grid) - 1; i++)
        {
            object obj = VisualTreeHelper.GetChild(grid, i);
            if (obj is ScrollViewer)
                ((ScrollViewer)obj).ChangeView(null, cursorPosInPx, null, true);
        }
    }
private静态浮点getTextHeightUntinLindex(TextBox TextBox,int index)
{
var高度=0;
var textBuffer=textBox.Text;
//删除'index'之后的所有内容以测量其大小
textBox.Text=textBuffer.Substring(0,索引);
textBox.Measure(新大小(Double.PositiveInfinity,Double.PositiveInfinity));
var height=textBox.DesiredSize().height;
//把全文放回去
Text=textBuffer;
返回高度;
}
私有void查找(字符串文本)
{
textarea.Focus(FocusState.prographic);
var start=textarea.SelectionStart+textarea.SelectionLength;
var found=(bool)checkboxFindCaseSensitive.IsChecked?textarea.Text.IndexOf(Text,start):textarea.Text.IndexOf(Text,start,StringComparison.CurrentCultureIgnoreCase);
如果(找到==-1)
{
textarea.SelectionStart=0;
found=(bool)checkboxFindCaseSensitive.IsChecked?textarea.Text.IndexOf(Text,start):textarea.Text.IndexOf(Text,start,StringComparison.CurrentCultureIgnoreCase);
如果(找到==-1)返回;
}
textarea.SelectionStart=已找到;
textarea.SelectionLength=text.Length;
// -------------------
var cursorPosInPx=getTextHeightUntinLindex(textarea,已找到);
//第一种方法:将文本框调整为所选单词
textarea.Height=cursorPosInPx;
//第二种方法:滚动文本框
var grid=(grid)visualtreeheloper.GetChild(textarea,0);

对于(var i=0;i来说,您可以做的是测量文本的高度直到索引,并相应地调整文本框的大小

private static float GetTextHeightUntilIndex(TextBox textBox, int index)
    {
        var height = 0;
        var textBuffer = textBox.Text;

        // Remove everything after `index` in order to measure its size
        textBox.Text = textBuffer.Substring(0, index);
        textBox.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
        var height = textBox.DesiredSize().Height;

        // Put the full text back
        textBox.Text = textBuffer;

        return height;
    }

private void Find(string text)
    {
        textarea.Focus(FocusState.Programmatic);
        var start = textarea.SelectionStart + textarea.SelectionLength;
        var found =  (bool)checkboxFindCaseSensitive.IsChecked ? textarea.Text.IndexOf(text, start) : textarea.Text.IndexOf(text, start, StringComparison.CurrentCultureIgnoreCase);
        if (found == -1)
        {
            textarea.SelectionStart = 0;
            found = (bool)checkboxFindCaseSensitive.IsChecked ? textarea.Text.IndexOf(text, start) : textarea.Text.IndexOf(text, start, StringComparison.CurrentCultureIgnoreCase);
            if (found == -1) return;
        }
        textarea.SelectionStart = found;
        textarea.SelectionLength = text.Length;

        // -------------------

        var cursorPosInPx = GetTextHeightUntilIndex(textarea, found);

        // First method: resize your textbox to the selected word
        textarea.Height = cursorPosInPx; 

        // Second method: scroll the textbox
        var grid = (Grid)VisualTreeHelper.GetChild(textarea, 0);
        for (var i = 0; i <= VisualTreeHelper.GetChildrenCount(grid) - 1; i++)
        {
            object obj = VisualTreeHelper.GetChild(grid, i);
            if (obj is ScrollViewer)
                ((ScrollViewer)obj).ChangeView(null, cursorPosInPx, null, true);
        }
    }
private静态浮点getTextHeightUntinLindex(TextBox TextBox,int index)
{
var高度=0;
var textBuffer=textBox.Text;
//删除'index'之后的所有内容以测量其大小
textBox.Text=textBuffer.Substring(0,索引);
textBox.Measure(新大小(Double.PositiveInfinity,Double.PositiveInfinity));
var height=textBox.DesiredSize().height;
//把全文放回去
Text=textBuffer;
返回高度;
}
私有void查找(字符串文本)
{
textarea.Focus(FocusState.prographic);
var start=textarea.SelectionStart+textarea.SelectionLength;
var found=(bool)checkboxFindCaseSensitive.IsChecked?textarea.Text.IndexOf(Text,start):textarea.Text.IndexOf(Text,start,StringComparison.CurrentCultureIgnoreCase);
如果(找到==-1)
{
textarea.SelectionStart=0;
found=(bool)checkboxFindCaseSensitive.IsChecked?textarea.Text.IndexOf(Text,start):textarea.Text.IndexOf(Text,start,StringComparison.CurrentCultureIgnoreCase);
如果(找到==-1)返回;
}
textarea.SelectionStart=已找到;
textarea.SelectionLength=text.Length;
// -------------------
var cursorPosInPx=getTextHeightUntinLindex(textarea,已找到);
//第一种方法:将文本框调整为所选单词
textarea.Height=cursorPosInPx;
//第二种方法:滚动文本框
var grid=(grid)visualtreeheloper.GetChild(textarea,0);

对于(var i=0;我可以使用RicheditboxI want Textbox。可以使用RicheditboxI want Textbox。它没有帮助,而且对于长文本来说太慢。有什么方法,如何通过滚动来解决它?检测所选单词的位置并手动滚动到该位置?没有,它也没有帮助。我发现了一些有趣的东西,请检查我的更新:什么没有帮助?你应该这样做使用调试器检查某些变量的状态,然后查看问题最终出现的位置!它不起作用,因为在GetTextHeightUntilIndex之前无法进行选择,所以出现了问题。这非常有效。要滚动到选择,只需将ScrollViewer的垂直偏移设置为GetTextHeightUntilIndex()返回的高度它没有帮助,而且对于长文本来说太慢。有什么办法,如何通过滚动来解决它吗?检测所选单词的位置并手动滚动到该位置?没有,它也没有帮助。我发现了一些有趣的事情,检查我的更新:什么没有帮助?你应该使用调试器来检查一些变量的状态,然后看看问题出在哪里它终于起作用了!它不起作用,因为在GetTextHeightUntilIndex之前不能进行选择,所以出现了问题。这非常有效。要滚动到选择,只需将ScrollViewer的垂直偏移设置为GetTextHeightUntilIndex()返回的高度