How to follow the end of a text in a TextBox with no NoWrap?(如何在没有 NoWrap 的 TextBox 中跟随文本的结尾?)
问题描述
我在 xaml 中有一个文本框:
<文本框名称=文本";HorizontalAlignment =左"高度=75"VerticalContentAlignment =中心"TextWrapping =NoWrap"文本=文本框"宽度=336"BorderBrush =黑色"字体大小=40"/>我用这个方法给它添加文字:
private string words = "TextBox 的初始文本内容.";公共异步无效文本旋转(){for(int a =0; a < words.Length; a++){Text.Text = words.Substring(0,a);等待任务.延迟(500);}}一旦文本脱离包装,有一种方法可以聚焦结尾,使旧文本消失在左侧,新文本消失在右侧,而不是仅仅将其添加到右侧而不看到.
一种快速的方法是测量需要滚动的字符串(words)
I have a TextBox in xaml:
<TextBox Name="Text" HorizontalAlignment="Left" Height="75" VerticalContentAlignment="Center" TextWrapping="NoWrap" Text="TextBox" Width="336" BorderBrush="Black" FontSize="40" />
I add text to it with this method:
private string words = "Initial text contents of the TextBox.";
public async void textRotation()
{
for(int a =0; a < words.Length; a++)
{
Text.Text = words.Substring(0,a);
await Task.Delay(500);
}
}
Once the text goes of out of the wrap is there a way to focus the end so the old text disappears to the left and the new on the right, as opposed to just adding it to the right without seeing.
A quick method is to measure the string (words) that needs scrolling with TextRenderer.MeasureText, divide the width measure in parts equals to the number of chars in the string and use ScrollToHorizontalOffset() to perform the scroll:
public async void textRotation()
{
float textPart = TextRenderer.MeasureText(words, new Font(Text.FontFamily.Source, (float)Text.FontSize)).Width / words.Length;
for (int i = 0; i < words.Length; i++)
{
Text.Text = words.Substring(0, i);
await Task.Delay(100);
Text.ScrollToHorizontalOffset(textPart * i);
}
}
Same, but using the FormattedText class to measure the string:
public async void textRotation()
{
var textFormat = new FormattedText(
words, CultureInfo.CurrentCulture, System.Windows.FlowDirection.LeftToRight,
new Typeface(this.Text.FontFamily, this.Text.FontStyle, this.Text.FontWeight, this.Text.FontStretch),
this.Text.FontSize, null, null, 1);
float textPart = (float)textFormat.Width / words.Length;
for (int i = 0; i < words.Length; i++)
{
Text.Text = words.Substring(0, i);
await Task.Delay(200);
Text.ScrollToHorizontalOffset(textPart * i);
}
}
这篇关于如何在没有 NoWrap 的 TextBox 中跟随文本的结尾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在没有 NoWrap 的 TextBox 中跟随文本的结尾?
基础教程推荐
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
