2022-10-03 17:17:57 -05:00
|
|
|
|
/*
|
|
|
|
|
Base-Converter - This file is a part of Base Converter
|
|
|
|
|
Copyright (C) 2022 Caleb Fontenot <foley2431@gmail.com>
|
|
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System;
|
2022-10-03 14:52:05 -05:00
|
|
|
|
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();
|
2022-10-04 21:48:13 -05:00
|
|
|
|
ToolTip toolTip = new ToolTip();
|
|
|
|
|
toolTip.SetToolTip(autoCompute, "Automatically computes input based on changes made to textbox contents.");
|
2022-10-03 14:52:05 -05:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-04 21:48:13 -05:00
|
|
|
|
private void compute()
|
2022-10-03 14:52:05 -05:00
|
|
|
|
{
|
|
|
|
|
// 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
|
|
|
|
}
|
2022-10-04 21:48:13 -05:00
|
|
|
|
if (octalRadioButton.Checked)
|
|
|
|
|
{
|
2022-10-03 14:52:05 -05:00
|
|
|
|
// 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-04 21:48:13 -05:00
|
|
|
|
}
|
|
|
|
|
private void currentFocus(string focusOn) {
|
|
|
|
|
switch (focusOn)
|
|
|
|
|
{
|
|
|
|
|
case "binaryTextBox":
|
|
|
|
|
binaryTextBox.Focus();
|
|
|
|
|
break;
|
|
|
|
|
case "hexTextBox":
|
|
|
|
|
hexTextBox.Focus();
|
|
|
|
|
break;
|
|
|
|
|
case "octalTextBox":
|
|
|
|
|
octalTextBox.Focus();
|
|
|
|
|
break;
|
|
|
|
|
case "decimalTextBox":
|
|
|
|
|
decimalTextBox.Focus();
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
this.Focus();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-03 15:49:04 -05:00
|
|
|
|
|
2022-10-04 21:48:13 -05:00
|
|
|
|
private void clearTextboxes()
|
|
|
|
|
{
|
|
|
|
|
hexTextBox.Text = "0";
|
|
|
|
|
binaryTextBox.Text = "0";
|
|
|
|
|
octalTextBox.Text = "0";
|
|
|
|
|
decimalTextBox.Text = "0";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void computeButton_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
compute();
|
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");
|
|
|
|
|
}
|
2022-10-04 21:48:13 -05:00
|
|
|
|
|
|
|
|
|
private void binaryTextBox_TextChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (autoCompute.Checked)
|
|
|
|
|
compute();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void hexTextBox_TextChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (autoCompute.Checked)
|
|
|
|
|
compute();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void octalTextBox_TextChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (autoCompute.Checked)
|
|
|
|
|
compute();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void decimalTextBox_TextChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (autoCompute.Checked)
|
|
|
|
|
compute();
|
|
|
|
|
}
|
2022-10-03 14:52:05 -05:00
|
|
|
|
}
|
2022-10-03 17:17:57 -05:00
|
|
|
|
}
|