Introduction

My initial motivation to write this component came after I was writing a component to support decimal and hexadecimal editing number at work. When I searched for some control or code on the internet, I realized that there was almost nothing like what I was looking for. Probably few people really need to enter numbers in base such as binary, octal or hexadecimal, but here we go.

Initially, my intent was to show the work and the code that I developed, but besides the code is not really long describing it in an article seems boring and unnecessary extensive. So this article is just to show the controls features and if the reader desires, he can download the source code or the assembly.

Background

This code was written using Visual Studio 2008 Express Edition and requires .NET Framework 3.5. Assembly verification was done using Microsoft's FxCop. Code Documentation files were generated using Doxygen.

Using the Code

The MultipleBaseNumberEditor is a Windows Forms component that extends a System.Windows.Forms.UserControl and defines an editor that allows editing a number in any of these bases: binary, octal, decimal or hexadecimal. To use MultipleBaseNumberEditor in your project, you can either add the source code MultiBaseNumberEditor.cs to your solution or add the numbersEditors assembly reference. Adding the source code to the project requires that the project be compiled once before it appears on Visual Studio's Toolbox (under your projects tab).

If the assembly is not registered, the control will not appear in .NET Framework Components when trying to add it to the toolbox. To add it to the toolbox manually without registering in GAC, click on browse and select the numbersEditors.dll.

To add the MultipleBaseNumberEditor component manually through Choose Toolbox Items, right click on Toolbox area and select "Choose Items", then click on "Browse...", find the numbersEditors.dll and click on Open. The component will appear in the list as shown in Figure 1. Make sure the component is checked, then click ok, the component shall appear on Toolbox.

Figure 1: Adding MultipleBaseNumberEditor component to Project

Drop the MultipleBaseNumberEditor from the Toolbox into a form on designer, see Figure 2. Check out the MultipleBaseNumberEditor properties on Properties view (Figure 3).Besides most common properties from a Control, I list below the properties specifically from MultiBaseNumberEditor:

Base

Value

Minimum

Maximum

DisplayBaseChooserButton

BaseChooserStyle

ConfigBinary

ConfigOctal

ConfigDecimal

ConfigHexadecimal

DisplayAllBaseConversions

Figure 2: Dropping the MultipleBaseNumberEditor on the Form

Figure 3: Check out the properties on Properties view

Base is of type BaseNumber. The current defined bases are defined as:

publicenum BaseNumber {
Binary,
Octet,
Decimal,
Hexadecimal
};

Value is an unsigned long. The control does not support negative values.

Minimum and Maximum define the limits Value can be.

Use the DisplayBaseChooserButton to show or hide the base chooser button. With the chooser button, the control allows the user to click and select which base he wants that number to be displayed.

The BaseChooserStyle defines the chooser button style. It can be a simple button or a drop-down list.

publicenum BaseChooserStyle {
Button,
Combo
}

The ConfigBinary, ConfigOctal, ConfigDecimal and ConfigHexadecimal are settings specifically to each base number allowing the user sets if control should display prefix, suffix and if it should padleft with '0' character.

The MultipleBaseNumberEditor provides a default tooltip that within prints the current value in all available formats. To turn that tooltip on or off, set the DisplayAllBaseConversions.

The control provides the events, besides those inherited from UserControl:

Future Work

This control extends a UserControl. It could extend a TextBox. One of the challenges of extending from TextBox is how to draw or add the other controls added here to select the base (that can be a button or combo).

A similar control to work with large byte arrays, displaying groups in any of the chosen bases.

Possibility to extend this control to any base number editor (I wonder what would be the purpose). Possibility to add more features.

Provides more flexibility allowing what base the control will display and the user can select.

FYI, Windows includes a calculator. Open it, go to View → Scientific. You will notice 4 radio buttons: Hex, Dec, Oct, Bin. This will allow you to enter numbers in the specified format and perform calculations on the number in that format. You can then convert to the other bases by selecting one of the other radio buttons. This is very similar to your Multiple Base Number Editor.

Yes! Calc.exe inspired me! But what if you want a control like that in your software?
But I once I needed a control that add a prefix 0x to the hexadecimal value.
I also needed once a control that would pad left '0' characters.