156 lines
4.7 KiB
C#
Raw Normal View History

2022-10-03 14:52:05 -05:00
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;
2022-10-03 15:49:04 -05:00
case "octalTextBox":
2022-10-03 14:52:05 -05:00
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
2022-10-03 16:43:16 -05:00
long decimalNum = 0, hexNum = 0, binNum = 0, octalNum = 0;
2022-10-03 14:52:05 -05:00
try
{ // Extract data from text boxes.
2022-10-03 16:43:16 -05:00
decimalNum = long.Parse(decimalTextBox.Text);
hexNum = Convert.ToInt64(hexTextBox.Text, 16);
binNum = Convert.ToInt64(binaryTextBox.Text, 2);
octalNum = Convert.ToInt64(octalTextBox.Text, 8);
2022-10-03 14:52:05 -05:00
}
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;
2022-10-03 15:49:04 -05:00
//Refocus the textbox when done.
currentFocus("decimalTextBox");
2022-10-03 14:52:05 -05:00
}
if (octalRadioButton.Checked) {
// The octal radio button is pressed.
hexNum = octalNum;
binNum = octalNum;
decimalNum = octalNum;
2022-10-03 15:49:04 -05:00
//Refocus the textbox when done.
currentFocus("octalTextBox");
2022-10-03 14:52:05 -05:00
}
if (hexRadioButton.Checked)
{
// The hexadecimal radio button is pressed.
binNum = hexNum;
decimalNum = hexNum;
octalNum = hexNum;
2022-10-03 15:49:04 -05:00
//Refocus the textbox when done.
currentFocus("hexTextBox");
2022-10-03 14:52:05 -05:00
}
if (binaryRadioButton.Checked)
{
// The binary radio button is pressed.
hexNum = binNum;
decimalNum = binNum;
octalNum = binNum;
2022-10-03 15:49:04 -05:00
//Refocus the textbox when done.
currentFocus("binaryTextBox");
2022-10-03 14:52:05 -05:00
}
// Print output
decimalTextBox.Text = decimalNum.ToString();
hexTextBox.Text = hexNum.ToString("X");
binaryTextBox.Text = Convert.ToString(binNum, 2);
octalTextBox.Text = Convert.ToString(octalNum, 8);
2022-10-03 15:49:04 -05:00
2022-10-03 14:52:05 -05:00
}
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");
}
}
}