Archive for the ‘.NET 1.x’ Category
When Sleep can be bad
Thread.Sleep(0); that is.
Joe Duffy explains why Thread.Sleep(1) is better than Thread.Sleep(0).
Thanks to Jason for the heads up.
ToolboxItem
Over on the develomentor advanced-dotnet mailing list Frans Bouma recently posted an answer to the question: How do you perform an operation the first time a control is added to the form.
Considering the Design time stuff is something I need to learn more about I thought I’d post his answer here in case I need to do something like that in the future (not to mention help whoever may read this)
Though I’d use a Toolbox item
Create a simple class derived from System.Drawing.Design.ToolboxItem and add it to your control via the ToolboxItemAttribute, which takes a string or a typeof definition.In your ToolboxItem class, you override CreateComponentsCore and you there create your control and also do your things you want to execute when the control is placed onto the form.
Rich menus are done!
The menus are done!

I’m not quite ready to release this code yet though. I have one major issue I need to take into account, how do I dynamically size up what the maximum width of the rich menu should be so that it also takes into account the DPI settings on the users PC. Much like how controls get resized depending on the DPI setting, so too should the maximum width of the rich menu. But that comes tomorrow.
Rich menu preview
I’m going to hate myself in the morning, but I have almost got rich menu support fully added to the normal menu drawer.
My only quandry left is how to determine the optimum default maximum width for the description area? How should I scale this value so that a user with large fonts set doesn’t see an incredibly tiny description area.
I’m also not sure why there is a blank space following the Copy menu item. I’m far too tired to actually debug it right now, but I wanted to get this up tonight.

Half way there
I’ve got the textual separator almost completely implemented in both Normal and Office XP menu styles.
The highlight color isn’t 100% right, only off by a few RGB values though. Same with the separator line running underneath it. I can’t seem to figure out the text color for the separator. SystemColors.Highlight isn’t dark enough, and I can’t seem to find an alpha value for black text over the light blue highlight. As much as I hate the idea maybe I’ll need to draw black text then overlay that with a transparent SystemColors.Highlight.
Here are my results so far though:

For those that don’t want to bother reading the 4 page article I wrote about it, BetterMenu (aka MenuExtenderComponent) is a .NET windows forms component that adds menu images, office xp style menus, MDI menu and MDI merged menu support to the regular .NET Menu controls. This project is focused at .NET 1.1 applications, but should work fine in a .NET 2.0 application. I recommend using the MenuStrip components if you are using .NET 2.0 though; they offer features that aren’t available in this component (such as drawing the entire length of the main menu bar, no 1px gap around the entire menu, etc).
VS.NET 2003 doesn’t like me
I don’t know why, but it took 5-7 restarts of the IDE to finally get all of the name changes into BetterMenu. I would work on it for a few minutes and then it would refuse to display any dialog but the Find/Replace one. It wouldn’t even show Find/Replace in Files which is what I wanted. Not to mention weird compile errors such as trying to build my demo projects when I told it to build the component and of course it errored, the component the project referenced hadn’t been build yet.
If this keeps up I may have to do a reinstall of VS.NET 2003 and VS2005.
Anyway, off to work with me!
Returning to an old project
Its been a while since I’ve talked about programming other than work or the syntax highlighter. Recent posts over on Jensen Harris’ Office User Interface blog have got me itching to add new features to an old project of mine the MenuExtenderComponent, soon to be named BetterMenu.
His recent posts about the richer menus in Office 12 have made me want to implement some of those features into MenuC BetterMenu. Namely the Textual Separators and Rich Menus. Both would be easy to implement, I just need to dig out the code, add a couple of properties, and implement a couple methods. Hopefully I can find my latest version of the code so I don’t have to go back in and refix bugs I fixed nearly a year ago.
preg_replace_callback in c#
Sometime today someone hit my blog after searching for “preg_replace_callback in c#”, this seems to be something useful to post about so here we go. And while this person is familiar with doing this stuff in PHP this post is for everyone so I’ll explain it from the ground up. If you are already familiar with preg_replace_callback in PHP then you can just look at the next line to see the key that unlocks the info in MSDN.
The magic of doing programmatic replacement of text in a .NET regular expression is handled by the MatchEvaluator delegate.
public delegate string MatchEvaluator( Match match );
When you write your Regex and use it in one of the Replace methods; every time your pattern has a match, that Match gets sent through the MatchEvaluator you specify. This delegate can point to any one of your methods that matches the right signature; and with it you can choose what to replace the found pattern with however you wish to.
SyntaxHighlighter for .NET problems fixed
Update: I should have proofread this first…my original intent was to just post about the problems; but after writing about them I decided to pack up a new release but I didn’t change the text to acknowledge that.
Looking back at the previous post I noticed I wasn’t very clear in how to use it. And I’ve also squashed some killer bugs with a ton of help from Nish.
In particular, it would ignore the language specification in a pre block unless the language is in quotes (“”); breaking compatibility with the PHP version. There were also a couple bugs dealing with how comments were handled; one was a simple copy/paste error on my part where instead of looking for block comments it looked for line comments again. The other is more sinister in that the sorting routines between PHP and .NET differ enough that the same same sorting routine will work fine in PHP but cause an infinite loop in .NET.
Both are now fixed.
The demo application has been cleaned up a bit; and the base code is now more forgiving of multiple language registrations and handling unregistered languages.
To use the demo application put the files you want to highlight into a directory with a “.src” extension. Run the ConsoleDemo.exe program with the directory name as the only paramter (or set your current directory to the same place the source files are and run it). The highlighted versions will be written out with the same name, but with a “.html” extension.
Once its done it will spit out some very basic stats: the number of bytes output, total time taken, and average time taken.
Download links follow:
SyntaxHighlighter.zip – Demo application (10KB)
SyntaxHighlighter_src.zip – Source code (15KB)
Nish is starting to use this on his regular website now so as long as it doesn’t completely thrash his site my next goal is going to be the ASP.NET control.
SyntaxHighlighter for .NET
After some prodding from Nish I ported the SyntaxHighlighter in use here and on his blog over to .NET. It spits out HTML so it is really meant to be used from ASP.NET, but I don’t have the true ASP.NET control written yet. For now you just have to settle for calling a few methods manually.
Since this one is mainly meant to be used with articles and not blog-like text it doesn’t do any fixing up of the text before processing it.
This is a very basic port; with only the “plain text” and “C#” languages added. Source code is included so anyone can modify or add their own languages.
Download links, features, bugs, coming features, and sample code follow the break.