2010-12-18 42 views
1

我有一個文本框(textBox1作爲例子),我週期性地追加一個字符(每秒左右)。我希望這樣做的目的是當文本太長以至於無法放入文本框時,溢出實際上會朝文本框的左側移動,即使它沒有被聚焦而不是右側的正常行爲,這樣最後一個字符將始終顯示。 我做它的方式是,當我加入的角色我也這樣做:TextBox ScrollToHorizo​​ntalOffset不會滾動文字足夠長

textBox1.ScrollToHorizontalOffset(textBox1.GetRectFromCharacterIndex(textBox1.Text.Length).Right); 

這很好地工作,直到文本的長度得到足夠長的時間。在這一點之後,滾動將停止,當前的左側溢出將保留,但新的溢出將會到達文本框的右側。 爲了測試我寫了當前文本的長度和textBox1.GetRectFromCharacterIndex(textBox1.Text.Length).Right對每次更新控制檯:

... 
Length: 22 
Rect.Right: 81.99 
Length: 23 
Rect.Right: 85.4 
Length: 24 
Rect.Right: 88.81 
Length: 25 
Rect.Right: 91.41 
Length: 26 
Rect.Right: 91.41 
Length: 27 
Rect.Right: 91.4099999999999 
Length: 28 
Rect.Right: 91.4099999999999 
Length: 29 
Rect.Right: 91.4099999999999 
Length: 30 
Rect.Right: 91.4099999999999 
... 
Length: 47 
Rect.Right: 91.4099999999999 
Length: 48 
Rect.Right: 91.4099999999999 
Length: 49 
Rect.Right: 91.4099999999999 
Length: 50 
Rect.Right: 91.4099999999999 
Length: 51 
Rect.Right: 91.4099999999999 
Length: 52 
Rect.Right: 92.88 
Length: 53 
Rect.Right: 94.8199999999999 
Length: 54 
Rect.Right: 96.29 
Length: 55 
Rect.Right: 98.2299999999999 
Length: 56 
Rect.Right: 99.7 
Length: 57 
Rect.Right: 101.64

Text.Length之前爲25的文本框仍然很大足以適應一切。在這一點之後,文本不再適合,滾動工作,直到長度爲52.

回答

3

這種行爲是奇怪的 - 我再現相同。

我得到了你所用,而不是使用

textbox1.ScrollToHorizontalOffset(double.MaxValue); 

既然要滾動的所有權利的方式看效果,存在通過比字符的實際位置更大的值沒有壞處。

0

以上所有內容對我來說都不可靠。在做別的事情時,我偶然發現了這種方法(使用選擇突出顯示)。 TB是文本框,是所需偏移量的字符索引。

TB.Focus(); //must be used 
TB.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Right; 
TB.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Left; 
TB.Select(0, ofs); //select up through desired offset 
TB.Select(0, 0);  //turn off selection 

...希望這有助於!

相關問題