Xerratus
Happily stressed out, since 1974


 
Thursday, August 17, 2006
<< Find any control on the calling page from within a custom user control
Wireless NAS solution that you cannot buy! >>

Well it's about time! 

Being a technical blogger, I occasionally like to post code snippets for code that I find useful or helpful for a myriad of things.  The part I hate though is copying my code, which is nicely formatted in Visual Studio 2005, to FreeTextBox, which is the rich text editor for my blog.  Yes, there are some nice sites that allow you to copy and paste in your code that generate html for you but going back and forth is sometimes a hassle.  Also, I have my colors set up a tad bit different than some of the code-to-html generators so the conversion is never like I have it set up.  There was always my trusty fall back; paste the selection into Microsoft Word then copy it again and THEN paste it into FreeTextBox.  This worked but there was one very annoying problem; it wrapped each line with <p></p> tags.  Because of this, the code always looked double-spaced.  To manually go in and remove was a pain for larger chunks of code.

This is what I need/want?
  • The ability to select the code snippet I want right in visual studio.
  • Copy it.
  • Paste it straight into my blog rich textbox WITH the same colors that visual studio has.
After a little searching yesterday, I found EXACTLY what I was looking for; A macro written by Jeff Atwood (www.codinghorror.com/blog).  This allows me to do what I need and I can present my code in a matter of minutes now, formatted just like I like it.  Small, simple and complete... perfect.  For those of you who need it, I implore you to try it.

Here's a quick snippet to demonstrate:

public static Control FindControlByID(ControlCollection controls, string id)
{
    
Control found = null;

    
foreach(Control control in controls)
    {
        
if(control.HasControls())
        {
            found = FindControlByID(control.Controls, id);
            
if(found != null)
            {
                
break;
            }
        }

        
if(control.ID == id)
        {
            found = control;
            
break;
        }
    }

    
return found;
}

Ahhhhhh, that's nice.