<?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>Code Corner &#187; winform</title>
	<atom:link href="http://CodeCorner.galanter.net/tag/winform/feed/" rel="self" type="application/rss+xml" />
	<link>http://CodeCorner.galanter.net</link>
	<description>ASP.NET, XML, SQL and Javascript tips and tricks</description>
	<lastBuildDate>Tue, 20 Jul 2010 18:52:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Serving image from ASP.NET MSChart to external applications via WebService</title>
		<link>http://CodeCorner.galanter.net/2010/01/06/serving-mschart-via-webservice/</link>
		<comments>http://CodeCorner.galanter.net/2010/01/06/serving-mschart-via-webservice/#comments</comments>
		<pubDate>Wed, 06 Jan 2010 21:37:33 +0000</pubDate>
		<dc:creator>Yuriy</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[New Stuff]]></category>
		<category><![CDATA[chart]]></category>
		<category><![CDATA[cool]]></category>
		<category><![CDATA[graphics]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[winform]]></category>

		<guid isPermaLink="false">http://CodeCorner.galanter.net/?p=829</guid>
		<description><![CDATA[I have an existing ASP.NET application that uses Microsoft Charting control for .NET. I created a CCharting class that hold several methods related to getting data for the chart, applying chart appearance etc. Main method of that class is Public Sub DrawChart(ByVal i_omsChart As Chart, ByVal i_iChartWidth As Integer, ByVal i_iChartHeight As Integer) As a [...]]]></description>
			<content:encoded><![CDATA[<p>I have an existing ASP.NET application that uses <a href="http://codecorner.galanter.net/2009/06/16/free-alternative-to-dundas-charts-from-microsoft/">Microsoft Charting control for .NET</a>. I created a <strong>CCharting</strong> class that hold several methods related to getting data for the chart, applying chart appearance etc. Main method of that class is</p>
<pre class="brush: vb;">Public Sub DrawChart(ByVal i_omsChart As Chart, ByVal i_iChartWidth As Integer, ByVal i_iChartHeight As Integer)</pre>
<p>As a 1st parameter it accepts actual chart control from the page, 2nd and 3rd are chart width and height. The method then gets the data for the chart, binds chart to that data, applies chart appearance (colors, series, axises) etc. So drawing a chart is a simple as instantiating the class and calling the method:</p>
<pre class="brush: vb;">Dim oCharting As New CCharting
CCharting.DrawChart(xmsChart,500,300)</pre>
<p>where <strong>xmsChart</strong> is a chart control from HTML markup of the page. The result is displayed on the page:</p>
<p><img src="http://www.galanter.net/sitepic/WebChart.png" alt="" /></p>
<p>But now I needed to give access to that chart to external applications, that do not have access neither to chart data nor to Microsoft charting control, may run under different OS's, be Web apps or not. <span id="more-829"></span>The solution I came up with is to serve chart image via Web Service.</p>
<p>Since <strong>CCharting</strong> class already had all the chart drawing functionality, I decided to reuse it. A WebService was created with <strong>GetChartImage</strong> method:</p>
<pre class="brush: vb;">&lt;WebMethod()&gt; _
Public Function GetChartImage(ByVal i_iChartWidth As Integer, ByVal i_iChartHeight As Integer) As Byte()
   Dim oCharting As New CCharting
   Dim omsChart As New Chart
   Dim oStream As New IO.MemoryStream()  

   oCharting.DrawChart(omsChart, i_iChartWidth, i_iChartHeight)
   omsChart.SaveImage(oStream, ChartImageFormat.Png)

   Return oStream.ToArray

End Function</pre>
<p>It's very simple. The method accepts chart width and height as parameters and returns chart image in form of a byte array. Notice <em>Line 4</em> - instead of hard-coded chart control from HTML markup a chart object is created programmaticaly. <strong>DrawChart</strong> method accept it and draws chart on it (<em>Line 7</em>). Then image is saved in PNG format into memory stream (<em>Line 8</em>). And finally the stream is converted to byte array to be returned as a result (<em>Line 10</em>).</p>
<p>An example of using this Web Service in an external application is a WinForm app. A very basic VB.NET winform app was created with a single PictureBox control (<em>named xpicChart</em>) on the form. A reference to WebService above was added (<em>namespace named WebApi)</em>. After that loading image into PictureBox takes just 3 lines of code:</p>
<pre class="brush: vb;">Dim oWebApi As New WebApi.CWebServiceSoapClient
Dim aImage As Byte() = oWebApi.GetChartImage(xpicChart.Width, xpicChart.Height)
xpicChart.Image = Image.FromStream(New IO.MemoryStream(aImage))</pre>
<p>1st line creates a WebService client object, 2nd calls the method and retrieves image as a byte array and 3rd loads image into the picture box by converting the byte array into memory stream. The result:</p>
<p><img src="http://www.galanter.net/sitepic/WinChart.png" alt="" />
<div id="crp_related">
<h4>Related Posts:</h4>
<ul>
<li><a href="http://CodeCorner.galanter.net/2009/12/24/image-storage-location-chart35/" rel="bookmark" class="crp_title">Microsoft Chart for ASP.NET 3.5: Correctly setting image storage location</a></li>
<li><a href="http://CodeCorner.galanter.net/2010/04/26/asp-net-chart-control-is-not-rendering-image/" rel="bookmark" class="crp_title">ASP.NET Chart Control is not rendering image</a></li>
</ul>
</div>
]]></content:encoded>
			<wfw:commentRss>http://CodeCorner.galanter.net/2010/01/06/serving-mschart-via-webservice/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Delaying SelectedIndexChanged event of ComboBox in VB.NET WinForm</title>
		<link>http://CodeCorner.galanter.net/2010/01/06/delaying-selectedindexchanged-event/</link>
		<comments>http://CodeCorner.galanter.net/2010/01/06/delaying-selectedindexchanged-event/#comments</comments>
		<pubDate>Wed, 06 Jan 2010 20:27:53 +0000</pubDate>
		<dc:creator>Yuriy</dc:creator>
				<category><![CDATA[VB.NET]]></category>
		<category><![CDATA[trick]]></category>
		<category><![CDATA[winform]]></category>

		<guid isPermaLink="false">http://CodeCorner.galanter.net/?p=821</guid>
		<description><![CDATA[A standard situation - ComboBox control of VB.NET WinForm is populated with some data in Form_Load event and index is set to -1 so no item is selected: Private Sub xfrm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load xCombo.DataSource = GetSomeData() xCombo.SelectedIndex = -1 End Sub Then, when user actually selects an item [...]]]></description>
			<content:encoded><![CDATA[<p>A standard situation - ComboBox control of VB.NET WinForm is populated with some data in Form_Load event and index is set to -1 so no item is selected:</p>
<pre class="brush: vb;">Private Sub xfrm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
   xCombo.DataSource = GetSomeData()
   xCombo.SelectedIndex = -1
End Sub</pre>
<p>Then, when user actually selects an item - SelectedIndexChanged event handler is called to perform action on the selected item:</p>
<pre class="brush: vb;">Private Sub xCombo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles xCombo.SelectedIndexChanged
   PerformSomeAction(xCombo.SelectedValue)
End Sub</pre>
<p>The problem with this approach is when data is bound in Form_Load - SelectedIndexChanged event handler is also called, and when SelectedIndex is set to -1, SelectedIndexChanged event is called again. Since we expect the SelectedIndexChanged to run only when user selects an item - this behavior may cause unwanted consequences.</p>
<p>The solution is<span id="more-821"></span> to remove Handles VB.NET keyword from event handler declaration:</p>
<pre class="brush: vb;">Private Sub xCombo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
   PerformSomeAction(xCombo.SelectedValue)
End Sub</pre>
<p>and programmaticaly assign even handler to the combobox event:</p>
<pre class="brush: vb;">Private Sub xfrm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
   xCombo.DataSource = GetSomeData()
   xCombo.SelectedIndex = -1
   AddHandler xCombo.SelectedIndexChanged, AddressOf xCombo_SelectedIndexChanged
End Sub</pre>
<p>In this case when data is bound to the combobox and SelectedIndex is assigned a value - no event handler is associated with SelectedIndexChanged event. And only after those actions are performed - event handler is added, so the next time user selects an item action is performed.
<div id="crp_related">
<h4>Related Posts:</h4>
<ul>
<li><a href="http://CodeCorner.galanter.net/2009/09/30/resize-iframe-after-content-has-been-resized/" rel="bookmark" class="crp_title">Resize IFRAME after content has been resized</a></li>
<li><a href="http://CodeCorner.galanter.net/2009/07/13/infragistics-drag-and-drop-detecting-element-during-drag/" rel="bookmark" class="crp_title">Infragistics Drag and Drop: detecting element during drag</a></li>
</ul>
</div>
]]></content:encoded>
			<wfw:commentRss>http://CodeCorner.galanter.net/2010/01/06/delaying-selectedindexchanged-event/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
