This is possible similar to the way I did it for the ListBox. However, it has very little practical value for a TextBox. Its scrolling behavior is controlled by the caret position, available as the SelectionStart property. You'd use the ScrollToCaret() method to force the TextBox to scroll to make the caret visible.
I have a multiline textbox in C# and am trying to use the Insert() and Delete() methods. Whenever I do, the textbox reloads and scrolls to the first line in the textbox. If I use the SelectionStart property and then the ScrollToCaret() method after using Insert() or Delete() the first line of the textbox is different than before I made the call causing the display to seem to move to the user. How can I keep the same line at the top of the textbox after using Insert() or Delete()
You'll have to P/Invoke the GetScrollInfo() API function to find out where the thumb is at. Try this:
using System.Runtime.InteropServices; ... public static double GetScrollPercentage(TextBoxBase ctl) { SCROLLINFO info = new SCROLLINFO(); info.cbSize = Marshal.SizeOf(info); info.fMask = 0x17; GetScrollInfo(ctl.Handle, 1, ref info); if (info.nMax <= info.nPage + 1) return 100; return 100.0 * info.nPos / (info.nMax - info.nPage + 1); }
[DllImport("user32.dll")] private static extern bool GetScrollInfo(IntPtr hwnd, int fnBar, ref SCROLLINFO lpsi); [StructLayout(LayoutKind.Sequential)] struct SCROLLINFO { public int cbSize; public uint fMask; public int nMin; public int nMax; public uint nPage; public int nPos; public int nTrackPos; }
I've done auto scroll for the Textbox. but, I've one more question. I want to know whether thumb position is bottom or not when input to the Textbox. This problem wasn't solved using WinProc and WM_VSCROLL.
How to can I control the Scrollbar on the Textbox...??
How to can I control the Scrollbar on the Textbox...??
Michael J Brown
Amir__
I have a multiline textbox in C# and am trying to use the Insert() and Delete() methods. Whenever I do, the textbox reloads and scrolls to the first line in the textbox. If I use the SelectionStart property and then the ScrollToCaret() method after using Insert() or Delete() the first line of the textbox is different than before I made the call causing the display to seem to move to the user. How can I keep the same line at the top of the textbox after using Insert() or Delete()
connect2sandeep
using System.Runtime.InteropServices;
...
public static double GetScrollPercentage(TextBoxBase ctl) {
SCROLLINFO info = new SCROLLINFO();
info.cbSize = Marshal.SizeOf(info);
info.fMask = 0x17;
GetScrollInfo(ctl.Handle, 1, ref info);
if (info.nMax <= info.nPage + 1) return 100;
return 100.0 * info.nPos / (info.nMax - info.nPage + 1);
}
[DllImport("user32.dll")]
private static extern bool GetScrollInfo(IntPtr hwnd, int fnBar, ref SCROLLINFO lpsi);
[StructLayout(LayoutKind.Sequential)]
struct SCROLLINFO {
public int cbSize;
public uint fMask;
public int nMin;
public int nMax;
public uint nPage;
public int nPos;
public int nTrackPos;
}
arvindbksc
Gurpreet Singh Gill
Thanks, nobugz.
I've done auto scroll for the Textbox. but, I've one more question.
I want to know whether thumb position is bottom or not when input to the Textbox.
This problem wasn't solved using WinProc and WM_VSCROLL.