Category Archives: Miscellaneous

Create your own custom InputBox

The InputBox method is handy for asking the user to enter a piece of information and was heavily used back in the VB.Old days. So much so, that I see a number of posts on forums that still use this to get data from the user even when using VB.NET. However, one draw back of the InputBox method is that you cannot determine what button was pressed like you can with a MessageBox. So if you want to determine if the user pressed the Cancel button for example, then the only option is to test if a result was returned (i.e. an empty string or not). But this is not foolproof as the user could have pressed the Ok button without entering any data.

So, if you need an InputBox and you also need to know whether the Ok or Cancel button was pressed, then you will need to roll out your own Input Box.

The following snippet demonstrates creating your own custom InputBox replacement by creating a simple form that associates a dialogResult value to the Ok and Cancel buttons allowing you to check what was pressed.

To start, create a new form in your project called InputBoxForm and add the following controls so that it looks similar to the screen shot below:

CustomInputBox

Control TypeProperties
LabelName: promptLabel
Text: ""
AutoSize: False
TextBoxName: inputTextBox
ButtonName: okButton
DialogResult: Ok
Text: Ok
ButtonName: cancelButton
DialogResult: Cancel
Text: Cancel

Now add the following code to the InputBoxForm:

Note the three properties at the top, this allows us to set the title of the form and the prompt that will be shown in the label that we added. The final property can be read from the calling routine to determine what was entered by the user.

Finally, to use:

Note how the ShowDialog method is used to show the input box. This allows us to test what button was pressed via the DialogResult property. This will be Cancel if the Cancel button was clicked or if the form was closed via the Close box. Otherwise it will be Ok and we can grab the value that the user entered.

Download SQL Server Express 2014 the easy way

Today I decided to install SQL Server 2014 Express Edition and figured how hard can that be? Well, Microsoft have certainly made it more difficult that’s for sure.

To start, I did a standard Google search for “SQL Server Express with Management Studio” as previous installations did not always come with Management Studio, and was pleasantly surprised to find that there are a number of options. You can download just a database engine, or one with tools (including the Management Studio) among others.

The instructions seemed straightforward, click the big green button labelled Get Started and fill in a form stating what version you wanted and what language. Sweet. Then you end up with a page with some resources and a big Download Now button. Couldn’t be simpler. WRONG!

The big Download Now button takes you back to the same page where you started so you end up in a perpetual loop. And Microsoft have a cheek to then send you an email thanking you for downloading with a link back to the same page.

So a little bit more googling and I come across a post from Scott Hanselman with direct links to the different downloads that you would need. For me this was SQL Server Express with Tools. This is how simple it should be.

Here’s the post: Downloading SQL Server Express

Treeview – checking and unchecking nodes

This code sample demonstrates how to check and uncheck nodes within a Treeview control regardless of what level you are at within the tree. For example, if you check a parent node then all child nodes of that parent will be checked. Likewise, if you have a child node that is not checked and you check it, it’s parent (and it’s parent) will be checked.

The following code is fired via the AfterCheck event of the Treeview control and I have provided both VB and C# samples. The only piece to point out is the use of the RemoveHandler and AddHandler code within the AfterCheck event. This is done to avoid a StackOverflowException being thrown.

VB.NET Sample

C# Sample