using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void currentFocus(string focusOn) { switch (focusOn) { case "binaryTextBox": binaryTextBox.Focus(); break; case "hexTextBox": hexTextBox.Focus(); break; case "octalRadioButton": octalTextBox.Focus(); break; case "decimalTextBox": decimalTextBox.Focus(); break; default: this.Focus(); break; } } private void clearTextboxes() { hexTextBox.Text = "0"; binaryTextBox.Text = "0"; octalTextBox.Text = "0"; decimalTextBox.Text = "0"; } private void computeButton_Click(object sender, EventArgs e) { // Define variables int decimalNum = 0, hexNum = 0, binNum = 0, octalNum = 0; try { // Extract data from text boxes. decimalNum = int.Parse(decimalTextBox.Text); hexNum = Convert.ToInt32(hexTextBox.Text, 16); binNum = Convert.ToInt32(binaryTextBox.Text, 2); octalNum = Convert.ToInt32(octalTextBox.Text, 8); } catch (Exception Ex) { clearTextboxes(); MessageBox.Show("Invalid number entered into one of the textboxes!"); } // What does the user want us to convert from? if (decimalRadioButton.Checked) { // The decimal radio button is pressed. hexNum = decimalNum; binNum = decimalNum; octalNum = decimalNum; } if (octalRadioButton.Checked) { // The octal radio button is pressed. hexNum = octalNum; binNum = octalNum; decimalNum = octalNum; } if (hexRadioButton.Checked) { // The hexadecimal radio button is pressed. binNum = hexNum; decimalNum = hexNum; octalNum = hexNum; } if (binaryRadioButton.Checked) { // The binary radio button is pressed. hexNum = binNum; decimalNum = binNum; octalNum = binNum; } // Print output decimalTextBox.Text = decimalNum.ToString(); hexTextBox.Text = hexNum.ToString("X"); binaryTextBox.Text = Convert.ToString(binNum, 2); octalTextBox.Text = Convert.ToString(octalNum, 8); } private void clearButton_Click(object sender, EventArgs e) { // Clear the textboxes. clearTextboxes(); // Reset focus to the textbox that is currently selected to be converted to. if (decimalRadioButton.Checked) currentFocus("decimalTextBox"); if (hexRadioButton.Checked) currentFocus("hexTextBox"); if (binaryRadioButton.Checked) currentFocus("binaryTextBox"); if (octalRadioButton.Checked) currentFocus("octalTextBox"); } private void exitButton_Click(object sender, EventArgs e) { this.Close(); } private void binaryRadioButton_CheckedChanged(object sender, EventArgs e) { currentFocus("binaryTextBox"); } private void hexRadioButton_CheckedChanged(object sender, EventArgs e) { currentFocus("hexTextBox"); } private void decimalRadioButton_CheckedChanged(object sender, EventArgs e) { currentFocus("decimalTextBox"); } private void octalRadioButton_CheckedChanged(object sender, EventArgs e) { currentFocus("octalTextBox"); } } }