开发者

How to automatically show beginning of text in TextBox

开发者 https://www.devze.com 2023-01-16 12:44 出处:网络
I have very long text and put it into TextBox. I want to display automatically the beginning of the text not the end. But TextBox automatically show the end of my text.

I have very long text and put it into TextBox. I want to display automatically the beginning of the text not the end. But TextBox automatically show the end of my text.

What can I do to achieve it.

I use SelectionStart method to put cursor at the beginning of text in TextBox in order to implement some simple IntelliSense so preferred solution开发者_高级运维 would not use methods that move cursor.


You could always initially put a smaller value in the textbox then upon your criteria for displaying the full text append the remaining portion of the full text.

Example:

textBox.text = someString.Substring(0, x);

then when needed do

textBox.AppendText(someString.Substring(x+1));


I assume you are using WinForms.


Update: Strange. The struck-out code below workes as described if executed in the form constructor, but not later in the form lifecycle (e.g. a button click handler).

Note that if you have already used SelectionStart to put the cursor at the beginning of the text (e.g., via textBox.SelectionStart = 0;), then all that needs follow is textBox.ScrollToCaret();.


Consider using the textBox.AppendText(someLongString) method when adding text to your text box instead of textBox.Text = someLongString.

If you must wipe out the current text before assigning the new text, use textBox.Text = string.Empty; followed by a call to textBox.AppendText();


You could use owner-draw to override the rendering of the textbox when it doesn't have the input focus. That would trivially give you complete control of what it shows when, without breaking any of the actual editing functionality of the textbox by trying to hack it.


You can send a Win32 scroll message to the underlying textbox handle via P/Invoke:

[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);

// From User32.dll
private const int WM_VSCROLL = 277;
private const int SB_TOP = 6;

SendMessage(yourTextBox.Handle, WM_VSCROLL, (IntPtr)SB_TOP, IntPtr.Zero);
0

精彩评论

暂无评论...
验证码 换一张
取 消