<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>Rénald's Blog - .NET 2.0</title>
    <link>http://www.renaldnollet.com/blog/</link>
    <description />
    <language>en-us</language>
    <copyright>Rénald Nollet</copyright>
    <lastBuildDate>Tue, 13 Nov 2007 17:26:00 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.9074.18820</generator>
    <managingEditor>rno.rno.rno@gmail.com</managingEditor>
    <webMaster>rno.rno.rno@gmail.com</webMaster>
    <item>
      <trackback:ping>http://www.renaldnollet.com/blog/Trackback.aspx?guid=d14beb28-7960-44c3-88d5-fbc0d5fbe30f</trackback:ping>
      <pingback:server>http://www.renaldnollet.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.renaldnollet.com/blog/PermaLink,guid,d14beb28-7960-44c3-88d5-fbc0d5fbe30f.aspx</pingback:target>
      <dc:creator>Rénald Nollet</dc:creator>
      <wfw:comment>http://www.renaldnollet.com/blog/CommentView,guid,d14beb28-7960-44c3-88d5-fbc0d5fbe30f.aspx</wfw:comment>
      <wfw:commentRss>http://www.renaldnollet.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=d14beb28-7960-44c3-88d5-fbc0d5fbe30f</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <span style="color:#333333; font-family:Georgia; font-size:12pt">Maybe you already
met this kind of problems with UserControl and Visual Studio 2005 : you design a main
winform without any problems, you design a user control without any problem. But then
when you add the user control to the main form, the designer will encouter an error
on the main form. You nevertheless may run the application without any problem.</span>
          <span style="font-size:1pt">
          </span>
        </p>
        <p>
          <span style="color:#333333; font-family:Georgia; font-size:12pt">The reason for this
is that the designer is calling the load method of user controls when you open the
form that is hosting those user controls. </span>
        </p>
        <p>
        </p>
        <p>
          <span style="color:#333333; font-family:Georgia; font-size:12pt">A simple test : </span>
        </p>
        <p>
          <span style="color:#333333; font-family:Georgia; font-size:12pt">Here is the designer
part of my user control (an empty one with just one label, this is enough to demonstrate
our problem):<br /><br /><img src="http://www.renaldnollet.com/blog/content/binary/071109_1500_VisualStudi1.jpg" alt="" /></span>
        </p>
        <p>
          <span style="color:#333333; font-family:Georgia; font-size:12pt">
            <br />
Here is the code behind :<br /><br /><img src="http://www.renaldnollet.com/blog/content/binary/071109_1500_VisualStudi2.jpg" alt="" /><br /><br />
The important thing there is the Load method. </span>
        </p>
        <p>
        </p>
        <p>
          <span style="color:#333333; font-family:Georgia; font-size:12pt">Then, if I want to
add the user control to a main empty form, here is what you will see : </span>
        </p>
        <p>
          <img src="http://www.renaldnollet.com/blog/content/binary/071109_1500_VisualStudi3.jpg" alt="" />
          <span style="color:#333333; font-family:Georgia; font-size:12pt">
          </span>
        </p>
        <p>
          <span style="color:#333333; font-family:Georgia; font-size:12pt">Most of the time,
this is not annoying, but sometimes, this may lead to the impossibility to woon the
main form with the designer (because you are in design time and not run time, information
may not be available): </span>
        </p>
        <p>
        </p>
        <p>
          <img src="http://www.renaldnollet.com/blog/content/binary/071109_1500_VisualStudi4.jpg" alt="" />
          <span style="color:#333333; font-family:Georgia; font-size:12pt">
          </span>
        </p>
        <p>
          <span style="color:#333333; font-family:Georgia; font-size:12pt">
            <br />
You can then work with the DesignTime property of the user control : </span>
        </p>
        <p>
        </p>
        <p>
          <img src="http://www.renaldnollet.com/blog/content/binary/071109_1500_VisualStudi5.jpg" alt="" />
          <span style="color:#333333; font-family:Georgia; font-size:12pt">
          </span>
        </p>
        <img width="0" height="0" src="http://www.renaldnollet.com/blog/aggbug.ashx?id=d14beb28-7960-44c3-88d5-fbc0d5fbe30f" />
      </body>
      <title>Visual Studio 2005 and the Load method of UserControl</title>
      <guid isPermaLink="false">http://www.renaldnollet.com/blog/PermaLink,guid,d14beb28-7960-44c3-88d5-fbc0d5fbe30f.aspx</guid>
      <link>http://www.renaldnollet.com/blog/2007/11/13/VisualStudio2005AndTheLoadMethodOfUserControl.aspx</link>
      <pubDate>Tue, 13 Nov 2007 17:26:00 GMT</pubDate>
      <description>&lt;p&gt;
&lt;span style="color:#333333; font-family:Georgia; font-size:12pt"&gt;Maybe you already
met this kind of problems with UserControl and Visual Studio 2005 : you design a main
winform without any problems, you design a user control without any problem. But then
when you add the user control to the main form, the designer will encouter an error
on the main form. You nevertheless may run the application without any problem.&lt;/span&gt;&lt;span style="font-size:1pt"&gt; &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="color:#333333; font-family:Georgia; font-size:12pt"&gt;The reason for this
is that the designer is calling the load method of user controls when you open the
form that is hosting those user controls. &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="color:#333333; font-family:Georgia; font-size:12pt"&gt;A simple test : &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="color:#333333; font-family:Georgia; font-size:12pt"&gt;Here is the designer
part of my user control (an empty one with just one label, this is enough to demonstrate
our problem):&lt;br /&gt;
&lt;br /&gt;
&lt;img src="http://www.renaldnollet.com/blog/content/binary/071109_1500_VisualStudi1.jpg" alt="" /&gt; &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="color:#333333; font-family:Georgia; font-size:12pt"&gt;
&lt;br /&gt;
Here is the code behind :&lt;br /&gt;
&lt;br /&gt;
&lt;img src="http://www.renaldnollet.com/blog/content/binary/071109_1500_VisualStudi2.jpg" alt="" /&gt;
&lt;br /&gt;
&lt;br /&gt;
The important thing there is the Load method. &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="color:#333333; font-family:Georgia; font-size:12pt"&gt;Then, if I want to
add the user control to a main empty form, here is what you will see : &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://www.renaldnollet.com/blog/content/binary/071109_1500_VisualStudi3.jpg" alt="" /&gt;&lt;span style="color:#333333; font-family:Georgia; font-size:12pt"&gt; &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="color:#333333; font-family:Georgia; font-size:12pt"&gt;Most of the time,
this is not annoying, but sometimes, this may lead to the impossibility to woon the
main form with the designer (because you are in design time and not run time, information
may not be available): &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://www.renaldnollet.com/blog/content/binary/071109_1500_VisualStudi4.jpg" alt="" /&gt;&lt;span style="color:#333333; font-family:Georgia; font-size:12pt"&gt; &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="color:#333333; font-family:Georgia; font-size:12pt"&gt;
&lt;br /&gt;
You can then work with the DesignTime property of the user control : &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://www.renaldnollet.com/blog/content/binary/071109_1500_VisualStudi5.jpg" alt="" /&gt;&lt;span style="color:#333333; font-family:Georgia; font-size:12pt"&gt; &lt;/span&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.renaldnollet.com/blog/aggbug.ashx?id=d14beb28-7960-44c3-88d5-fbc0d5fbe30f" /&gt;</description>
      <comments>http://www.renaldnollet.com/blog/CommentView,guid,d14beb28-7960-44c3-88d5-fbc0d5fbe30f.aspx</comments>
      <category>.NET 2.0</category>
      <category>Visual Studio 2005</category>
    </item>
    <item>
      <trackback:ping>http://www.renaldnollet.com/blog/Trackback.aspx?guid=c76c63ae-09d8-48be-8528-84f38a84fcfc</trackback:ping>
      <pingback:server>http://www.renaldnollet.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.renaldnollet.com/blog/PermaLink,guid,c76c63ae-09d8-48be-8528-84f38a84fcfc.aspx</pingback:target>
      <dc:creator>Rénald Nollet</dc:creator>
      <wfw:comment>http://www.renaldnollet.com/blog/CommentView,guid,c76c63ae-09d8-48be-8528-84f38a84fcfc.aspx</wfw:comment>
      <wfw:commentRss>http://www.renaldnollet.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=c76c63ae-09d8-48be-8528-84f38a84fcfc</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <span style="color:#333333; font-family:Georgia; font-size:12pt">Recently, one of
my clients was borred about a customisation of a .NET 2.0 RichTextBox control.<br /></span>
        </p>
        <p>
          <span style="color:#333333; font-family:Georgia; font-size:12pt">He was using a ToolStrip
to change the style of text selections :<br /><br /><img src="http://www.renaldnollet.com/blog/content/binary/071109_1445_Attributebi1.jpg" alt="" /></span>
        </p>
        <p>
          <span style="color:#333333; font-family:Georgia; font-size:12pt">The problem he met
was about the toggling between the different styles.<br /><br />
If you take a look at the FontStyle enumeration, you'll see that it "has a FlagsAttribute
attribute that allows a bitwise combination of its member values" (cfr MSDN).<br /><br />
Knowing that, the values correlated to the different FontStyle are : </span>
        </p>
        <ul>
          <li>
            <span style="color:#333333; font-family:Georgia; font-size:12pt">Regular : 0 (binary
representation : 0000) </span>
          </li>
          <li>
            <span style="color:#333333; font-family:Georgia; font-size:12pt">Bold : 1 (binary
representation : 0001) </span>
          </li>
          <li>
            <span style="color:#333333; font-family:Georgia; font-size:12pt">Italic : 2 (binary
representation : 0010) </span>
          </li>
          <li>
            <span style="color:#333333; font-family:Georgia; font-size:12pt">Underline : 4 (binary
representation : 0100) </span>
          </li>
          <li>
            <span style="color:#333333; font-family:Georgia; font-size:12pt">Strikeout : 8 (binary
representation : 1000) </span>
          </li>
        </ul>
        <p>
          <span style="color:#333333; font-family:Georgia; font-size:12pt">The fact that enumeration
constants are defined in power of 2 is of required by the FlagsAttribute to avoid
overlaps of different combinations. </span>
        </p>
        <p>
          <span style="color:#333333; font-family:Georgia; font-size:12pt">Imagine that the
selected text in the RichTextBox is bold and underlined. You'll get a FontStyle constant
of 5 (binary representation : 0101). </span>
        </p>
        <p>
          <span style="color:#333333; font-family:Georgia; font-size:12pt">If you ask to remove
the bold style, you'll then just have to apply a XOR operator : </span>
        </p>
        <p>
          <span style="color:#333333; font-family:Georgia; font-size:12pt">
            <strong>0101 XOR
0001 = 0100</strong>
          </span>
        </p>
        <p>
          <span style="color:#333333; font-family:Georgia; font-size:12pt">If you then want
to toggle again : </span>
        </p>
        <p>
          <span style="color:#333333; font-family:Georgia; font-size:12pt">
            <strong>0100 XOR
0001 = 0101</strong>
          </span>
        </p>
        <p>
          <span style="color:#333333; font-family:Georgia; font-size:12pt">The code in C# will
thus be something like (rtb is the reference to the RichTextBox): </span>
        </p>
        <p>
          <span style="color:#cc0000; font-family:Times New Roman; font-size:10pt">Font f =
new Font(this.rtb.Font.FontFamily, this.rtb.Font.Size, this.rtb.SelectionFont.Style
^ FontStyle.Bold);<br />
this.rtb.SelectionFont = f;</span>
          <span style="color:#333333; font-family:Georgia; font-size:12pt">
          </span>
        </p>
        <p>
          <span style="color:#333333; font-family:Georgia; font-size:12pt">Easy, isn't it? :) </span>
        </p>
        <p>
          <span style="color:#333333; font-family:Georgia; font-size:12pt">This will of course
apply to all the enumeration types that have the FlagsAttribute. </span>
        </p>
        <img width="0" height="0" src="http://www.renaldnollet.com/blog/aggbug.ashx?id=c76c63ae-09d8-48be-8528-84f38a84fcfc" />
      </body>
      <title>Attribute, bitwise operations and enumerations : the FontStyle example</title>
      <guid isPermaLink="false">http://www.renaldnollet.com/blog/PermaLink,guid,c76c63ae-09d8-48be-8528-84f38a84fcfc.aspx</guid>
      <link>http://www.renaldnollet.com/blog/2007/09/20/AttributeBitwiseOperationsAndEnumerationsTheFontStyleExample.aspx</link>
      <pubDate>Thu, 20 Sep 2007 15:12:00 GMT</pubDate>
      <description>&lt;p&gt;
&lt;span style="color:#333333; font-family:Georgia; font-size:12pt"&gt;Recently, one of
my clients was borred about a customisation of a .NET 2.0 RichTextBox control.&lt;br /&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="color:#333333; font-family:Georgia; font-size:12pt"&gt;He was using a ToolStrip
to change the style of text selections :&lt;br /&gt;
&lt;br /&gt;
&lt;img src="http://www.renaldnollet.com/blog/content/binary/071109_1445_Attributebi1.jpg" alt="" /&gt; &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="color:#333333; font-family:Georgia; font-size:12pt"&gt;The problem he met
was about the toggling between the different styles.&lt;br /&gt;
&lt;br /&gt;
If you take a look at the FontStyle enumeration, you'll see that it "has a FlagsAttribute
attribute that allows a bitwise combination of its member values" (cfr MSDN).&lt;br /&gt;
&lt;br /&gt;
Knowing that, the values correlated to the different FontStyle are : &lt;/span&gt;
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;span style="color:#333333; font-family:Georgia; font-size:12pt"&gt;Regular : 0 (binary
representation : 0000) &lt;/span&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#333333; font-family:Georgia; font-size:12pt"&gt;Bold : 1 (binary
representation : 0001) &lt;/span&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#333333; font-family:Georgia; font-size:12pt"&gt;Italic : 2 (binary
representation : 0010) &lt;/span&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#333333; font-family:Georgia; font-size:12pt"&gt;Underline : 4 (binary
representation : 0100) &lt;/span&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;span style="color:#333333; font-family:Georgia; font-size:12pt"&gt;Strikeout : 8 (binary
representation : 1000) &lt;/span&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
&lt;span style="color:#333333; font-family:Georgia; font-size:12pt"&gt;The fact that enumeration
constants are defined in power of 2 is of required by the FlagsAttribute to avoid
overlaps of different combinations. &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="color:#333333; font-family:Georgia; font-size:12pt"&gt;Imagine that the
selected text in the RichTextBox is bold and underlined. You'll get a FontStyle constant
of 5 (binary representation : 0101). &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="color:#333333; font-family:Georgia; font-size:12pt"&gt;If you ask to remove
the bold style, you'll then just have to apply a XOR operator : &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="color:#333333; font-family:Georgia; font-size:12pt"&gt;&lt;strong&gt;0101 XOR
0001 = 0100&lt;/strong&gt; &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="color:#333333; font-family:Georgia; font-size:12pt"&gt;If you then want
to toggle again : &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="color:#333333; font-family:Georgia; font-size:12pt"&gt;&lt;strong&gt;0100 XOR
0001 = 0101&lt;/strong&gt; &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="color:#333333; font-family:Georgia; font-size:12pt"&gt;The code in C# will
thus be something like (rtb is the reference to the RichTextBox): &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="color:#cc0000; font-family:Times New Roman; font-size:10pt"&gt;Font f =
new Font(this.rtb.Font.FontFamily, this.rtb.Font.Size, this.rtb.SelectionFont.Style
^ FontStyle.Bold);&lt;br /&gt;
this.rtb.SelectionFont = f;&lt;/span&gt;&lt;span style="color:#333333; font-family:Georgia; font-size:12pt"&gt; &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="color:#333333; font-family:Georgia; font-size:12pt"&gt;Easy, isn't it? :) &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="color:#333333; font-family:Georgia; font-size:12pt"&gt;This will of course
apply to all the enumeration types that have the FlagsAttribute. &lt;/span&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.renaldnollet.com/blog/aggbug.ashx?id=c76c63ae-09d8-48be-8528-84f38a84fcfc" /&gt;</description>
      <comments>http://www.renaldnollet.com/blog/CommentView,guid,c76c63ae-09d8-48be-8528-84f38a84fcfc.aspx</comments>
      <category>.NET 2.0</category>
    </item>
  </channel>
</rss>