<?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; events</title>
	<atom:link href="http://bitmatic.com/tag/events/feed" rel="self" type="application/rss+xml" />
	<link>http://bitmatic.com</link>
	<description>Lean IT-solutions in .NET/C#</description>
	<lastBuildDate>Tue, 11 Jan 2011 09:37:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Redirecting MouseWheel events to another control</title>
		<link>http://bitmatic.com/c/redirecting-mousewheel-events-to-another-control</link>
		<comments>http://bitmatic.com/c/redirecting-mousewheel-events-to-another-control#comments</comments>
		<pubDate>Sun, 01 Nov 2009 12:42:36 +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[native code]]></category>

		<guid isPermaLink="false">http://bitmatic.com/?p=478</guid>
		<description><![CDATA[Redirecting events from one control to another is quite easy. I recently needed it for an application that uses a number of ComboBoxes to filter data in a DataGridView. It was very confusing to the user when she selected an item in the ComboBox and subsequently tried using the mouse wheel to scroll through the [...]]]></description>
			<content:encoded><![CDATA[<p>Redirecting events from one control to another is quite easy. I recently needed it for an application that uses a number of ComboBoxes to filter data in a DataGridView. It was very confusing to the user when she selected an item in the ComboBox and subsequently tried using the mouse wheel to scroll through the data, that it would be the ComboBox, and not the DataGridView that would be scrolling.<br />
I decided to make a special ComboBox that would redirect Mouse wheel events to the DataGridView, instead of trying to fix it by manipulating focus. Manipulating focus gives you nothing but problems in my experience.</p>
<h3>The ComboBox with MouseWheel redirect</h3>
<p>The class i implemented for this solution just overrides the WndProc function, which handles all the messages sent to the control. It then reroutes mouse wheel events to another control (the reciever) through the Win32 API function SendMessage.<br />
That&#8217;s it&#8230;<br />
<!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode">
<span class="kwrd">public</span> <span class="kwrd">class</span> ComboBoxMWRedirect : ComboBox
{
    [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">int</span> message,
        IntPtr WParam,
        IntPtr LParam);

    <span class="kwrd">private</span> <span class="kwrd">const</span> <span class="kwrd">int</span> WM_MOUSEWHEEL = 0x020A;
    <span class="kwrd">private</span> Control _reciever;

    <span class="rem">//needed for designer support</span>
    <span class="kwrd">private</span> ComboBoxMWRedirect() {}

    <span class="kwrd">public</span> ComboBoxMWRedirect(Control reciever)
    {
        _reciever = reciever;
    }

    <span class="kwrd">protected</span> <span class="kwrd">override</span> <span class="kwrd">void</span> WndProc(<span class="kwrd">ref</span> Message message)
    {
        <span class="kwrd">if</span> (message.Msg == WM_MOUSEWHEEL)
        {
            <span class="rem">//route mouse wheel messages to the reciever</span>
            SendMessage(
                _reciever.Handle,
                message.Msg,
                message.WParam,
                message.LParam);
        }
        <span class="kwrd">else</span>
        {
            <span class="rem">//route all other messages on to the base class</span>
            <span class="kwrd">base</span>.WndProc(<span class="kwrd">ref</span> message);
        }
    }
}</pre>
<p>Now to have one of your ComboBoxes redirect the mouse wheel, just use one of these instead, and specify the recieving control when you call the constructor. If, for instance, i want to redirect to a DataGridView calle dataGridView1 i would construct the redirecting ComboBox like this:<br />
<!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode">
ComboBoxMWRedirect cb = <span class="kwrd">new</span> ComboBoxMWRedirect(dataGridView1);</pre>
<h3>Completely avoiding the event</h3>
<p>The code can also be used to avoid the Mouse wheel event altogether. Just change the WndProc overridde to the code below, and mouse wheel events will be ignored.<br />
<!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode">
<span class="kwrd">protected</span> <span class="kwrd">override</span> <span class="kwrd">void</span> WndProc(<span class="kwrd">ref</span> Message message)
{
    <span class="rem">//avoid mouse wheel events</span>
    <span class="kwrd">if</span> (message.Msg != WM_MOUSEWHEEL)
        <span class="kwrd">base</span>.WndProc(<span class="kwrd">ref</span> message);
}</pre>
<p>Overriding WndProc gives you a lot of power and control over what happens in your application. This particular (very limited) example handles MouseWheel events on a ComboBox, but you can really use this to do a lot of very powerful stuff, like disabling the keyboard or parts of it, routing messages to other controls, broadcasting messages to have several controls react to the same message, etc. etc.</p>
]]></content:encoded>
			<wfw:commentRss>http://bitmatic.com/c/redirecting-mousewheel-events-to-another-control/feed</wfw:commentRss>
		<slash:comments>1</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 [...]]]></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>

