Base-Converter/BaseConverter/Form1.cs

361 lines
13 KiB
C#

/*
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;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
ToolTip toolTip = new ToolTip();
toolTip.SetToolTip(autoCompute, "Automatically computes input based on changes made to textbox contents.");
}
int paddingOffset = 0, spacingOffset = 0, i = 0;
public void printToBinary(string whatToPrint)
{
binaryTextBox.Text = whatToPrint;
}
public long compute()
{
// Define variables
long decimalNum = 0, hexNum = 0, binNum = 0, octalNum = 0;
//int paddingOffset = 0,spacingOffset = 0, i = 0;
try
{ // Extract data from text boxes.
decimalNum = long.Parse(decimalTextBox.Text);
binNum = Convert.ToInt64(binaryTextBox.Text, 2);
hexNum = Convert.ToInt64(hexTextBox.Text, 16);
octalNum = Convert.ToInt64(octalTextBox.Text, 8);
paddingOffset = int.Parse(paddingOffsetTextBox.Text);
}
catch
{
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;
//Refocus the textbox when done.
currentFocus("decimalTextBox");
}
if (octalRadioButton.Checked)
{
// The octal radio button is pressed.
hexNum = octalNum;
binNum = octalNum;
decimalNum = octalNum;
//Refocus the textbox when done.
currentFocus("octalTextBox");
}
if (hexRadioButton.Checked)
{
// The hexadecimal radio button is pressed.
binNum = hexNum;
decimalNum = hexNum;
octalNum = hexNum;
//Refocus the textbox when done.
currentFocus("hexTextBox");
}
if (binaryRadioButton.Checked)
{
// The binary radio button is pressed.
hexNum = binNum;
decimalNum = binNum;
octalNum = binNum;
//Refocus the textbox when done.
currentFocus("binaryTextBox");
}
// Print output
hexTextBox.Text = hexNum.ToString("X");
octalTextBox.Text = Convert.ToString(octalNum, 8);
decimalTextBox.Text = decimalNum.ToString();
return binNum;
}
public string padding(long paddingInput)
{
if (paddingToggle.Checked) // Padding: Add Zeros to the beginning of the string
{
string binaryString = Convert.ToString(paddingInput, 2); // Define string to offset
int binarySize = binaryString.Length; //count the length of the string.
if (paddingOffset > binarySize)
{ // Is the decimal offset larger than the size of ths string?
while (i <= (paddingOffset - binarySize))
{
binaryString = "0" + binaryString;
i++;
}
}
//debugLabel.Text = sender.GetType().ToString();
return binaryString;
}
else
{
return Convert.ToString(paddingInput, 2);
}
}
public string spacing(string binaryString)
{
spacingOffset = int.Parse(spacingOffsetTextBox.Text);
try
{
if (spacingToggle.Checked)
{
//string binaryString = spacingInput; // Define string to offset
int binarySize = binaryString.Length; //count the length of the string.
i = binarySize; //- (spacingOffset + 1);
int spacingCounterOffset = 0;
var builder = new StringBuilder(binaryString); // String builder
/*
if (binarySize % 2 == 0) // compensate for numbers being odd
{
spacingCounterOffset = 0;
}
else
{
spacingCounterOffset = spacingOffset - 1;
}
*/
while (i != 0)
{
if (!(i == binarySize)) // Get rid of trailing space
{
if (i % spacingOffset == 0) // If i mod spacingOffset equals zero, append a space to the offset specified by i.
{
builder.Insert((i - spacingCounterOffset), " ");
//spacingCounterOffset++;
}
i--;
}
else
{
i--;
}
}
return builder.ToString();
//builder = null; // null out builder when done
//binaryTextBox.Text = binaryString;
}
}
catch
{
MessageBox.Show("Divide by zero");
}
return binaryString;
}
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;
}
}
private void clearTextboxes()
{
hexTextBox.Text = "0";
binaryTextBox.Text = "0";
octalTextBox.Text = "0";
decimalTextBox.Text = "0";
}
private void computeButton_Click(object sender, EventArgs e)
{
// Remove commas from decimalTextBox before parsing it
decimalTextBox.Text = decimalTextBox.Text.Replace(",", "");
// Remove spacing from binaryTextBox before parsing it
binaryTextBox.Text = binaryTextBox.Text.Replace(" ", "");
// Set binary output
string paddingString = padding(compute());
binaryTextBox.Text = spacing(paddingString);
}
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");
}
private void binaryTextBox_Enter(object sender, EventArgs e)
{
//debugLabel.Text = sender.GetType().ToString();
if (autoCompute.Checked & (sender.GetType().ToString().Equals("System.Windows.Forms.TextBox")))
{
// Remove commas from decimalTextBox before parsing it
//decimalTextBox.Text = decimalTextBox.Text.Replace(",", "");
// Remove spacing from binaryTextBox before parsing it
binaryTextBox.Text = binaryTextBox.Text.Replace(" ", "");
// Set binary output
string paddingString = padding(compute());
binaryTextBox.Text = spacing(paddingString);
}
}
private void hexTextBox_Enter(object sender, EventArgs e)
{
//debugLabel.Text = sender.GetType().ToString();
if (autoCompute.Checked & (sender.GetType().ToString().Equals("System.Windows.Forms.TextBox")))
{
// Remove commas from decimalTextBox before parsing it
decimalTextBox.Text = decimalTextBox.Text.Replace(",", "");
// Remove spacing from binaryTextBox before parsing it
binaryTextBox.Text = binaryTextBox.Text.Replace(" ", "");
// Set binary output
string paddingString = padding(compute());
binaryTextBox.Text = spacing(paddingString);
}
}
private void octalTextBox_Enter(object sender, EventArgs e)
{
//debugLabel.Text = sender.GetType().ToString();
if (autoCompute.Checked & (sender.GetType().ToString().Equals("System.Windows.Forms.TextBox")))
{
// Remove commas from decimalTextBox before parsing it
decimalTextBox.Text = decimalTextBox.Text.Replace(",", "");
// Remove spacing from binaryTextBox before parsing it
binaryTextBox.Text = binaryTextBox.Text.Replace(" ", "");
// Set binary output
string paddingString = padding(compute());
binaryTextBox.Text = spacing(paddingString);
}
}
private void decimalTextBox_Enter(object sender, EventArgs e)
{
//debugLabel.Text = sender.GetType().ToString();
if (autoCompute.Checked & (sender.GetType().ToString().Equals("System.Windows.Forms.TextBox")))
{
// Remove commas from decimalTextBox before parsing it
//decimalTextBox.Text = decimalTextBox.Text.Replace(",", "");
// Remove spacing from binaryTextBox before parsing it
binaryTextBox.Text = binaryTextBox.Text.Replace(" ", "");
// Set binary output
string paddingString = padding(compute());
binaryTextBox.Text = spacing(paddingString);
}
}
private void paddingToggle_CheckedChanged(object sender, EventArgs e)
{
if (paddingToggle.Checked)
{
paddingOffsetTextBox.ReadOnly = false;
}
else
{
paddingOffsetTextBox.ReadOnly = true;
}
}
private void spacingToggle_CheckedChanged(object sender, EventArgs e)
{
if (spacingToggle.Checked)
{
spacingOffsetTextBox.ReadOnly = false;
}
else
{
spacingOffsetTextBox.ReadOnly = true;
}
}
}
}