<?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>Farooq Azam &#187; c sharp</title>
	<atom:link href="http://www.farooqazam.net/tag/c-sharp/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.farooqazam.net</link>
	<description>C# &#38; .NET Programming Blog</description>
	<lastBuildDate>Fri, 23 Jul 2010 20:15:42 +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>C# &#8211; Check if connected to internet</title>
		<link>http://www.farooqazam.net/csharp-check-if-connected-to-internet/</link>
		<comments>http://www.farooqazam.net/csharp-check-if-connected-to-internet/#comments</comments>
		<pubDate>Fri, 23 Jul 2010 10:26:31 +0000</pubDate>
		<dc:creator>Farooq Azam</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[c sharp]]></category>
		<category><![CDATA[connection]]></category>
		<category><![CDATA[internet]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.farooqazam.net/?p=274</guid>
		<description><![CDATA[I was working on an application which checked for updates. I used WebRequest/WebResponse for checking updates. It showed a continuous Progressbar while checking for updates. But there was a problem. It worked properly when you were connected to internet but it  just showed the progressbar all the time if not connected. The solution for that was [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px; margin-top: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.farooqazam.net%2Fcsharp-check-if-connected-to-internet%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.farooqazam.net%2Fcsharp-check-if-connected-to-internet%2F&amp;source=farooqaaa&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>I was working on an application which checked for updates. I used WebRequest/WebResponse for checking updates. It showed a continuous Progressbar while checking for updates. But there was a problem. It worked properly when you were connected to internet but it  just showed the progressbar all the time if not connected.</p>
<p>The solution for that was to check for internet connection before checking for updates. If connected then check for updates otherwise show an error.</p>
<p>Checking for the state of internet connection is very simple.</p>
<p>Firstly, import <em>System.Runtime.InteropServices</em> namespace. Add this to the top of your code:</p>
<pre class="brush: csharp;">using System.Runtime.InteropServices;</pre>
<p>Then use this code for checking connection state:</p>
<p><small><em>For C# Beginners: Add this code above or below public Form1() { &#8230; }</em></small></p>
<pre class="brush: csharp;">// API Method
[DllImport(&quot;wininet.dll&quot;)]
private extern static bool InternetGetConnectedState(out int Description, int ReservedValue);

// A method for checking the state
public static bool IsConnected()
{
    int Description;
    return InternetGetConnectedState(out Description, 0);
}</pre>
<p>Now you can use the above method to check for connection before doing anything that requires internet connection.</p>
<p>Example use:</p>
<pre class="brush: csharp;">// If internet connection is active
if (IsConnected())
{
    // do something
}
else
{
   MessageBox.Show(&quot;Please connect to the internet.&quot;);
</pre>
<p>If you have any questions please don&#8217;t hesitate to post a comment.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.farooqazam.net/csharp-check-if-connected-to-internet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Crop Image &#8211; C# and VB.NET</title>
		<link>http://www.farooqazam.net/crop-image-c-sharp-and-vb-net/</link>
		<comments>http://www.farooqazam.net/crop-image-c-sharp-and-vb-net/#comments</comments>
		<pubDate>Mon, 24 May 2010 04:00:00 +0000</pubDate>
		<dc:creator>Farooq Azam</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[VB.NET]]></category>
		<category><![CDATA[bitmap]]></category>
		<category><![CDATA[c sharp]]></category>
		<category><![CDATA[crop]]></category>
		<category><![CDATA[image]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.farooqazam.net/?p=239</guid>
		<description><![CDATA[For cropping an image in C# &#38; VB.NET we can use the Graphics class. We&#8217;ll use a method (or function in VB.NET) which will take 2 parameters, a source image and a rectangle of the section that should be cropped.  Let&#8217;s code! The Code C#: public Bitmap CropImage(Bitmap source, Rectangle section) { // An empty [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px; margin-top: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.farooqazam.net%2Fcrop-image-c-sharp-and-vb-net%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.farooqazam.net%2Fcrop-image-c-sharp-and-vb-net%2F&amp;source=farooqaaa&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p><a href="http://www.farooqazam.net/wp-content/uploads/2010/05/crop.gif"><img class="alignnone size-full wp-image-240" title="crop" src="http://www.farooqazam.net/wp-content/uploads/2010/05/crop.gif" alt="" width="350" height="234" /></a></p>
<p>For cropping an image in C# &amp; VB.NET we can use the <em>Graphics</em> class. We&#8217;ll use a method (or function in VB.NET) which will take 2 parameters, a source image and a rectangle of the section that should be cropped.  Let&#8217;s code!</p>
<h3>The Code</h3>
<p><strong><br />
C#:</strong></p>
<pre class="brush: csharp;">
public Bitmap CropImage(Bitmap source, Rectangle section)
{

 // An empty bitmap which will hold the cropped image
 Bitmap bmp = new Bitmap(section.Width, section.Height);

 Graphics g = Graphics.FromImage(bmp);

 // Draw the given area (section) of the source image
 // at location 0,0 on the empty bitmap (bmp)
 g.DrawImage(source, 0, 0, section, GraphicsUnit.Pixel);

 return bmp;
}

// Example use:

Bitmap source = new Bitmap(@&quot;C:\tulips.jpg&quot;);
Rectangle section = new Rectangle(new Point(12, 50), new Size(150, 150));

Bitmap CroppedImage = CropImage(source, section);
</pre>
<p><strong><br />
VB.NET</strong></p>
<pre class="brush: vb;">
Function CropImage(ByVal source As Bitmap, ByVal section As Rectangle) As Bitmap

 ' An empty bitmap which will hold the cropped image
 Dim bmp As New Bitmap(section.Width, section.Height)

 Dim g As Graphics = Graphics.FromImage(bmp)

 ' Draw the given area (section) of the source image
 ' at location 0,0 on the empty bitmap (bmp)
 g.DrawImage(source, 0, 0, section, GraphicsUnit.Pixel)

 return bmp
End Function

' Example use:

Dim source As New Bitmap(&quot;C:\tulips.jpg&quot;)
Dim section As New Rectangle(new Point(12, 50), new Size(150, 150))

Dim CroppedImage As Bitmap = CropImage(source, section)
</pre>
<h3>Explanation</h3>
<p>The above code will work perfectly but it seems confusing, doesn&#8217;t it? You might have been wondering how it works. Well&#8230;</p>
<p>We first make an empty bitmap of the same width/height as the <em>section</em> rectangle. This bitmap will hold the cropped image. We use the <em>section</em> rectangle&#8217;s width/height because it determines the size of the cropped image. Then we use the <em>Graphics</em> class&#8217;s <em>DrawImage()</em> method (function) for the cropping. It draws the given area (<em>section</em>) of the <em>source</em> image on the empty bitmap and that&#8217;s it. The <span style="text-decoration: line-through;">empty</span> bitmap now holds the cropped image.</p>
<h3>Source Code</h3>
<p><a href="http://www.farooqazam.net/source/CropImage-Csharp.zip">Download Source Code for C#</a></p>
<p><a href="http://www.farooqazam.net/source/CropImage-VB.zip">Download Source Code for VB.NET</a></p>
<p>If you have any questions, don&#8217;t hesitate to post a comment.</p>
<p>Thanks</p>
]]></content:encoded>
			<wfw:commentRss>http://www.farooqazam.net/crop-image-c-sharp-and-vb-net/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>C# &#8211; Autocomplete Textbox</title>
		<link>http://www.farooqazam.net/c-sharp-autocomplete-textbox/</link>
		<comments>http://www.farooqazam.net/c-sharp-autocomplete-textbox/#comments</comments>
		<pubDate>Sat, 15 May 2010 08:52:37 +0000</pubDate>
		<dc:creator>Farooq Azam</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[autocomplete]]></category>
		<category><![CDATA[c sharp]]></category>
		<category><![CDATA[textbox]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.farooqazam.net/?p=207</guid>
		<description><![CDATA[There are times when you&#8217;ll need an autocomplete textbox. Autocomplete textbox makes the user&#8217;s life easier. It shows suggestions as the user types. In case of a web browser, if you type &#8220;goo&#8221; it will automatically show all the suggestions starting with or containing &#8220;goo&#8221; like &#8220;Google, Goofy&#8221; etc. Enough talk! Lets get on with [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px; margin-top: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.farooqazam.net%2Fc-sharp-autocomplete-textbox%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.farooqazam.net%2Fc-sharp-autocomplete-textbox%2F&amp;source=farooqaaa&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p><a href="http://www.farooqazam.net/wp-content/uploads/2010/05/auto-complete-textbox.gif"><img class="alignnone size-full wp-image-222" title="Autocomplete textbox" src="http://www.farooqazam.net/wp-content/uploads/2010/05/auto-complete-textbox.gif" alt="" width="369" height="215" /></a></p>
<p>There are times when you&#8217;ll need an autocomplete textbox. Autocomplete textbox makes the user&#8217;s life easier. It shows suggestions as the user types. In case of a web browser, if you type &#8220;goo&#8221; it will automatically show all the suggestions starting with or containing &#8220;goo&#8221; like &#8220;Google, Goofy&#8221; etc. Enough talk! Lets get on with it.</p>
<p>Fortunately, textbox has a built-in autocomplete feature! We don&#8217;t have to do any coding.</p>
<p>You can either set it up from the Designer (if you are using Visual Studio or <a title="Sharp Develop" href="http://www.icsharpcode.net/opensource/sd/">#Develop</a>) or do it via code. It is simple both ways. Lets do it!</p>
<h3>Setting up AutoComplete</h3>
<p><a href="../wp-content/uploads/2010/05/auto-completes.gif"><img title="AutoComplete Properties" src="../wp-content/uploads/2010/05/auto-completes.gif" alt="" width="357" height="215" /></a></p>
<p>For setting up autocomplete you&#8217;ll have to use three properties of the TextBox class &#8212; AutoCompleteSource,  AutoCompleteCustomSource and AutoCompleteMode. Here&#8217;s an explanation of these properties:<span id="more-207"></span></p>
<p><strong>AutoCompleteSource</strong> is the source of the suggestions. You can assign it CustomSource if you want to use custom suggestions or use other auto sources like FileSystem, HistoryList etc. Below are all the available sources:</p>
<ul>
<li><strong>FileSystem</strong>: This will show suggestions from the file system i.e files stored on the computer. Like if you write &#8220;C:\&#8221; it&#8217;ll show all the files stored on C: drive.</li>
<li><strong>FileSystemDirectories:</strong> This is same as FileSystem but it shows directory names instead of file names.</li>
<li><strong>HistoryList:</strong> This will show URLs from Internet Explorer&#8217;s history.</li>
<li><strong>RecentlyUsedList:</strong> A list of recently used applications, folders, and URLs (from internet explorer).</li>
<li><strong>AllUrl: </strong>This specifies an equivalent of HistoryList and RecentlyUsedList as the source.</li>
<li><strong>AllSystemSources:</strong> Specifies an equivalent of AllUrl and FileSystem as the source.</li>
</ul>
<p><strong>AutoCompleteModes</strong> are of 3 types. Suggest, Append and SuggestAppend:</p>
<ul>
<li><strong>Suggest</strong> will show suggestions in a drop-down as the user types.</li>
<li><strong>Append</strong> will auto complete as you type. Like if you write &#8220;Goo&#8221; it&#8217;ll automatically make it &#8220;Google&#8221;.</li>
<li><strong>SuggestAppend</strong> will do both Suggest/Append.</li>
</ul>
<p><strong>AutoCompleteCustomSource</strong> is collection of custom suggestions. It will be used when you assign &#8220;CustomSource&#8221; as &#8220;AutoCompleteSource&#8221;.</p>
<p>Okay enough explanations. Lets make an autocomplete textbox now.</p>
<h3>Using Designer:</h3>
<p>If you are using the Designer, then all you have to do is to select  your TextBox and change those three properties (explained above). For instance, if you want to use custom suggestions then use  &#8220;CustomSource&#8221; and add the custom suggestions to  &#8220;AutoCompleteCustomSource&#8221; (don&#8217;t forget to set up AutoCompleteMode). Debug your project and see! Its that easy  <img src='http://www.farooqazam.net/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> . Don&#8217;t forget that you can also use all the other sources like FileSystem, HistoryList etc.</p>
<p><a href="http://www.farooqazam.net/wp-content/uploads/2010/05/auto-completes.gif"><img title="AutoComplete Properties" src="http://www.farooqazam.net/wp-content/uploads/2010/05/auto-completes.gif" alt="" width="357" height="215" /></a></p>
<h3>Via Coding:</h3>
<p>Here&#8217;s an example:</p>
<pre class="brush: csharp;">
// List of custom suggestions
string[] suggestions = new string[] {
&quot;Google&quot;,
&quot;Google Images&quot;,
&quot;Yahoo&quot;,
&quot;Youtube&quot;
};

// Use the AutoCompleteMode that suits you.
textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;

// Since we are using custom suggestions you
// should use this source.
// Use the other non-custom sources if you
// don't want to use custom suggestions.
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;

// And finally add the above suggestions to the CustomSource
textBox1.AutoCompleteCustomSource.AddRange(suggestions);
</pre>
<p>That&#8217;s it.</p>
<p>If you have any questions, don&#8217;t hesitate to post a comment.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.farooqazam.net/c-sharp-autocomplete-textbox/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>C# &#8211; Check if Form is already running</title>
		<link>http://www.farooqazam.net/c-sharp-check-if-form-is-already-running/</link>
		<comments>http://www.farooqazam.net/c-sharp-check-if-form-is-already-running/#comments</comments>
		<pubDate>Wed, 12 May 2010 18:02:35 +0000</pubDate>
		<dc:creator>Farooq Azam</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[c sharp]]></category>
		<category><![CDATA[check]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[running]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.farooqazam.net/?p=191</guid>
		<description><![CDATA[To check if a form is already running you&#8217;ll have to look for it in the Application.OpenForms collection. When a form is opened it is added to that collection. Here&#8217;s a boolean method that checks whether a form is open or not : private bool CheckForm(Form form) { foreach (Form f in Application.OpenForms) if (form [...]]]></description>
			<content:encoded><![CDATA[<script type="text/javascript"><!--
google_ad_client = "pub-7756739986631233";
/* 468x60, created 9/12/09 */
google_ad_slot = "1727532708";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><div class="tweetmeme_button" style="float: right; margin-left: 10px; margin-top: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.farooqazam.net%2Fc-sharp-check-if-form-is-already-running%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.farooqazam.net%2Fc-sharp-check-if-form-is-already-running%2F&amp;source=farooqaaa&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>To check if a form is already running you&#8217;ll have to look for it in the Application.OpenForms collection. When a form is opened it is added to that collection.</p>
<p>Here&#8217;s a boolean method that checks whether a form is open or not :</p>
<pre class="brush: csharp;">
private bool CheckForm(Form form)
{
   foreach (Form f in Application.OpenForms)
       if (form == f)
          return true;

 return false;
}
</pre>
<p>Example use:</p>
<pre class="brush: csharp;">
// if form2 is not running

if (!CheckForm(form2))
  {
      // do something
  }
</pre>
<p>Thanks.</p>
<p>If you have any questions, don&#8217;t hesitate to post a comment.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.farooqazam.net/c-sharp-check-if-form-is-already-running/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>C# Auto click button and auto fill form</title>
		<link>http://www.farooqazam.net/c-sharp-auto-click-button-and-auto-fill-form/</link>
		<comments>http://www.farooqazam.net/c-sharp-auto-click-button-and-auto-fill-form/#comments</comments>
		<pubDate>Wed, 17 Feb 2010 11:58:39 +0000</pubDate>
		<dc:creator>Farooq Azam</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[auto click]]></category>
		<category><![CDATA[auto fill]]></category>
		<category><![CDATA[auto search]]></category>
		<category><![CDATA[bot]]></category>
		<category><![CDATA[c sharp]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.farooqazam.net/?p=162</guid>
		<description><![CDATA[This tutorial will show you how to auto fill forms and click buttons in a website using the webBrowser control. When you learn to do this you can make your own web bots! To show you how autoclick/autofill works we&#8217;ll make a simple Google AutoSearch Bot. So lets begin&#8230; First of all, add a webBrowser [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px; margin-top: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.farooqazam.net%2Fc-sharp-auto-click-button-and-auto-fill-form%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.farooqazam.net%2Fc-sharp-auto-click-button-and-auto-fill-form%2F&amp;source=farooqaaa&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p><a title="Goolge AutoSearch Bot" href="http://www.farooqazam.net/wp-content/uploads/2010/02/da-app.gif"><img class="alignnone size-full wp-image-176" title="da-app" src="http://www.farooqazam.net/wp-content/uploads/2010/02/da-app.gif" alt="da-app" width="473" height="427" /></a></p>
<p>This tutorial will show you how to auto fill forms and click buttons in a website using the webBrowser control. When you learn to do this you can make your own web bots!</p>
<p>To show you how autoclick/autofill works we&#8217;ll make a simple Google AutoSearch Bot.</p>
<p>So lets begin&#8230;<span id="more-162"></span></p>
<p>First of all, add a <em>webBrowser</em> control to your form<em>.</em> Set its <em>&#8220;Url&#8221; </em>property to &#8220;www.google.com&#8221;.</p>
<p>Now we&#8217;ll add two methods <em>SetText()</em> and <em>ClickButton()</em>.<em> SetText()</em> method will automatically fill a textBox and the <em>ClickButton()</em> will click the submit button.</p>
<blockquote><p>Since we are making a Google AutoSearch Bot we need to find what&#8217;s the name of the Google search textBox and the submit button. To find these, visit Google.com from your browser and view the page source.</p></blockquote>
<p>Here&#8217;s the code for the SetText() method:</p>
<pre class="brush: csharp;">// Set value for the attribute that has the name (attName)

void SetText(string attribute, string attName, string value)
 {

 // Get a collection of all the tags with name &quot;input&quot;;

 HtmlElementCollection tagsCollection = webBrowser1.Document.GetElementsByTagName(&quot;input&quot;);

 foreach (HtmlElement currentTag in tagsCollection)
 {

 // If the attribute of the current tag has the name attName

   if (currentTag.GetAttribute(attribute).Equals(attName))

 // Then set its attribute &quot;value&quot;.

   currentTag.SetAttribute(&quot;value&quot;, value);
 }
}
</pre>
<p>And now the code for the ClickButton() method:</p>
<pre class="brush: csharp;">// Click the button whose attribute has a name &quot;attName&quot;

 void ClickButton(string attribute, string attName)
 {
    HtmlElementCollection col = webBrowser1.Document.GetElementsByTagName(&quot;input&quot;);

    foreach (HtmlElement element in col)
    {
      if (element.GetAttribute(attribute).Equals(attName))
      {

      // Invoke the &quot;Click&quot; member of the button
      element.InvokeMember(&quot;click&quot;);
      }
    }
 }
</pre>
<p>Now that the main methods are added we can now tell the bot what to do <img src='http://www.farooqazam.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p>Declare &#8220;bool searched = false&#8221; at Class-level (i.e add it above pulic Form1() {&#8230;&#8230;). We&#8217;ll use it to check whether we have already searched or not.</p>
<p>Add <em>DoucmentComplete</em> event for the webBrowser. DocumentComplete event is fired when the page is loaded completely.</p>
<p>Use this code:</p>
<p><em>Note: The name of the Google search textBox is &#8220;q&#8221; and the submit buttons is &#8220;btnG&#8221;.</em></p>
<pre class="brush: csharp;">
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
 {

 // Check if already searched

 if(searched == false)
 {

 // Set the text to &quot;Search Text Here&quot; of the textBox whose &quot;name&quot; attribute is &quot;q&quot;;

 SetText(&quot;name&quot;, &quot;q&quot;, &quot;Search Text Here&quot;);

 // Click the button whose name attribute is &quot;btnG&quot;

 ClickButton(&quot;name&quot;, &quot;btnG&quot;);

 // Since we have already searched, set it to true

 searched = true;

 }
 }
</pre>
<p>That&#8217;s it! Debug the project and watch the bot <img src='http://www.farooqazam.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>If you have any questions don&#8217;t hesitate to post a comment.</p>
<p>Thanks</p>
]]></content:encoded>
			<wfw:commentRss>http://www.farooqazam.net/c-sharp-auto-click-button-and-auto-fill-form/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>Convert color to hex using C#</title>
		<link>http://www.farooqazam.net/convert-color-to-hex-using-c-sharp/</link>
		<comments>http://www.farooqazam.net/convert-color-to-hex-using-c-sharp/#comments</comments>
		<pubDate>Tue, 29 Dec 2009 17:50:44 +0000</pubDate>
		<dc:creator>Farooq Azam</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[c sharp]]></category>
		<category><![CDATA[color]]></category>
		<category><![CDATA[convert]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.farooqazam.net/?p=155</guid>
		<description><![CDATA[So you want convert a .NET color to hex or a hex color to .NET color. Well, its very easy to do this. We won&#8217;t have to write lots of code or a special class for it. Thanks to ColorTranslator class. We can convert between these with only 1 line of code. Here&#8217;s how its [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px; margin-top: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.farooqazam.net%2Fconvert-color-to-hex-using-c-sharp%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.farooqazam.net%2Fconvert-color-to-hex-using-c-sharp%2F&amp;source=farooqaaa&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>So you want convert a .NET color to hex or a hex color to .NET color. Well, its very easy to do this. We won&#8217;t have to write lots of code or a special class for it. Thanks to ColorTranslator class. We can convert between these with only 1 line of code. Here&#8217;s how its done:</p>
<p>To convert a Hex color to .NET color use this code:</p>
<pre class="brush: csharp;">Color color = ColorTranslator.FromHtml(&quot;#000000&quot;);</pre>
<p>For converting a .NET color to Hex color, use thise code:</p>
<pre class="brush: csharp;">Color color = Color.FromArgb(0, 100, 50, 100);

// Get the hex color
string HexColor = ColorTranslator.ToHtml(color);
 </pre>
<p>That was simple &amp; easy. If you have any questions don&#8217;t hesitate to leave a comment.</p>
<p>Thanks</p>
]]></content:encoded>
			<wfw:commentRss>http://www.farooqazam.net/convert-color-to-hex-using-c-sharp/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Image Viewer application using C#</title>
		<link>http://www.farooqazam.net/image-viewer-application-using-c-sharp/</link>
		<comments>http://www.farooqazam.net/image-viewer-application-using-c-sharp/#comments</comments>
		<pubDate>Fri, 25 Sep 2009 17:38:45 +0000</pubDate>
		<dc:creator>Farooq Azam</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[application]]></category>
		<category><![CDATA[c sharp]]></category>
		<category><![CDATA[image viewer]]></category>
		<category><![CDATA[picturebox]]></category>
		<category><![CDATA[rotate]]></category>

		<guid isPermaLink="false">http://www.farooqazam.net/?p=126</guid>
		<description><![CDATA[A detailed tutorial about creating an Image Viewer or Picture Viewer application in C#. This application also converts Images from one type to another. Following are the features of this application: Open / Save images. Convert images. Rotate Images. All popular image formats are supported. Let&#8217;s get started! Setting up the Designer For this application [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px; margin-top: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.farooqazam.net%2Fimage-viewer-application-using-c-sharp%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.farooqazam.net%2Fimage-viewer-application-using-c-sharp%2F&amp;source=farooqaaa&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p><a title="Image Viewer" href="http://www.farooqazam.net/wp-content/uploads/2009/09/image-viewer.gif"><img class="alignnone size-full wp-image-127" title="image-viewer" src="http://www.farooqazam.net/wp-content/uploads/2009/09/image-viewer.gif" alt="image-viewer" width="469" height="411" /></a></p>
<p>A detailed tutorial about creating an Image Viewer or Picture Viewer application in C#. This application also converts Images from one type to another.</p>
<p>Following are the features of this application:</p>
<ul>
<li>Open / Save images.</li>
<li>Convert images.</li>
<li>Rotate Images.</li>
<li>All popular image formats are supported.</li>
</ul>
<p>Let&#8217;s get started!<span id="more-126"></span></p>
<h3>Setting up the Designer</h3>
<p>For this application you will need an openFileDialog, saveFileDialog, pictureBox and a few buttons.</p>
<ol>
<li>Add openFileDialog and saveFileDialog to the form.</li>
<li>Use this filter for the openFileDialog and saveFileDialog: &#8221; JPEG File|*.jpg|GIF File|*.gif|PNG File|*.png|BMP File|*.bmp &#8220;</li>
<li>Add 4 buttons (<a title="Image Viewer" href="http://www.farooqazam.net/wp-content/uploads/2009/09/image-viewer.gif">refer to the image above</a>). Name the buttons just like I&#8217;ve did in that image.</li>
<li>Set the &#8220;Enabled&#8221; property for &#8220;Save&#8221; button to &#8220;False&#8221;. And set &#8220;Visible&#8221; to &#8220;False&#8221; for &#8220;RotateClockwise&#8221; and &#8220;RotateCounterclockwise&#8221; buttons.</li>
<li>Add a pictureBox and place it under the buttons (refer to the image above).</li>
</ol>
<p>That&#8217;s it. Now lets get on with the code.</p>
<h3>The Code</h3>
<p>First of all, add this code to the very top of your form:</p>
<pre class="brush: csharp;">
using System.Drawing.Imaging;
</pre>
<p>Declare these methods after the &#8220;public Form1() { &#8230;.&#8221; method:</p>
<pre class="brush: csharp;">// This method will be used to save the image
private void saveImage(string ext)
{
   ImageFormat format = null;

   if(ext == &quot;.gif&quot;)
      format = ImageFormat.Gif;
   else if(ext == &quot;.jpg&quot; || ext == &quot;.jpeg&quot;)
      format = ImageFormat.Jpeg;
   else if(ext == &quot;png&quot;)
      format = ImageFormat.Png;
   else if(ext == &quot;.bmp&quot;)
      format = ImageFormat.Bmp;

   pictureBox1.Image.Save(saveFileDialog1.FileName, format);

}

// This method will be used for rotating the image.
private void rotateImage(int angle)
{
   Bitmap img = new Bitmap(pictureBox1.Image);

   // 0 = Clockwise, 1 = counter-clockwise

   if (angle == 0)
      img.RotateFlip(RotateFlipType.Rotate90FlipNone);
   else if (angle == 1)
      img.RotateFlip(RotateFlipType.Rotate90FlipXY);

   pictureBox1.Size = img.Size;
   pictureBox1.Image = img;
}
</pre>
<p>Double-click the &#8220;Open&#8221; button to add the &#8220;Click()&#8221; event for it and use this code:</p>
<pre class="brush: csharp;">private void openBtn_Click(object sender, EventArgs e)
{
   DialogResult result = openFileDialog1.ShowDialog();
   if (result == DialogResult.OK)
   {
      Image img = Image.FromFile(openFileDialog1.FileName);

      // Assigns the opened image to picturebox.
      pictureBox1.Image = img;

      // Makes the pictureBox width/height same as the opened image.
      pictureBox1.Width = img.Width;
      pictureBox1.Height = img.Height;
   }

   // Enables the Save button.
   saveBtn.Enabled = true;

   // Shows the RotateClockwise and Counterclockwise buttons.
   rotateClockwise.Show();
   rotateCounter.Show();
 }</pre>
<p>Double-click the Save button and use this code:</p>
<pre class="brush: csharp;">private void saveBtn_Click(object sender, EventArgs e)
{
   DialogResult res = saveFileDialog1.ShowDialog();
   if (res == DialogResult.OK)
   {
      // Extracts the extension from the opened file.
      string ext = System.IO.Path.GetExtension(saveFileDialog1.FileName);
      ext = ext.ToLower();

      saveImage(ext);
   }
}</pre>
<p>Double-click the RotateClockwise button and use this code:</p>
<pre class="brush: csharp;">private void rotateClockwise_Click(object sender, EventArgs e)
{
   // Calls the rotateImage method and passes the parameter &quot;0&quot;
   // Refer to the rotateImage method to see how it works.
   rotateImage(0);
}
</pre>
<p>And lastly, double-click the Rotate Counterclockwise button and use this code:</p>
<pre class="brush: csharp;"> private void rotateCounter_Click(object sender, EventArgs e)
{
   // 1 = counter-clockwise
   rotateImage(1);
}
</pre>
<p>That&#8217;s it! We are done.</p>
<h3>Source Code</h3>
<p>ImageViewer Source – <a title="Download ScreenCapture class" href="../source/ImageViewer.zip"><strong>Download Now</strong></a> (42 Kb)</p>
<p>&#8212;&#8212;</p>
<p>Feedback and comments are warmly welcome.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.farooqazam.net/image-viewer-application-using-c-sharp/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Screen Capture Class For C#</title>
		<link>http://www.farooqazam.net/screen-capture-class-for-c-sharp/</link>
		<comments>http://www.farooqazam.net/screen-capture-class-for-c-sharp/#comments</comments>
		<pubDate>Fri, 18 Sep 2009 16:30:46 +0000</pubDate>
		<dc:creator>Farooq Azam</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[c sharp]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[screen capture]]></category>

		<guid isPermaLink="false">http://www.farooqazam.net/?p=106</guid>
		<description><![CDATA[A class that will help you take screenshots quickly and easily. Following are the features of this class: Take screenshot of the whole screen Take screenshot of a particular area using a Rectangle Copy the screenshot to Clipboard. Save the screenshot straight after it is captured. All popular image formats are supported. How to use? [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px; margin-top: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.farooqazam.net%2Fscreen-capture-class-for-c-sharp%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.farooqazam.net%2Fscreen-capture-class-for-c-sharp%2F&amp;source=farooqaaa&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p><img class="alignnone size-full wp-image-108" title="ScreenCapture" src="http://www.farooqazam.net/wp-content/uploads/2009/09/ScreenCapture.gif" alt="ScreenCapture" width="495" height="291" /></p>
<p>A class that will help you take screenshots quickly and easily.</p>
<p>Following are the features of this class:<span id="more-106"></span></p>
<ul>
<li>Take screenshot of the whole screen</li>
<li>Take screenshot of a particular area using a Rectangle</li>
<li>Copy the screenshot to Clipboard.</li>
<li>Save the screenshot straight after it is captured.</li>
<li>All popular image formats are supported.</li>
</ul>
<p><span id="more-103"> </span></p>
<h3>How to use?</h3>
<p>To import this class into your C# project all you have to do is to drag and drop the “ScreenCapture.cs” file into your project.</p>
<p>It is very simple to use this class. Here’s an overview of all the features:</p>
<pre class="brush: csharp;">// Add this code to the top of your code
// i.e: add it above &quot;public Form1() { ......&quot;

SC.ScreenCapture SC = new SC.ScreenCapture();

// To take a screenshot of the whole screen
// Use this code:

SC.CaptureScreen();

// If you want to save the screenshot straight after it is taken
// Use this code:
// Replace &quot;C:\\screenshot.jpg&quot; with your own path

SC.CaptureScreen(@&quot;C:\screenshot.jpg&quot;);

// To take screenshot of a particular area
// Use this code:
// Replace &quot;Rect&quot; with a Rectangle

SC.CaptureRectangle(Rect);

// To save the screenshot straight after it is taken
// Use this code:
// Replace &quot;C:\screenshot-rect.jpg&quot; with your own path and &quot;Rect&quot; with a rectangle.

SC.CaptureRectangle(Rect, &quot;C:\\screenshot-rect.jpg&quot;);

// Note: Use the code below after you've taken a screenshot.
// To copy the screenshot to Clipboard
// Use this code

SC.CopyToClipboard();
</pre>
<p>The above instructions showed you all the features this class offers. However, if you want to see this class in acton. Then I’ve made a simple application for you, which uses this class. Download the class alone or the sample application below:</p>
<h3>Dowload the ScreenCapture class and a sample application</h3>
<blockquote><p>ScreenCapture Class – <a title="Download ScreenCapture class" href="../source/ScreenCapture-class.zip"><strong>Download Now</strong></a> (1 Kb)</p>
<p>Sample Application – <a title="Download Sample Application" href="../source/ScreenCapture-sample.zip"><strong>Download Now</strong></a> (46 Kb)</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.farooqazam.net/screen-capture-class-for-c-sharp/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Screen Capture Application using C#</title>
		<link>http://www.farooqazam.net/screen-capture-application-using-c-sharp/</link>
		<comments>http://www.farooqazam.net/screen-capture-application-using-c-sharp/#comments</comments>
		<pubDate>Fri, 11 Sep 2009 16:51:31 +0000</pubDate>
		<dc:creator>Farooq Azam</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[application]]></category>
		<category><![CDATA[c sharp]]></category>
		<category><![CDATA[screen capture]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.farooqazam.net/?p=77</guid>
		<description><![CDATA[This is a detailed tutorial about making a simple Screen Capture (Screenshot Capture) application in C#. The features are: Take screenshot, view it and save it. We will be making a simple application but if you are little bit good with C# then you can enhance it and make it much better. Setting up the [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px; margin-top: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.farooqazam.net%2Fscreen-capture-application-using-c-sharp%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.farooqazam.net%2Fscreen-capture-application-using-c-sharp%2F&amp;source=farooqaaa&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p><a title="Click to enlarge" href="../wp-content/uploads/2009/09/form1.gif"><img title="Screen Capture Application" src="../wp-content/uploads/2009/09/form1.gif" alt="Screen Capture Application" width="411" height="380" /></a></p>
<p>This is a detailed tutorial about making a simple Screen Capture (Screenshot Capture) application in C#. The features are: Take screenshot, view it and save it. We will be making a simple application but if you are little bit good with C# then you can enhance it and make it much better.</p>
<h3>Setting up the Form</h3>
<p>For this tutorial we will need 2 buttons, a timer, SaveFileDialog and a PictureBox. Here&#8217;s the info:<span id="more-77"></span></p>
<ol>
<li>Add a button first and set its &#8220;Text&#8221; property to &#8220;Take Screenshot&#8221; and &#8220;Name&#8221; property to &#8220;button1&#8243;.</li>
<li>Add another button and set &#8220;Text&#8221; property to &#8220;Save Screenshot&#8221;, set its &#8220;Enabled&#8221; property to &#8220;False&#8221; and &#8220;Name&#8221; property to &#8220;button2&#8243;.</li>
<li>Add a timer and set it &#8220;Interval&#8221; to &#8220;500&#8243; and make sure its &#8220;Enabled&#8221; property is set to &#8220;False&#8221;..</li>
<li>Now add SaveFileDialog and set &#8220;Filter&#8221; to &#8221; JPEG Files|*.jpg|GIF Files|*.gif|PNG Files|*.png &#8220;.</li>
<li>Finally, add a Picturebox.</li>
</ol>
<p>That&#8217;s it. The form is now set-up. Lets get on with the coding.</p>
<h3>The Coding</h3>
<p>First of all, declare these variables at the top of the form; i.e: just above &#8220;public form1() { &#8230;&#8230; &#8220;.</p>
<pre class="brush: csharp;">Graphics g;

// This will get the Size (width &amp;amp; height) of the screen.
Size screenBounds = Screen.PrimaryScreen.Bounds.Size;

public Form1()
 {
 InitializeComponent();
 }
</pre>
<p>Okay. Now double-click &#8220;Take Screenshot&#8221; button which will automatically add the &#8220;Click&#8221; event for it. Use this code:</p>
<pre class="brush: csharp;">private void button1_Click(object sender, EventArgs e)
{
   // We need to hide the form so that we can
   // capture the area behind it.
   this.Hide();
   timer1.Start();
}
</pre>
<p>We started the Timer in the above code. Now lets put it to some use.</p>
<p>Double-click the timer which will automatically add the &#8220;Tick&#8221; event for it. Use this code:</p>
<pre class="brush: csharp;">private void timer1_Tick(object sender, EventArgs e)
{
   // The screenshot will be stored in this bitmap.
   Bitmap capture = new Bitmap(screenBounds.Width, screenBounds.Height);

   // The code below takes the screenshot and
   // saves it in &quot;capture&quot; bitmap.
   g = Graphics.FromImage(capture);
   g.CopyFromScreen(Point.Empty, Point.Empty, screenBounds);

   // This code assigns the screenshot
   // to the Picturebox so that we can view it
   pictureBox1.Image = capture;

   // The code below make the form visible again, enables the &quot;Save&quot; button and stops the timer.
   this.Show();
   button2.Enabled = true;
   timer1.Stop();
}</pre>
<p>The Screenshot capturing and showing it on the picturebox part is done now. Now let&#8217;s add the code for the &#8220;Save Screenshot&#8221; button.</p>
<p>Double-click the Save Screenshot button which adds the &#8220;Click&#8221; event. Use this code:</p>
<pre class="brush: csharp;">private void button2_Click(object sender, EventArgs e)
{
// Show the SaveFileDialog
DialogResult res = saveFileDialog1.ShowDialog();

// Saves the screenshot if the OK button is clicked.
if (res == DialogResult.OK)
{
// This will get the extension of the saved file
string ext = System.IO.Path.GetExtension(saveFileDialog1.FileName);

if(ext == &quot;.jpg&quot;)
pictureBox1.Image.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
else if(ext == &quot;.gif&quot;)
pictureBox1.Image.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Gif);
else if(ext == &quot;.png&quot;)
pictureBox1.Image.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Png);
}
}</pre>
<p>That&#8217;s it! We are done.</p>
<h3>Source Code</h3>
<p><a title="Download Source Code" href="/source/ScreenCapture.rar"><strong>Download Now</strong></a> (36 Kb)</p>
<p>Feedback and comments are warmly welcome <img src='http://www.farooqazam.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
]]></content:encoded>
			<wfw:commentRss>http://www.farooqazam.net/screen-capture-application-using-c-sharp/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Draw a Rectangle in C# using Mouse</title>
		<link>http://www.farooqazam.net/draw-a-rectangle-in-c-sharp-using-mouse/</link>
		<comments>http://www.farooqazam.net/draw-a-rectangle-in-c-sharp-using-mouse/#comments</comments>
		<pubDate>Mon, 07 Sep 2009 09:33:19 +0000</pubDate>
		<dc:creator>Farooq Azam</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[c sharp]]></category>
		<category><![CDATA[draw]]></category>
		<category><![CDATA[mouse]]></category>
		<category><![CDATA[rectangle]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.farooqazam.net/?p=62</guid>
		<description><![CDATA[This tutorial will show you how to draw rectangle in C# using Mouse. You can normally draw a rectangle by putting the values manually. But that doesn&#8217;t always work. Sometimes you&#8217;ll require the rectangle to be drawn automatically (without putting values manually). This tutorial will show you how to do that. Let&#8217;s begin: P.S: The [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px; margin-top: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.farooqazam.net%2Fdraw-a-rectangle-in-c-sharp-using-mouse%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.farooqazam.net%2Fdraw-a-rectangle-in-c-sharp-using-mouse%2F&amp;source=farooqaaa&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p><img class="alignnone size-full wp-image-65" title="Draw a Rectange in C# using Mouse" src="http://www.farooqazam.net/wp-content/uploads/2009/09/draw-rect.gif" alt="Draw a Rectange in C# using Mouse" width="425" height="294" /></p>
<p>This tutorial will show you how to draw rectangle in C# using Mouse. You can normally draw a rectangle by putting the values manually. But that doesn&#8217;t always work. Sometimes you&#8217;ll require the rectangle to be drawn automatically (without putting values manually). This tutorial will show you how to do that. Let&#8217;s begin:<span id="more-62"></span></p>
<p><strong>P.S:</strong> The source code for this tutorial is available for download. Scroll to the bottom of this post.</p>
<p>It is very simple and it isn&#8217;t as hard as it sounds.</p>
<p>First of all, declare the variable &#8220;rect&#8221;. Declaring it outside all the methods will let us access at anywhere in the current form.</p>
<pre class="brush: csharp;">Rectangle rect;

// Optional
public Form1()
{
   InitializeComponent();
   // Use the cross &quot;+&quot; cursor
   this.Cursor = System.Windows.Forms.Cursors.Cross;
   // This will reduce flicker (Recommended)
   this.DoubleBuffered = true;
}</pre>
<p>Now add <strong>MouseDown</strong> event to your form and use the following code:</p>
<pre class="brush: csharp;">private void Form1_MouseDown(object sender, MouseEventArgs e)
{
   // &quot;e.X&quot; and &quot;e.Y&quot; are used to get MousePositionX and MousePositionY
   rect = new Rectangle(e.X, e.Y, 0, 0);
   this.Invalidate();
}</pre>
<p>The above code sets the X-coordinate and Y-coordinate of  the upper-left corner of the rectangle.</p>
<p>Okay. Now add the <strong>MouseMove</strong> event to the form and use this code:</p>
<pre class="brush: csharp;">private void Form1_MouseMove(object sender, MouseEventArgs e)
{
   // This makes sure that the left mouse button is pressed.
   if (e.Button == MouseButtons.Left)
   {
       // Draws the rectangle as the mouse moves
       rect = new Rectangle(rect.Left, rect.Top, e.X - rect.Left, e.Y - rect.Top);
   }
   this.Invalidate();
}</pre>
<p>Almost done! Add the <strong>Paint</strong> event to your form and use this code:</p>
<pre class="brush: csharp;">private void Form1_Paint(object sender, PaintEventArgs e)
{
   // Replace &quot;Color.Red&quot; with any color and repalce &quot;2&quot; with any size you like.
   using (Pen pen = new Pen(Color.Red, 2))
   {
      e.Graphics.DrawRectangle(pen, rect);
   }
}</pre>
<p>That&#8217;s it! We are done.</p>
<p>The source code is available for download:</p>
<p><a title="Download Source Code" href="../source/Draw_Rectangle.rar"><strong>Download Source Code</strong></a></p>
<p>If you have any questions please don&#8217;t hesitate to post a comment.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.farooqazam.net/draw-a-rectangle-in-c-sharp-using-mouse/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
	</channel>
</rss>
