How do I – Activate my MSDN copy of Visio 2013 Professional?

Written by Cornelius J. van Dyk on . Posted in Blog, How Do I...

Situation:  You have a MSDN subscription and you have downloaded Office 2013 or one of the Office products, like Visio 2013 Professional, from Microsoft and have installed said product.  You have a license key provided by MSDN, but the installer never prompts you for the license key.  This is a departure from traditional installer behavior where you would enter the license key before the install can even be done.  Once you’ve completed the install, you open your product and are confronted by this little gem of a warning just below the Ribbon:


Since you have a license key, you click the “Activate” button.  You are presented with this screen which is there for a while, so be patient:             


Eventually, you’re presented with a Sign In screen like this:


Having gotten your license key from MSDN, you promptly enter your MSDN UserID and password and click the “Sign in” button.  Unfortunately, you’re presented with an error thus:


After trying the login a couple more times (just in case you fat fingered the password), you click the “Can’t access your account?” button.  Your browser opens and you’re taken to this page:


Again, you enter your MSDN UserID as well as the CAPCHA code and click the “Next” button.  This time you’re presented with an error screen stating that your ID doesn’t exist thus:


So now what?

This is the problem with the new installer.  In order to use our MSDN license key, we’re going to have to follow these steps:

  1. Click “Start”.
  2. On the popup menu, click “Control Panel”.
  3. In the Control Panel, double click the “Programs and Features” icon.
  4. Scroll down through the list of installed products and locate the product in question.  In this example it was “Microsoft Visio Professional 2013”.
  5. Select the target product.
  6. In the toolbar above the list, click the “Change” button.screenshot.498
  7. When the configuration window appears, select the “Enter a Product Key” radio button and click “Continue”.screenshot.499
  8. No enter your product key and click “Continue”.screenshot.500
  9. Unfortunately, you have to go through the configuration piece again.  For a simple license key addition, I don’t see the need in this, but that how it works for now.  You could just click “Continue” but in my case, I wanted to make sure nothing was getting reset so I clicked “Customize”.screenshot.501
  10. Ensure your configuration settings are as desired and click the “Continue” button.screenshot.502
  11. It sets in the Configuration Progress screen for quite a while.  Just be patient.screenshot.503
  12. Once the configuration completes, click the “Close” button.screenshot.504
  13. Finally re-launch the application and the red warning should now be gone.screenshot.505


This took me a while to figure out, so hopefully this saves someone some time.



How do I – Sort a list of custom class objects in descending order in C#?

Written by Cornelius J. van Dyk on . Posted in Blog, How Do I...

Suppose we have a custom class defined thus:

    class CustomClass
        public string String1 { get; set; }
        public string String2 { get; set; }
        public string String3 { get; set; }
        public int Int1 { get; set; }
        public int Int2 { get; set; }

        public CustomClass()
            String1 = "";
            String2 = "";
            String3 = "";
            Int1 = 0;
            Int2 = 0;

        public int Calculate()
            return Int1 + Int2;
As we can see, the class initializes it’s variables and has a Calculate() method that simply returns the added value of the two int variables of the class. Now suppose we have a list of these class objects in our app with varying values thus:
    CustomClass MyClass = new CustomClass();
    MyClass.Int1 = 10;
    MyClass.Int2 = 5;
    CustomClass My2ndClass = new CustomClass();
    My2ndClass.Int1 = 7;
    CustomClass My3rdClass = new CustomClass();

    List lst = new List();
We now have a list containing 3 of our custom class objects. The first object in the list has an Int1 value of 10. The second has an Int1 value of 5 and the third, given the class’ initialization method, will have an Int1 value of 0. Given the order that we added the class objects to our list, the list, when looking at the Int1 field would look thus: 7, 10, 0 How do we go about sorting this list by any one of the variable fields of the custom class, say Int1 in our case? The built in support for Linq in Visual C# and the .NET Framework, actually makes this very easy. Using a Lambda expression, the task becomes a one liner thus:
  lst = lst.OrderByDescending(x => x.Int1).ToList();
We start with the OrderByDescending() method and then pass the Lambda expression to reference back to the object itself. The x => part of the expression tells C# to reference back to the calling object which in our case is lst. C# will reference back to lst as x which is where the x.Int1 then instructs the OrderByDescending method to use the Int1 field as the value by which to sort, in descending order, the x object which translates to lst. By appending the ToList() method to the back of the statement, C# will take the sorted result and generate a new List<> object which we simply assign back to the original lst variable. If we needed to sort in ascending order we would use the OrderBy() method instead thus making the statement:
 lst = lst.OrderBy(x => x.Int1).ToList();



How do I – Play the TNG Red Alert (or any audio file for that matter) in my WinForms C# app

Written by Cornelius J. van Dyk on . Posted in Blog, How Do I...

Out of the box, C# and .NET supports the playing of any .WAV file.  The code is pretty straight forward.  We simply need to reference the system media class to create a SoundPlayer{} object thus:

System.Media.SoundPlayer p = new System.Media.SoundPlayer(@"C:\Media\MySoundEffect.wav");
Once we have the object, we simply invoke the Play() method thus:

So in two simple lines of code, we are able to play any .wav file from our application.  There are some more embedded problems that may not immediately be obvious.  Allow me to explain.

Suppose I’m trying to add the Star Trek TNG Red Alert sound to my app for an emergency notification.  I begin by locating the sound in question.  A quick web search takes me to the site where the Red Alert section has several samples to choose from.  Once I locate the sound sample that I wish to use, I click the link and download the file.


PROBLEM #1:  The file is in the .mp3 audio format.  Attempting to use it in the instantiation of the SoundPlayer() method fails with the message that only .wav files are supported.

RESOLUTION We could use a custom media player class, or we could just let the internet do some work for us.  A quick search pointed me to the online audio conversion web site.  The simple interface couldn’t be cleaner or more straight forward:


Simply take the .mp3 file we previously downloaded and upload and convert it on this site.  Now we finally have a .wav file that we can use in our project.  Now we would like the alert sound to be played 3 times and a popup window present the error notification.  That’s easy, right?  We simply use a for() loop that looks like this:

for (int i = 0; i <= 2; i++)
    System.Media.SoundPlayer p = new System.Media.SoundPlayer(@"TNG-Red-Alert.wav");
MessageBox.Show("There is an error!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);

We compile and run the app and expect the alert sound to play 3 times… only… it doesn’t.  So why doesn’t it play our audio?  It seems the problem is related to the fact that the asynchronous .Play() method, being spun from the current executing thread, does not have enough time to execute before the modal .Show() method is executed.  As you know, the MessageBox.Show() method will block the existing thread from executing any further until user action is taken on the window.  As such, the Play() method is queued really quickly given present computer CPU speeds, and once the.Show() method is invoked, it aborts the asynchronous threads to the .Play() method.  To solve the problem we have to allow the .Play() method some time to begin execution.  The easiest way to do that is to simply .Sleep() the current thread.  By injecting this line of code:


right after the .Play() call, the currently executing thread will pause for 1.5 seconds.  Depending on what audio you’re playing, you can tweak this value (in milliseconds) to match accordingly.

At this point, the alert message and audio work correctly… but the OCD in me still isn’t happy with the effect we get.  When the error condition is generated, the audio alert is played three times, per our code, and then the error alert is popped up.  The problem I have with that is that the audio warning gets my attention, but then I have to wait for it to finish playing three times before the error message with useful information pops up.  That should be easy to fix, right?  Why not just simply move the .Show() method call to before the for() loop containing the .Play() call?

Unfortunately, this doesn’t work as you’d expect.  Because of the fact that the .Show() call actually freezes the current thread, the message box is displayed and then awaits user interaction before it allows the audio code to execute.  What we need is for the .Show() code to be spun off in another thread so as to allow the audio alert to play while the message is displayed.  To do this, we create a new method thus:

public void ShowAlert()
    System.Threading.Thread t = new System.Threading.Thread(() =>
        MessageBox.Show("There is an error!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);

Once we have the message box contained in a separate thread, we simply make that call prior to playing our audio thus:

for (int i = 0; i <= 2; i++)
    System.Media.SoundPlayer p = new System.Media.SoundPlayer(@"TNG-Red-Alert.wav");

Once the user interacts with the message box, the thread (t) will terminate gracefully.  In the mean time our main app is free to continue execution of our loop to play the alert sound.



Fixing Visual Studio 2012 defaults

Written by Cornelius J. van Dyk on . Posted in Annoyances, How Do I...

One of the biggest pet peeves I experienced when switching to Visual Studio 2012 was the fact that the default command buttons were changed.  In going for a cleaner look, many of the main command buttons most developers use most frequently, was removed from the toolbar interface and buried in the menu structure instead.

Personally, the buttons I find myself using most frequently are the Navigate Backwards and Navigate Forwards (image ), Comment Selected Lines and Uncomment Selected Lines (image ) and lastly Increase Line Indent and Decrease Line Indent (image ).

The Navigate functions are still there, but the Comment and Indent functions are buried deep within the Edit/Advanced menu.  While it’s true that the Comment functions do have hot keys attached to them, I hardly consider Ctrl+K,Ctrl+C for a single click to be useful to most developers save the few that know the hotkeys by heart.

As for the Indent commands, they don’t have any hotkeys and you’d have to click through Edit/Advanced/Indent to get the action.  Though small annoyances, these can be addressed by simply adding the commands back to the command bar.  Here’s how to do just that:

  1. Click Tools on the menu bar.
  2. image  
  3. On the popup menu, click “Customize”.
  4. After the Customize dialog window opens, click over to the “Commands” tab.
  5. Select the “Toolbar” radio button.
  6. In the dropdown to the right of the Toolbar radio button, select the “Standard” toolbar to work with.
  7. The toolbar’s controls will be previewed in the bottom left of the dialog window.
  8. To the right of the preview, click the “Add Command” button.
  9. image  
  10. In the Add Command dialog window that opens up, select the “Edit” category to the left.
  11. On the right, scroll down through the commands and locate the “Line Indent” command.
  12. Select the command and click the “OK” button.
  13. image
  14. The command will be added to the top of the Controls preview.
  15. image  
  16. Repeat steps 8 through 12 for the “Line Unindent”, “Selection Comment” and “Selection Uncomment” commands as well.  The interface doesn’t allow multi-select, though that would be nice touch in a future update.
  17. Once you have all the commands, use the “Move Up” and “Move Down” buttons to the right to arrange the commands in the order you wish them to appear on the toolbar.
  18. image  
  19. When you’re happy with the arrangement, click the “Close” button.  Your new toolbar should now reflect your favorite buttons again. 🙂
  20. image