<?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; How-to</title>
	<atom:link href="http://bitmatic.com/tag/how-to/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</generator>
		<item>
		<title>Using embedded resource files in visual studio 2008</title>
		<link>http://bitmatic.com/c/using-embedded-resource-files-in-visual-studio-2008</link>
		<comments>http://bitmatic.com/c/using-embedded-resource-files-in-visual-studio-2008#comments</comments>
		<pubDate>Sun, 28 Jun 2009 20:37:17 +0000</pubDate>
		<dc:creator>Jakob</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[How-to]]></category>
		<category><![CDATA[VS2008]]></category>

		<guid isPermaLink="false">http://bitmatic.com/?p=341</guid>
		<description><![CDATA[Why embed files ? Many of my projects use a lot of secondary files. It can be images, icons, xml-files, text, sound, whatever&#8230;. These files pose a couple of problems for me: They clutter the program folder. The user may accidentally delete or change them. They expose your graphics/sound files to stealing and copying. They [...]]]></description>
			<content:encoded><![CDATA[<h3>Why embed files ?</h3>
<p>Many of my projects use a lot of secondary files. It can be images, icons, xml-files, text, sound, whatever&#8230;. These files pose a couple of problems for me:</p>
<ul>
<li>They clutter the program folder.</li>
<li>The user may accidentally delete or change them.</li>
<li>They expose your graphics/sound files to stealing and copying.</li>
<li>They expose program-sensitive information that a user may try to use to hack/change the behaviour of the program.</li>
</ul>
<p>Luckily there is a fairly easy solution to parts of these problems. Embed the file in the assembly.</p>
<h3>Embedding a file</h3>
<p>The actual embedding of a file in an assembly in Visual Studio 2008 is very easy indeed. You just add the file to the project, and set the build action to &#8220;Embedded Resource&#8221;.<br />
The compiler will then automagically embed the file in the assembly, where it will be accessible during runtime, to use as you wish. Of course this will make your assembly grow, and with many files, it may grow a lot, but this is usually no problem. You can embed any type of file, and you can arrange the files any way you want in project folders and the like.</p>
<h3>Getting the file from the assembly</h3>
<p>Getting the file out of the assembly is almost as easy as getting it in there. It uses functions from the System.Reflection to access the assembly itself, so adding a &#8220;using System.Reflection;&#8221; to your code is advisable.<br />
There are basically two functions worth noting here:</p>
<ul>
<li>GetManifestResourceNames() &#8211; Gets the names of all the embedded resources in the assembly.</li>
<li>GetManifestResourceStream(string name) &#8211; Returns a stream representing the named resource.</li>
</ul>
<p>So getting hold of the first resource in an assembly is as easy as:</p>
<pre class="csharpcode">
Assembly A = Assembly.GetExecutingAssembly();
<span class="kwrd">string</span>[] names = A.GetManifestResourceNames();
Stream S = A.GetManifestResourceStream(names[0]);</pre>
<p>The above example should get the point across. It is very easy to gain access to an embedded resource as you would access any file.<br />
A more practical example, is using an embedded resource file to set the image of a PictureBox control. It could look like this:</p>
<pre class="csharpcode">
Assembly A = Assembly.GetExecutingAssembly();
Stream S = A.GetManifestResourceStream(<span class="str">"MyProject.Images.jakob.gif"</span>);
pictureBox.Image = Image.FromStream(s);</pre>
<p>In the example here, the actual name of the resource is shown. &#8220;MyProject.Images.jakob.gif&#8221; is the name of a resource called &#8220;jakob.gif&#8221; belonging to the project named &#8220;MyProject&#8221;, and placed in a project folder with the name &#8220;Images&#8221;. This is a fairly ease naming convention, that makes all resource names unique and easy to figure out. If you ever have doubts about the name of a resource, just call GetManifestResourceNames(), and it will tell you the names.</p>
<h3>Saving all the ressources in an assembly to disk</h3>
<p>I&#8217;m going to finish by showing you a nice trick, that shows of the functions i outlined above. It is a function that iterates through all the resources in an assembly, and saves them to disk. It handles the files as raw byte arrays, so it should work with any file. I have tested it on gif, zip and pdf files, and it works like a charm.</p>
<pre class="csharpcode">
<span class="kwrd">private</span> <span class="kwrd">void</span> SaveAllResources()
{
  Assembly A = Assembly.GetExecutingAssembly();
  <span class="kwrd">string</span>[] names = A.GetManifestResourceNames();

  <span class="kwrd">foreach</span> (<span class="kwrd">string</span> filename <span class="kwrd">in</span> names)
  {
    Stream S = A.GetManifestResourceStream(filename);
    <span class="kwrd">byte</span>[] rawFile = <span class="kwrd">new</span> <span class="kwrd">byte</span>[S.Length];

    <span class="rem">//Read the data from the assembly</span>
    S.Read(rawFile, 0, (<span class="kwrd">int</span>)S.Length);

    <span class="rem">//Save the data to the hard drive</span>
    <span class="kwrd">using</span> (FileStream fs = <span class="kwrd">new</span> FileStream(filename, FileMode.Create))
    {
      fs.Write(rawFile, 0, (<span class="kwrd">int</span>)S.Length);
    }
  }
}</pre>
<p>Have fun embedding <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/using-embedded-resource-files-in-visual-studio-2008/feed</wfw:commentRss>
		<slash:comments>0</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>

