INTRODUCTION
The step-by-step article describes how to use an overloaded
version of the
Int32.Parse method to convert the string representation of a hexadecimal
integer to a decimal integer by using Visual C# .NET. This article contains
code that uses this method to change the background color of a Windows
Form.
back to the topRequirements
This
article assumes that you are familiar with the following topics:
- Programming by using Visual C# .NET
The
following list outlines the recommended hardware, software, network
infrastructure, and service packs that you need:
- Microsoft Windows Server 2003, Microsoft Windows XP, or
Microsoft Windows 2000
- Microsoft Visual Studio .NET 2003 or Microsoft Visual
Studio .NET 2002
back to the topThe Int32.Parse method
To convert the string representation of a hexadecimal integer to a
decimal integer, you can use the
Int32.Parse method. This method converts the string representation of any
number to its 32-bit signed integer equivalent. The .NET Framework provides the
following four overloaded versions of the
Int32.Parse method:
- Int32.Parse(String)
- Int32.Parse(String, IFormatProvider)
- Int32.Parse(String, NumberStyles)
- Int32.Parse(String, NumberStyles,
IFormatProvider)
To convert the string representation of a hexadecimal integer to
a decimal integer, you can use the
Int32.Parse(String, NumberStyles) method. This version of the
Int32.Parse method accepts a string and a number style. The string contains
the string representation of any number in a style that you specify by using
the number style.
back to the
topThe NumberStyles enumeration
The
NumberStyles enumeration is included in the
System.Globalization namespace. You can use this enumeration to specify the permitted
styles in strings that you pass to the
Int32.Parse(String,
NumberStyles) method. The enumeration has a
FlagsAttribute attribute that allows a bitwise combination of its member values.
The most significant bit (MSB) of the
NumberStyles enumeration is the leftmost bit.
back to the topStep-by-step sample
- Start Visual Studio .NET.
- On the File menu, point to
New, and then click Project. The New
Project dialog box appears.
- Under Project Types, click Visual
C# Projects.
- Under Templates, click Windows
Application.
- Click OK. By default, a file that is named
Form1.cs is created.
- Right-click the Form1 Windows Form, and
then click View Code.
- Replace the existing code with the following code.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplication1
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Forms Designer support.
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
this.BackColor= stringToColor("000000");
}
/// <summary>
/// Clean up any resources that were being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method by using the code editor.
/// </summary>
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackColor = System.Drawing.SystemColors.Control;
this.ClientSize = new System.Drawing.Size(292, 266);
this.ForeColor = System.Drawing.SystemColors.Control;
this.Name = "Form1";
this.Text = "Form1";
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private System.Drawing.Color stringToColor(System.String paramValue)
{
int red;
int green;
int blue;
red = (System.Int32.Parse( paramValue.Substring(0, (2) - (0)),System.Globalization.NumberStyles.AllowHexSpecifier));
green = (System.Int32.Parse(paramValue.Substring(2, (4) - (2)),System.Globalization.NumberStyles.AllowHexSpecifier));
blue = (System.Int32.Parse(paramValue.Substring(4, (6) - (4)),System.Globalization.NumberStyles.AllowHexSpecifier));
return System.Drawing.Color.FromArgb(red, green, blue);
}
}
}
- On the Build menu, click Build
Solution.
- On the Debug menu, click
Start to run the application. The Form1 Windows Form appears.
The background color of the form is black.
back to the topTroubleshooting
If you pass a string to the
Int32.Parse method, and the string uses "0x" to specify a hexadecimal format,
a System.FormatException error may occur at run time.
back to the top