Click here to Skip to main content
15,880,651 members
Articles / Desktop Programming / Windows Forms

Multiple Base Number Editor

Rate me:
Please Sign up or sign in to vote.
4.85/5 (6 votes)
22 Mar 2010CPOL4 min read 36.6K   501   18   12
A Windows Forms editor for editing number on bases binary, octal, decimal and hexadecimal

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).

It is also possible to register the assembly in GAC. For that, follow the instructions found in this link: http://msdn.microsoft.com/en-us/library/ex0ss12c(VS.80).aspx

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.

addreference.jpg

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

Form1.jpg

Figure 2: Dropping the MultipleBaseNumberEditor on the Form

properties.jpg

Figure 3: Check out the properties on Properties view

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

C#
public enum 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.

C#
public enum 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:

C#
public event EventHandler ValueChanged;   // Occurs when Value changes
public event EventHandler BaseChanged;    // Occurs when the base number changes

In Figure 4, it shows the MultipleBaseNumberEditor into a form with different settings.

sample.jpg

Figure 4: A sample view

Also, this assembly is CLS Compliance (for more information about CLS Compliance: http://msdn.microsoft.com/en-us/library/bhc3fa7f.aspx).

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.

History

  • 22nd March, 2010: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Brazil Brazil
Software Developer and Gamer and Dancer. Computer Science Graduated at Pontifical Catholic University of Belo Horizonte, MG, Brazil. Software Architect student at IGTI. Over 12000 hours of software development experience in many Technologies including: languages (C#, C++, VB.Net, Java, Delphi, Javascript, HTML, SQL); platforms (.Net Framework 1.1, 2, 3.5, 4); databases (Oracle, MS SqlServer); GIS applications (ESRI ArcGIS 9.2, 10); IDE (Visual Studio 2005, 2008, 2010, 2012, Delphi 7, 2006, Eclipse, Adobe Flash Builder 4 for Flex, Qt); Tools (Team Foundation, Perforce, Git, SVN, Starteam, SourceSafe, Winmerge, Doxygen, Notepad++, WireShark); Building web and desktop applications.

I'm more confortable and efficient coding in C#. I believe one should be always searching to overcome oneself.

Comments and Discussions

 
GeneralInvalid sample.zip Pin
Doncp24-Mar-10 12:29
Doncp24-Mar-10 12:29 
GeneralParabéns Pin
Ivo Closs22-Mar-10 16:21
Ivo Closs22-Mar-10 16:21 
GeneralRe: Parabéns Pin
Ramon.Pinho23-Mar-10 14:19
Ramon.Pinho23-Mar-10 14:19 
GeneralCalc.exe Pin
AspDotNetDev22-Mar-10 10:59
protectorAspDotNetDev22-Mar-10 10:59 
GeneralRe: Calc.exe Pin
Ramon.Pinho22-Mar-10 13:35
Ramon.Pinho22-Mar-10 13:35 
GeneralSomething similar Pin
Paw Jershauge22-Mar-10 10:32
Paw Jershauge22-Mar-10 10:32 
GeneralRe: Something similar Pin
Ramon.Pinho22-Mar-10 14:37
Ramon.Pinho22-Mar-10 14:37 
GeneralRe: Something similar Pin
Paw Jershauge23-Mar-10 0:22
Paw Jershauge23-Mar-10 0:22 
GeneralInvalid sample.zip Pin
Doncp22-Mar-10 8:17
Doncp22-Mar-10 8:17 
AnswerRe: Invalid sample.zip [modified] Pin
Ramon.Pinho22-Mar-10 14:08
Ramon.Pinho22-Mar-10 14:08 
GeneralRe: Invalid sample.zip Pin
Doncp23-Mar-10 6:17
Doncp23-Mar-10 6:17 
GeneralRe: Invalid sample.zip [modified] Pin
elderdo25-Mar-16 10:08
elderdo25-Mar-16 10:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.