<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Bitmatic &#187; Scroll</title>
	<atom:link href="http://bitmatic.com/tag/scroll/feed" rel="self" type="application/rss+xml" />
	<link>http://bitmatic.com</link>
	<description>Defragmenting a .NET programmers brain</description>
	<lastBuildDate>Tue, 13 Jul 2010 13:15:58 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Fixing a slow scrolling DataGridView</title>
		<link>http://bitmatic.com/c/fixing-a-slow-scrolling-datagridview</link>
		<comments>http://bitmatic.com/c/fixing-a-slow-scrolling-datagridview#comments</comments>
		<pubDate>Sun, 25 Oct 2009 12:14:43 +0000</pubDate>
		<dc:creator>Jakob</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[controls]]></category>
		<category><![CDATA[DataGridView]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[Scroll]]></category>
		<category><![CDATA[VS2008]]></category>

		<guid isPermaLink="false">http://bitmatic.com/?p=476</guid>
		<description><![CDATA[Whenever your C#/.NET DataGridView reaches a certain size, it tends to get really slow to scroll. Depending on the speed of your computer this may be more or less noticeable. In an application i did for a client this became a real problem due to a combination of lots of DataGridView cells and fairly slow [...]]]></description>
			<content:encoded><![CDATA[<p>Whenever your C#/.NET DataGridView reaches a certain size, it tends to get really slow to scroll. Depending on the speed of your computer this may be more or less noticeable. In an application i did for a client this became a real problem due to a combination of lots of DataGridView cells and fairly slow computers.<br />
Luckily the solution turned out to be simple&#8230;</p>
<h3>Turn on double buffering</h3>
<p>Turning on double buffering seems to solve the problem. Normally double buffering would only help reduce flickering, since painting is being done to an off-screen buffer, but for the DataGridView it also significantly reduces the amount of functions being called internally in the DataGridView &#8211; thus reducing processor load and increasing speed. (statistics gathered with the <a href="http://www.eqatec.com/tools/tracer">Eqatec Tracer</a>)</p>
<h3>My DataGridView doesn&#8217;t have a DoubleBuffered property !?!?</h3>
<p>For some reason Microsoft has decided to hide the DoubleBuffered property from DataGridView. Luckily you can set it anyway with reflection.<br />
<!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode">
<span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">class</span> ExtensionMethods
{
    <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">void</span> DoubleBuffered(<span class="kwrd">this</span> DataGridView dgv, <span class="kwrd">bool</span> setting)
    {
        Type dgvType = dgv.GetType();
        PropertyInfo pi = dgvType.GetProperty(<span class="str">"DoubleBuffered"</span>,
            BindingFlags.Instance | BindingFlags.NonPublic);
        pi.SetValue(dgv, setting, <span class="kwrd">null</span>);
    }
}</pre>
<p>Just drop the above class into your project somewhere, or add the function to your existing extension methods.<br />
The extension method allows you to set the DoubleBuffered property on your DataGridView in the following manner:<br />
<!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode">
dataGridView1.DoubleBuffered(<span class="kwrd">true</span>);</pre>
<p>You now have a smooth scrolling DataGridView <img src='http://bitmatic.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://bitmatic.com/c/fixing-a-slow-scrolling-datagridview/feed</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>Implementing a scrolling RichTextBox</title>
		<link>http://bitmatic.com/c/implementing-a-scrolling-richtextbox</link>
		<comments>http://bitmatic.com/c/implementing-a-scrolling-richtextbox#comments</comments>
		<pubDate>Sun, 10 May 2009 16:45:17 +0000</pubDate>
		<dc:creator>Jakob</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[controls]]></category>
		<category><![CDATA[native code]]></category>
		<category><![CDATA[Richtextbox]]></category>
		<category><![CDATA[Scroll]]></category>

		<guid isPermaLink="false">http://bitmatic.com/?p=283</guid>
		<description><![CDATA[It is often necessary to make a .NET RichTextBox scroll to the top or the bottom. Unfotunately there is no direct method of doing this in the class itself.
One possible solution involves using the ScrollToCaret() function of the control. This method is outlined in my first post about RichTextBox scrolling. It appears that some programmers [...]]]></description>
			<content:encoded><![CDATA[<p>It is often necessary to make a .NET RichTextBox scroll to the top or the bottom. Unfotunately there is no direct method of doing this in the class itself.<br />
One possible solution involves using the ScrollToCaret() function of the control. This method is outlined in my <a href="http://bitmatic.com/csharp/scrolling-a-c-richtextbox-when-adding-text">first post about RichTextBox scrolling</a>. It appears that some programmers have had problems with that solution though. It requires careful management of focus in the application to work under all circumstances.</p>
<h3>The ScrollingRichTextBox</h3>
<p>A far more elegant solution is to use the underlying message system to control the scroll. Using the SendMessage function it is possible to send a <a href="http://msdn.microsoft.com/en-us/library/bb787577(VS.85).aspx">WM_VSCROLL</a> message to the control that makes the control scroll without needing to worry about focus.<br />
I think the best solution is to make a small class that inherits from RichTextBox, and neatly wraps the SendMessage calls necessary to make it work. Many programmers are unfamiliar with the syntax involved, so wrapping it in a more familiar syntax is a good idea.</p>
<p>The complete class looks like this:</p>
<p><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode">
<span class="kwrd">class</span> ScrollingRichTextBox : System.Windows.Forms.RichTextBox
{
  [DllImport(<span class="str">"user32.dll"</span>, CharSet = CharSet.Auto)]
  <span class="kwrd">private</span> <span class="kwrd">static</span> <span class="kwrd">extern</span> IntPtr SendMessage(
    IntPtr hWnd,
    <span class="kwrd">uint</span> Msg,
    IntPtr wParam,
    IntPtr lParam);

  <span class="kwrd">private</span> <span class="kwrd">const</span> <span class="kwrd">int</span> WM_VSCROLL = 277;
  <span class="kwrd">private</span> <span class="kwrd">const</span> <span class="kwrd">int</span> SB_LINEUP = 0;
  <span class="kwrd">private</span> <span class="kwrd">const</span> <span class="kwrd">int</span> SB_LINEDOWN = 1;
  <span class="kwrd">private</span> <span class="kwrd">const</span> <span class="kwrd">int</span> SB_TOP = 6;
  <span class="kwrd">private</span> <span class="kwrd">const</span> <span class="kwrd">int</span> SB_BOTTOM = 7;

  <span class="kwrd">public</span> <span class="kwrd">void</span> ScrollToBottom()
  {
    SendMessage(Handle, WM_VSCROLL, <span class="kwrd">new</span> IntPtr(SB_BOTTOM), <span class="kwrd">new</span> IntPtr(0));
  }

  <span class="kwrd">public</span> <span class="kwrd">void</span> ScrollToTop()
  {
    SendMessage(Handle, WM_VSCROLL, <span class="kwrd">new</span> IntPtr(SB_TOP), <span class="kwrd">new</span> IntPtr(0));
  }

  <span class="kwrd">public</span> <span class="kwrd">void</span> ScrollLineDown()
  {
    SendMessage(Handle, WM_VSCROLL, <span class="kwrd">new</span> IntPtr(SB_LINEDOWN), <span class="kwrd">new</span> IntPtr(0));
  }

  <span class="kwrd">public</span> <span class="kwrd">void</span> ScrollLineUp()
  {
    SendMessage(Handle, WM_VSCROLL, <span class="kwrd">new</span> IntPtr(SB_LINEUP), <span class="kwrd">new</span> IntPtr(0));
  }
}</pre>
<p>Feel free to steal&#8230;</p>
<h3>How it works</h3>
<p>The class imports the SendMessage function from the user32.dll system library. I&#8217;m not gonna go deeply into that function in this post. It is insanely powerfull and deserves it&#8217;s own post someday. It allows the program to send native windows messages to the control. The message in play here is the <a href="http://msdn.microsoft.com/en-us/library/bb787577(VS.85).aspx">WM_VSCROLL</a> message that takes a parameter that tells it how to scroll. These parameters are constants that are defined in the <a href="http://doc.ddart.net/msdn/header/include/winuser.h.html">winuser.h</a> header file.</p>
<p>I have implemented four functions in this class, allowing it to scroll to the top and bottom, as well as a single line up and down.</p>
<p>With the SendMessage function imported and using <a href="http://doc.ddart.net/msdn/header/include/winuser.h.html">winuser.h</a> as a reference you could easily implement even more functionality in the class, such as pageup/pagedown (SB_PAGEUP/SB_PAGEDOWN), but i don&#8217;t want to bloat the class with that for this article. The principle is what counts here.</p>
<h3>A simple usage example</h3>
<p>For applications that require this sort of scrolling, using the ScrollingRichTextBox over the ordinary RichTextBox is the way to go. In <a href="http://bitmatic.com/csharp/scrolling-a-c-richtextbox-when-adding-text">a previous blog post</a>, i talked about hooking up to the OnTextChanged event of a RichTextBox and using ScrollToCaret() to do the scrolling. A method that has a few issues.<br />
Now, using the ScrollingRichTextBox, these issues are resolved and the complete code for that event looks like this:<br />
<!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode">
<span class="kwrd">private</span> <span class="kwrd">void</span> scrollingRichTextBox_TextChanged(<span class="kwrd">object</span> sender, EventArgs e)
{
  scrollingRichTextBox.ScrollToBottom();
}</pre>
<p>Neat huh?</p>
]]></content:encoded>
			<wfw:commentRss>http://bitmatic.com/c/implementing-a-scrolling-richtextbox/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Scrolling a C# RichTextbox when adding text</title>
		<link>http://bitmatic.com/c/scrolling-a-c-richtextbox-when-adding-text</link>
		<comments>http://bitmatic.com/c/scrolling-a-c-richtextbox-when-adding-text#comments</comments>
		<pubDate>Thu, 30 Apr 2009 10:16:53 +0000</pubDate>
		<dc:creator>Jakob</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[controls]]></category>
		<category><![CDATA[events]]></category>
		<category><![CDATA[How-to]]></category>
		<category><![CDATA[Richtextbox]]></category>
		<category><![CDATA[Scroll]]></category>

		<guid isPermaLink="false">http://bitmatic.com/?p=258</guid>
		<description><![CDATA[The need for an autoscroll property&#8230;
Tons of applications are using the .NET RichTextBox for outputting status information. You have a RichTextBox that you just keep on adding text to. This is all very simple, but when the box is completely filled with text, further text added will no longer be visible, since the RichTextBox does [...]]]></description>
			<content:encoded><![CDATA[<h3>The need for an autoscroll property&#8230;</h3>
<p>Tons of applications are using the .NET RichTextBox for outputting status information. You have a RichTextBox that you just keep on adding text to. This is all very simple, but when the box is completely filled with text, further text added will no longer be visible, since the RichTextBox does not scroll along with the text. Wouldn&#8217;t it be nice if you could make it scroll along with the text automatically.</p>
<p>Well&#8230; You can.</p>
<h3>Use the TextChanged event</h3>
<p>Whenever you add (or change in any way) text to the RichTextBox the TextChanged event will fire immediately after the text has changed. Just double-click on the TextChanged event in the RichTextBox&#8217; event list to add an event handler.<br />
Now modify the code of the event handler to look like this:<br />
<!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode">
<span class="kwrd">private</span> <span class="kwrd">void</span> richTextBox_TextChanged(<span class="kwrd">object</span> sender, EventArgs e)
{
  richTextBox.SelectionStart = richTextBox.Text.Length;
  richTextBox.ScrollToCaret();
}</pre>
<p>This code uses the ScrollToCaret function to do the actual scrolling. The function will adjust the scroll of the box, so that the &#8220;caret&#8221; is visible (think of the caret as the blinking cursor). This of course requires that the caret is placed somewhere meaningfull. This is done by setting the SelectionStart property to the last character in the box. The result is that whenever the text in the RichTextBox changes the box will scroll to the very bottom.</p>
<p>While this method is perfect for your basic status information box, where information is always added to the bottom, it may not always be the proper solution. This method will reset any selection the user has made in the RichTextBox, so if you need to copy text out of the box and the text changes very often this is not really the way to go.</p>
<p>On the other hand it is a very simple and safe way of implementing &#8220;autoscroll&#8221; and i have used it in a lot of applications without any problems.</p>
]]></content:encoded>
			<wfw:commentRss>http://bitmatic.com/c/scrolling-a-c-richtextbox-when-adding-text/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
