WinForms bug: NodeMouseDoubleClick event in TreeView will sometimes have the wrong node in EventArgs

Here’s a strange bug I’ve encountered today in a WinForms application. Usually, double clicking on a collapsed tree node (which obviously has child nodes) will expand it. Most of the time when we need something to happen when a child node is double clicked, but not the parent, we write something similar to:

   1: void NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
   2: {
   3:   if (e.Node.Nodes.Count == 0)
   4:   {
   5:     // ... handle child node
   6:   }
   7: }
To my surprise, I discovered today that by double clicking on a collapsed parent node, the e.Node parameter was not the node I actually double clicked on, but one of the child nodes. Since the sub-child nodes count was 0, the event handled the (incorrect) node.

I noticed that it was not the same node all the time – it was different. I made a small WinForm application with just a TreeView, and I filled it with the contents of my C:\Windows directory. I placed a MessageBox.Show(e.Node.Text); in the NodeMouseDoubleClick handler. And that helped me realize that the e.Node is actually the node that is currently under the mouse cursor!

Here’s the initial state:

image

Double clicking on Fonts makes the following happen:

image

Notice where the cursor is – that’s the node that was passed in the event handler. Apparently, during the expand, if there are more child nodes that fit on the screen, the parent node scrolls to the top, but the cursor stays in the same place where the double click occurred.

I googled up this problem and I found that this bug was reported on Connect, and it will be fixed in the next version.

In the mean time, the workaround is to use the TreeView’s SelectedNode property instead.

Smart Paste Add-In for Visual Studio 2008

Say you need to paste a chunk of XML (or WKT) into Visual Studio as a string literal (for your unit tests, or what not). How would you do it?

In C# string literals can be represented in two ways:

C# supports two forms of string literals: regular string literals and verbatim string literals.

A regular string literal consists of zero or more characters enclosed in double quotes, as in "hello", and may include both simple escape sequences (such as \t for the tab character) and hexadecimal and Unicode escape sequences.

A verbatim string literal consists of an @ character followed by a double-quote character, zero or more characters, and a closing double-quote character. A simple example is @"hello". In a verbatim string literal, the characters between the delimiters are interpreted verbatim, the only exception being a quote-escape-sequence. In particular, simple escape sequences and hexadecimal and Unicode escape sequences are not processed in verbatim string literals. A verbatim string literal may span multiple lines.

So when you have a chunk of text that looks like this:

    PROJCS["OSGB 1936 / British National Grid",
        GEOGCS["OSGB 1936",
            DATUM["OSGB_1936",
                SPHEROID["Airy 1830",6377563.396,299.3249646,AUTHORITY["EPSG","7001"]],
                TOWGS84[375,-111,431,0,0,0,0],
                AUTHORITY["EPSG","6277"]],
            PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],
            UNIT["DMSH",0.0174532925199433,AUTHORITY["EPSG","9108"]],
            AXIS["Lat",NORTH],
            AXIS["Long",EAST],
            AUTHORITY["EPSG","4277"]]

How would you go about turning this into an escaped string literal (verbatim or regular)?

Being as most programmers are, well, lazy, I didn’t want to do yet another find/replace session. I googled for the first thing that came to mind, “Smart Paste Visual Studio 2008”. I found this great add-in called Smart Paster by Alex Papadimoulis (creator of TheDailyWTF), which adds a highly configurable “Paste As…” context menu:

And now, all I need to do is “Paste As” string, to get this (with a few tweaks):

"    PROJCS[\"OSGB 1936 / British National Grid\"," +
"        GEOGCS[\"OSGB 1936\"," +
"            DATUM[\"OSGB_1936\"," +
"                SPHEROID[\"Airy 1830\",6377563.396,299.3249646,AUTHORITY[\"EPSG\",\"7001\"]]," +
"                TOWGS84[375,-111,431,0,0,0,0]," +
"                AUTHORITY[\"EPSG\",\"6277\"]]," +
"            PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]]," +
"            UNIT[\"DMSH\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9108\"]]," +
"            AXIS[\"Lat\",NORTH]," +
"            AXIS[\"Long\",EAST]," +
"            AUTHORITY[\"EPSG\",\"4277\"]]"

Thanks for the Add-in, Alex!

P.S. The add-in written in Visual Basic, and the installer targets .NET 1.1. For 2008 you need to compile from source yourself, but if you don’t have Visual Basic installed, you can download the compiled assemblies from my SkyDrive. Place them in your C:\Users\<username>\Documents\Visual Studio 2008\Addins directory.

Awesome tip: make View Code default double click action on a WinForm control

Here is something I wish I’d known years ago – you can make the “View Source” action default when you double click on a WinForm control.

Here’s how:

  1. Right click any Form/UserControl file, and choose Open With…
  2. Select CSharp Editor, then press Set as Default
  3. ????
  4. Profit!

That’s it! Now you won’t break the keyboard when you accidentally double click on your MainForm.cs file.

(I'm crying) Amazon Kindle DX - the 10" PDF reader

Amazon just went ahead and announced the new Kindle DX – a bigger Kindle with native PDF support. The major difference is of course the size: the new Kindle DX has a 1200x824 resolution, and it is priced at $489. It is scheduled to ship at Summer 2009.

Here are some additional details:

  • Size: 10.4″ x 7.2″ x 0.38″ (Kindle 2: 8″ x 5.3″ x 0.36″)
  • Display: 16 shades of gray eInk 9.7″ 1200×824 pixels (Kindle 2: 6″ 800×600)
  • Weight: 18.9oz (Kindle 2: 10.2oz)
  • Storage: 3.3GB (Kindle 2: 1.4GB)
  • Battery life: 4 days with 3G modem on, 2 weeks with modem off
  • Connectivity: 3G wireless modem, USB 2.0 port and 3.5mm audio jack

Dr. House says it best:

house-do-want_thumbnail

ReSharper: Fixing array and object initializer indentation

There are tons of options when it comes to configuring your coding style in ReSharper. Somehow I managed to screw the indentation of array and object initializers’ variables. This is what it looked like (I use At Next Line (BSD style) setting):

int[] array = new int[]
{
1, 2, 3
}

To fix this, go to Formatting Style –> Other, and set the Continuous line indent multiplier to 1 (mine was 0)

image

And now it’s back to normal:

int[] array = new int[]
{
  1, 2, 3
}

Skype finally comes to the iPhone

Skype for the iPhone was released in the App Store today. Unfortunately, it was crashing on startup – it showed the Skype blue logo and then crashed back to the home screen. A quick Google query led me to discover that I’m not alone – this affects many “jailbroken” iPhone users.

UPDATE: To fix this on a jailbroken iPhone, update the package “Mobile Substrate” on Cydia to version 0.9.2803-1 (or higher). After the update Skype works great!

ReSharper 4.5 Beta Released - First Impressions

JetBrains just announced that ReSharper 4.5 has reached Beta status, and now available to download.

Here are some of my first impressions:

  • The installation was a snap – it asked to remove the previous version (4.1), and installed the new version instead, while keeping the previous settings.
  • There is a noticeable improvement in the Visual Studio 2008 responsiveness after startup, Start Page appears to be frozen much less than before.
  • Memory usage has dramatically improved! devenv.exe is no longer the hungriest process on my system, Firefox has regained its rightful place at the top of the Task Manager:

  • The Options dialog contains a new page for Naming Style settings, allowing much more control in customizing the naming conventions:

image 

  • Finally, there is now a solution-wide analysis of unused public/protected methods, as well as visibility (works only when solution-wide analysis is on):

image

 image

That’s it for now. The beta seems stable and fast! Another quality product from the wizards at JetBrains. Can't wait for the final version!

Kindle 2 in Israel part 2: What works, what doesn't

One of the main selling points of the Kindle is Whispernet – a free EVDO Internet (provided by Sprint), which allows downloading content on the Kindle, browsing the web, reading RSS feeds, and searching Wikipedia. This is all brilliantly illustrated in this xkcd comic:

 

Unfortunately, Sprint’s coverage of Israel is pretty bad :)

There are two “registrations” of the Kindle. The first one is on the Amazon site. Since I purchased the device for myself, in my own account, the Kindle had automatically been assigned to me. This allows me to purchase Kindle content on the Amazon site, since it knows that I own a Kindle. The second registration is needed to be performed in the device itself. Since there is currently no way of connecting the Kindle to the Internet, I cannot register the device. Until Kindle is online and registered, many features are disabled; even the clock on the Kindle is set wirelessly. This is unfortunate – all the free book samples from Amazon can be only delivered wirelessly to the Kindle, although some alternative eBook stores exist.

Until it becomes possible to register the device in some other way, the only thing I can enjoy are books manually transferred to the Kindle via USB. It appears as a removable thumb drive on my PC, having only 1.4GB of free storage (the rest is used by the OS), so copying content to the Kindle is very easy – just drag and drop. Once disconnected (or ejected) from the PC, the new content immediately appears in the Home page.

One more issue with the Kindle is that it comes with Latin-only characters font. It supports Unicode (there was some confusion on this point – Kindle reads Unicode characters, it just lacks the fonts to display them correctly). There is some progress being made by the community to have additional fonts available on the Kindle.

Bottom line: without realizing it, I’ve spent 3 hours last night reading on the Kindle. No eye strain, no stiff hands, no uncomfortable positions – the Kindle is a joy to read on, and I intend to do so much more of that.

Kindle 2 in Israel

Kindle 2 Hello, my name is Igal, and I’m an addict.

Well, not really, but I sure like gadgets. I bought an iPhone (1st generation) immediately after its price dropped from $600 to $400. I love it and I use it for almost everything. Except reading. I tried reading on the iPhone – it supports PDF attachments, for example, you could open a PDF file from an attached mail message, and the iPhone would display it perfectly. Unfortunately, it’s almost impossible to read on that tiny screen for a long time, and the backlit LCD hurts the eyes.

I’ve been researching electronic readers for some time. There are many to choose from, but recently Amazon had released the new Kindle 2. The first generation Kindle was pretty ugly device, and for the price tag at the time it didn’t seem to be worth it, but the new Kindle looks great, and after viewing and reading many reviews, I decided to buy it.

The problem is – this device is only available in the US. My co-worker suggested a company called Mustop – an Israeli US-based company which provides an address in the US and sends the packages to Israel. I did just that – I registered with Mustop, placed an order on the Amazon site, providing them with the Mustop address I had been given. The whole thing took a total of 15 days from the moment of purchase (I chose the free delivery, between 5-9 business days) to the moment a DHL delivery placed the package in my hands.

Things learned:

  • Before ordering a previously unknown in Israel device that contains a wireless modem, it’s best to have a personal importing permit from the Ministry of Communications, otherwise the device will not go through customs.
  • Nobody answers the phones in the MOC.
  • DHL are kind enough to fax the necessary forms for you, for a small price of almost 500 NIS, and it would still take between 14-28 business days, if at all, for the MOC to approve.
  • Trying to call a different department in the MOC helps, I was told to call the correct phone number “in 5 minutes”. I think the same person answered.
  • TrackThis is an awesome free service which tracks your package and informs you via email/facebook/twitter about the change in its status immediately.
  • When the status goes from “Clearance delay” to “Clearance processing completed” in 11pm just one day after I faxed the forms, and “Arrived at DHL facility” the next morning, it can make a person very happy and very confused at the same time.
  • Calling DHL customer service in disbelief, confirming that the package is on its way, can make your entire day. Heck, week, even.
  • 4pm. “Sign here please”. Status changed to “Shipment Delivered”. I’m now a happy Kindle owner.

There are some things that don’t work in Israel, but I’ll leave that to my next post.

Awesome tip: Copy text from any Message Box to Clipboard

I just found out that it’s possible to copy the text from any message box to clipboard by pressing Ctrl-C when the message box appears! Think of all the times you needed to search for an exact error message text, and needed to switch back and forth between the message and your browser window.

For example:

image

Pressing Ctrl-C will copy the following to the clipboard:

[Window Title]
notepd

[Content]
Windows cannot find 'notepd'. Make sure you typed the name correctly, and then try again.

[OK]

This also works in Windows XP.

«July»
SunMonTueWedThuFriSat
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678