2005-08-26

Channel 9 has a new look & feel

Channel 9, the Microsoft-sponsored developer community site, has a brand new look & feel. It looks shinier, it now supports tags, some functionality is now a lot faster (using AJAX), ...
I like it. A lot.

2005-08-20

Copying an HTML-fragment to the Clipboard

Last year, I wrote an article for RealDevs, that showed how to Copy an HTML-fragment to the Clipboard in C#. However, when I recently tried to update the article (fixed a bug in the code), I got no response. I wrote several e-mails to the webmaster, but the article didn't get updated. So I decided to publish it here, on my blog.

Introduction

Applications like Word and Excel let you copy some text or tabular data to the clipboard in HTML-format, so that the formatting and visual style doesn't change when pasted. At work, I had to do something similar: our application uses a lot of grid-controls to display data, and we wanted the users to be able to select data from a grid, and copy it to the clipboard, in a format that would keep the tabular structure of the data. After some research, I found out that you can copy data to the clipboard in HTML-format, and I thought: this is it. Piece of cake. But it wasn't. Apparently, you have to pass a bit more information than just the HTML-code. Googling around gave me some hints how it should be done, but nothing really specific. So I started experimenting, until it worked correctly.
Tip: use a StringBuilder to build the HTML-code, instead of concatenating strings. StringBuilders are very efficient for building long strings (hence the name StringBuilder): they are a lot faster than strings, and allocate much less memory.

Possible applications

There are a lot of situations where you could use this:

The Code

using System;
using System.Text;
using System.Windows.Forms;

class Test {
public static void CopyHtmlToClipBoard(string html) {
   Encoding enc = Encoding.UTF8;

   string begin = "Version:0.9\r\nStartHTML:{0:000000}\r\nEndHTML:{1:000000}"
      + "\r\nStartFragment:{2:000000}\r\nEndFragment:{3:000000}\r\n";

   string html_begin = "<html>\r\n<head>\r\n"
      + "<meta http-equiv=\"Content-Type\""
      + " content=\"text/html; charset=" + enc.WebName + "\">\r\n"
      + "<title>HTML clipboard</title>\r\n</head>\r\n<body>\r\n"
      + "<!--StartFragment-->";

   string html_end = "<!--EndFragment-->\r\n</body>\r\n</html>\r\n";

   string begin_sample = String.Format(begin, 0, 0, 0, 0);

   int count_begin = enc.GetByteCount(begin_sample);
   int count_html_begin = enc.GetByteCount(html_begin);
   int count_html = enc.GetByteCount(html);
   int count_html_end = enc.GetByteCount(html_end);

   string html_total = String.Format(
      begin
      , count_begin
      , count_begin + count_html_begin + count_html + count_html_end
      , count_begin + count_html_begin
      , count_begin + count_html_begin + count_html
      ) + html_begin + html + html_end;

   DataObject obj = new DataObject();
   obj.SetData(DataFormats.Html, new System.IO.MemoryStream(
      enc.GetBytes(html_total)));
   Clipboard.SetDataObject(obj, true);
}
}

Example: copying a DataTable

StringBuilder html = new StringBuilder();
html.Append("<table>");
foreach ( DataRow row in table.Rows ) {
   html.Append("<tr>");
   foreach ( object o in row )
      html.AppendFormat("<td>{0}</td>", o);
   html.Append("</tr>");
}
html.Append("</table>");
Test.CopyHtmlToClipboard(html.ToString());

2005-08-11

Books about productivity

The last 2 weeks, I've read 2.5 books about increasing productivity, at work as well as at home.
First, I read David Allen's Getting Things Done. It's a very practical, to-the-point guide that teaches you some techniques to dramatically improve your productivity. Most techniques and systems are based on common sense and logics. But only if you apply them, you'll see the real value.

The second book I tried to read, was Rosalene Glickman's Optimal thinking. This book is about learning to be your best self. I didn't finish it because it all made too much sense to me. After reading about 100 pages, I realized that I hadn't learned anything new yet. Apparently, my thinking process is very close to optimal, and I don't feel that I need to improve it a lot. If you feel that you're not your best self, and you think you can improve, then this book is probably a step in the right direction.

The third book I read, was Julie Morgenstern's Making work work. Just like Getting Things Done, this book is about improving your productivity. It's not as to-the-point as GTD, but it has more elaborate examples and stories about some of her clients. The 2 books have different approaches and techniques but are built around the same ideas. If you read both books, you'll see some similarities.

If you only have time to read one book, I'd recommend Getting Things Done. I feel it reads faster, it has a more practical format, and it offers slightly better techniques. But if you have more time, I think that reading Making work work is a great supplement, and gives you a different perspective on the issue.