WordPress Hosting
Free Photos
WordPress Themes

Scroll Utils

/** Made by Lonami Exo * 31 - January - 2015 * (C) LonamiWebs - */ using System; using System.Runtime.InteropServices; using System.Windows.Forms; public static class ScrollUtils { #region Consts and externs const int EM_LINESCROLL = 0x00B6; [DllImport("user32.dll")] static extern int SetScrollPos(IntPtr hWnd, int nBar, int nPos, bool bRedraw); [DllImport("user32.dll")] static extern int SendMessage(IntPtr hWnd, int wMsg, int wParam, int lParam); [DllImport("user32.dll")] static extern int GetScrollPos(IntPtr hWnd, int nBar); #endregion #region Public static methods /// <summary> /// Sets the given scroll position to the desired TextBox /// </summary> /// <param name="tb">The TextBox</param> /// <param name="position">The new scroll position</param> /// <param name="orientation">The scroll orientation</param> public static void SetScrollPosition(TextBox tb, int position, Orientation orientation = Orientation.Vertical) { var handle = GetHandle(tb); int add = position - GetScrollPosition(tb, orientation); SetScrollPos(handle, (int)orientation, add, true); // this work w/o this but... SendMessage(handle, EM_LINESCROLL, 0, add); } /// <summary> /// Adds (or substracts) the given scroll position to the desired TextBox /// </summary> /// <param name="tb">The TextBox</param> /// <param name="position">The scroll position to be added</param> /// <param name="orientation">The scroll orientation</param> public static void AddScrollPosition(TextBox tb, int position, Orientation orientation = Orientation.Vertical) { var handle = GetHandle(tb); SetScrollPos(handle, (int)orientation, position, true); // this work w/o this but... SendMessage(handle, EM_LINESCROLL, 0, position); } /// <summary> /// Gets the current scroll position from the specified TextBox /// </summary> /// <param name="tb">The TextBox</param> /// <param name="orientation">The scroll orientation</param> /// <returns></returns> public static int GetScrollPosition(TextBox tb, Orientation orientation = Orientation.Vertical) { var handle = GetHandle(tb); return GetScrollPos(handle, (int)orientation); } static IntPtr GetHandle(TextBox tb) { IntPtr handle = IntPtr.Zero; if (tb.InvokeRequired) { tb.Invoke(new MethodInvoker(() => handle = tb.Handle)); } else handle = tb.Handle; return handle; } #endregion }
- Mommy, I want to scroll a TextBox
- But you can't do that, soon

NOW YOU DO! Yes, with some code took from P/Invoke, you can scroll any textbox to the desired location, even retrieve where is the scroll at!

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.