EDN Admin
Well-known member
I have a basic calculator buttons btn0 through btn9 and hexA-HexF and a label to hold the numbers. Group box with choices hexdecimal , decimal, octal, and binary. what I need is the code for hexdecimal and octal and binary. could someone please help to
provide the code for each. everything happens under the Equals button.hereis the code so far.
<pre class="prettyprint using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace HexCalcChar
{
public partial class Calc : Form
{
private string m_value;
private ArrayList m_store;
private System.Windows.Forms.Label lbRes;
public Calc()
{
InitializeComponent();
Decimal.Checked = true;
m_value = "";
m_store = new ArrayList();
}
private void AddOperatorToArray(Button bttn)
{
m_store.Add(m_value);
lbRes.Text += bttn.Text;
m_value = "";
m_store.Add(bttn.Text);
Period.Enabled = true;
SetEnableOperatorBttns(false);
}
private void AddToArray(Button bttn)
{
m_value += bttn.Text;
lbRes.Text += bttn.Text;
SetEnableOperatorBttns(true);
}
private void Reset()
{
m_value = "";
lbRes.Text = "";
m_store.Clear();
Period.Enabled = true;
}
private void SetEnableOperatorBttns(bool enable)
{
Add.Enabled = enable;
Subtract.Enabled = enable;
Times.Enabled = enable;
Divide.Enabled = enable;
Equals.Enabled = enable;
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
// protected override void Dispose(bool disposing)
//{
// if (disposing)
// {
// if (components != null)
// {
// components.Dispose();
// }
// }
// base.Dispose(disposing);
// }
private void btn0_Click(object sender, EventArgs e)
{
AddToArray(btn0);
}
private void btn1_Click(object sender, EventArgs e)
{
AddToArray(btn1);
}
private void btn2_Click(object sender, EventArgs e)
{
AddToArray(btn2);
}
private void btn3_Click(object sender, EventArgs e)
{
AddToArray(btn3);
}
private void btn4_Click(object sender, EventArgs e)
{
AddToArray(btn4);
}
private void btn5_Click(object sender, EventArgs e)
{
AddToArray(btn5);
}
private void btn6_Click(object sender, EventArgs e)
{
AddToArray(btn6);
}
private void btn7_Click(object sender, EventArgs e)
{
AddToArray(btn7);
}
private void btn8_Click(object sender, EventArgs e)
{
AddToArray(btn8);
}
private void btn9_Click(object sender, EventArgs e)
{
AddToArray(btn9);
}
private void HexA_Click(object sender, EventArgs e)
{
AddToArray(HexA);
MessageBox.Show("Not done yet");
}
private void HexB_Click(object sender, EventArgs e)
{
AddToArray(HexB);
MessageBox.Show("Not done yet");
}
private void HexC_Click(object sender, EventArgs e)
{
AddToArray(HexC);
MessageBox.Show("Not done yet");
}
private void HexD_Click(object sender, EventArgs e)
{
AddToArray(HexD);
MessageBox.Show("Not done yet");
}
private void HexE_Click(object sender, EventArgs e)
{
AddToArray(HexE);
MessageBox.Show("Not done yet");
}
private void HexF_Click(object sender, EventArgs e)
{
AddToArray(HexF);
MessageBox.Show("Not done yet");
}
private void Period_Click(object sender, EventArgs e)
{
AddToArray(Period);
Period.Enabled = false;
}
private void Equals_Click(object sender, EventArgs e)
{
if (Decimal.Checked)
{
try
{
m_store.Add(m_value);
m_store.Add(Equals.Text);
/* m_res here take the first element in m_store if this element is
* operator like +*-/ Exception give us Error else it will take the
* Digits or dot point
*/
float m_res = float.Parse(m_store[0].ToString());
/* this loop extract all element in Array then check it
* if the element is operator it will calculate the prefix
* and postfix and give us the result
* Example 1+2/3*5
* 1) 1+2 = 3
* 2) 3/3 = 1
* 3) 1*5 = 5
* note:- no periority operator and no brakets so its basic calculator
*/
for (int i = 0; i < m_store.Count; i++)
{
if (m_store.ToString() == "+")
{
lbRes.Text = "";
m_res += float.Parse(m_store[i + 1].ToString());
lbRes.Text = m_res.ToString();
}
else if (m_store.ToString() == "-")
{
lbRes.Text = "";
m_res -= float.Parse(m_store[i + 1].ToString());
lbRes.Text = m_res.ToString();
}
else if (m_store.ToString() == "*")
{
lbRes.Text = "";
m_res *= float.Parse(m_store[i + 1].ToString());
lbRes.Text = m_res.ToString();
}
else if (m_store.ToString() == "/")
{
lbRes.Text = "";
m_res /= float.Parse(m_store[i + 1].ToString());
lbRes.Text = m_res.ToString();
}
}
m_store.Clear();
m_value = lbRes.Text;
for (int i = 0; i < m_value.Length; i++)
{
if (m_value.ToString() == ".")
{
Period.Enabled = false;
break;
}
else
{
Period.Enabled = true;
}
}
Equals.Enabled = false;
}
catch (Exception exception)
{
MessageBox.Show(exception.Message, "Error");
}
}
else if (HexDecimal.Checked)
{
//
// code for hexdecimal goes here
//
MessageBox.Show("not done yet");
}
else if (Octal.Checked)
{
//
// code for octal goes here
//
MessageBox.Show("Not done yet");
}
else if (Binary.Checked)
{
//
// code for binary goes here
//
MessageBox.Show("Not done yet");
}
}
private void Times_Click(object sender, EventArgs e)
{
AddOperatorToArray(Times);
}
private void Subtract_Click(object sender, EventArgs e)
{
AddOperatorToArray(Subtract);
}
private void Add_Click(object sender, EventArgs e)
{
AddOperatorToArray(Add);
}
private void Divide_Click(object sender, EventArgs e)
{
AddOperatorToArray(Divide);
}
private void ClearEntry_Click(object sender, EventArgs e)
{
Reset();
}
private void Percentage_Click(object sender, EventArgs e)
{
}
private void Clear_Click(object sender, EventArgs e)
{
Reset();
}
private void HexDecimal_CheckedChanged(object sender, EventArgs e)
{
m_value = "";
lbRes.Text = "";
btn0.Enabled = true;
btn1.Enabled = true;
btn2.Enabled = true;
btn3.Enabled = true;
btn4.Enabled = true;
btn5.Enabled = true;
btn6.Enabled = true;
btn7.Enabled = true;
btn8.Enabled = true;
btn9.Enabled = true;
HexA.Enabled = true;
HexB.Enabled = true;
HexC.Enabled = true;
HexD.Enabled = true;
HexE.Enabled = true;
HexF.Enabled = true;
}
private void Decimal_CheckedChanged(object sender, EventArgs e)
{
m_value = "";
lbRes.Text = "";
btn0.Enabled = true;
btn1.Enabled = true;
btn2.Enabled = true;
btn3.Enabled = true;
btn4.Enabled = true;
btn5.Enabled = true;
btn6.Enabled = true;
btn7.Enabled = true;
btn8.Enabled = true;
btn9.Enabled = true;
HexA.Enabled = false;
HexB.Enabled = false;
HexC.Enabled = false;
HexD.Enabled = false;
HexE.Enabled = false;
HexF.Enabled = false;
}
private void Octal_CheckedChanged(object sender, EventArgs e)
{
m_value = "";
lbRes.Text = "";
btn0.Enabled = true;
btn1.Enabled = true;
btn2.Enabled = true;
btn3.Enabled = true;
btn4.Enabled = true;
btn5.Enabled = true;
btn6.Enabled = true;
btn7.Enabled = true;
btn8.Enabled = false;
btn9.Enabled = false;
HexA.Enabled = false;
HexB.Enabled = false;
HexC.Enabled = false;
HexD.Enabled = false;
HexE.Enabled = false;
HexF.Enabled = false;
}
private void Binary_CheckedChanged(object sender, EventArgs e)
{
m_value = "";
lbRes.Text = "";
btn0.Enabled = true;
btn1.Enabled = true;
btn2.Enabled = false;
btn3.Enabled = false;
btn4.Enabled = false;
btn5.Enabled = false;
btn6.Enabled = false;
btn7.Enabled = false;
btn8.Enabled = false;
btn9.Enabled = false;
HexA.Enabled = false;
HexB.Enabled = false;
HexC.Enabled = false;
HexD.Enabled = false;
HexE.Enabled = false;
HexF.Enabled = false;
}
private void QWord_CheckedChanged(object sender, EventArgs e)
{
}
private void DWord_CheckedChanged(object sender, EventArgs e)
{
}
private void Word_CheckedChanged(object sender, EventArgs e)
{
}
private void Byte_CheckedChanged(object sender, EventArgs e)
{
}
}
}
[/code]
<br/><hr class="sig michael r demulling
View the full article
provide the code for each. everything happens under the Equals button.hereis the code so far.
<pre class="prettyprint using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace HexCalcChar
{
public partial class Calc : Form
{
private string m_value;
private ArrayList m_store;
private System.Windows.Forms.Label lbRes;
public Calc()
{
InitializeComponent();
Decimal.Checked = true;
m_value = "";
m_store = new ArrayList();
}
private void AddOperatorToArray(Button bttn)
{
m_store.Add(m_value);
lbRes.Text += bttn.Text;
m_value = "";
m_store.Add(bttn.Text);
Period.Enabled = true;
SetEnableOperatorBttns(false);
}
private void AddToArray(Button bttn)
{
m_value += bttn.Text;
lbRes.Text += bttn.Text;
SetEnableOperatorBttns(true);
}
private void Reset()
{
m_value = "";
lbRes.Text = "";
m_store.Clear();
Period.Enabled = true;
}
private void SetEnableOperatorBttns(bool enable)
{
Add.Enabled = enable;
Subtract.Enabled = enable;
Times.Enabled = enable;
Divide.Enabled = enable;
Equals.Enabled = enable;
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
// protected override void Dispose(bool disposing)
//{
// if (disposing)
// {
// if (components != null)
// {
// components.Dispose();
// }
// }
// base.Dispose(disposing);
// }
private void btn0_Click(object sender, EventArgs e)
{
AddToArray(btn0);
}
private void btn1_Click(object sender, EventArgs e)
{
AddToArray(btn1);
}
private void btn2_Click(object sender, EventArgs e)
{
AddToArray(btn2);
}
private void btn3_Click(object sender, EventArgs e)
{
AddToArray(btn3);
}
private void btn4_Click(object sender, EventArgs e)
{
AddToArray(btn4);
}
private void btn5_Click(object sender, EventArgs e)
{
AddToArray(btn5);
}
private void btn6_Click(object sender, EventArgs e)
{
AddToArray(btn6);
}
private void btn7_Click(object sender, EventArgs e)
{
AddToArray(btn7);
}
private void btn8_Click(object sender, EventArgs e)
{
AddToArray(btn8);
}
private void btn9_Click(object sender, EventArgs e)
{
AddToArray(btn9);
}
private void HexA_Click(object sender, EventArgs e)
{
AddToArray(HexA);
MessageBox.Show("Not done yet");
}
private void HexB_Click(object sender, EventArgs e)
{
AddToArray(HexB);
MessageBox.Show("Not done yet");
}
private void HexC_Click(object sender, EventArgs e)
{
AddToArray(HexC);
MessageBox.Show("Not done yet");
}
private void HexD_Click(object sender, EventArgs e)
{
AddToArray(HexD);
MessageBox.Show("Not done yet");
}
private void HexE_Click(object sender, EventArgs e)
{
AddToArray(HexE);
MessageBox.Show("Not done yet");
}
private void HexF_Click(object sender, EventArgs e)
{
AddToArray(HexF);
MessageBox.Show("Not done yet");
}
private void Period_Click(object sender, EventArgs e)
{
AddToArray(Period);
Period.Enabled = false;
}
private void Equals_Click(object sender, EventArgs e)
{
if (Decimal.Checked)
{
try
{
m_store.Add(m_value);
m_store.Add(Equals.Text);
/* m_res here take the first element in m_store if this element is
* operator like +*-/ Exception give us Error else it will take the
* Digits or dot point
*/
float m_res = float.Parse(m_store[0].ToString());
/* this loop extract all element in Array then check it
* if the element is operator it will calculate the prefix
* and postfix and give us the result
* Example 1+2/3*5
* 1) 1+2 = 3
* 2) 3/3 = 1
* 3) 1*5 = 5
* note:- no periority operator and no brakets so its basic calculator
*/
for (int i = 0; i < m_store.Count; i++)
{
if (m_store.ToString() == "+")
{
lbRes.Text = "";
m_res += float.Parse(m_store[i + 1].ToString());
lbRes.Text = m_res.ToString();
}
else if (m_store.ToString() == "-")
{
lbRes.Text = "";
m_res -= float.Parse(m_store[i + 1].ToString());
lbRes.Text = m_res.ToString();
}
else if (m_store.ToString() == "*")
{
lbRes.Text = "";
m_res *= float.Parse(m_store[i + 1].ToString());
lbRes.Text = m_res.ToString();
}
else if (m_store.ToString() == "/")
{
lbRes.Text = "";
m_res /= float.Parse(m_store[i + 1].ToString());
lbRes.Text = m_res.ToString();
}
}
m_store.Clear();
m_value = lbRes.Text;
for (int i = 0; i < m_value.Length; i++)
{
if (m_value.ToString() == ".")
{
Period.Enabled = false;
break;
}
else
{
Period.Enabled = true;
}
}
Equals.Enabled = false;
}
catch (Exception exception)
{
MessageBox.Show(exception.Message, "Error");
}
}
else if (HexDecimal.Checked)
{
//
// code for hexdecimal goes here
//
MessageBox.Show("not done yet");
}
else if (Octal.Checked)
{
//
// code for octal goes here
//
MessageBox.Show("Not done yet");
}
else if (Binary.Checked)
{
//
// code for binary goes here
//
MessageBox.Show("Not done yet");
}
}
private void Times_Click(object sender, EventArgs e)
{
AddOperatorToArray(Times);
}
private void Subtract_Click(object sender, EventArgs e)
{
AddOperatorToArray(Subtract);
}
private void Add_Click(object sender, EventArgs e)
{
AddOperatorToArray(Add);
}
private void Divide_Click(object sender, EventArgs e)
{
AddOperatorToArray(Divide);
}
private void ClearEntry_Click(object sender, EventArgs e)
{
Reset();
}
private void Percentage_Click(object sender, EventArgs e)
{
}
private void Clear_Click(object sender, EventArgs e)
{
Reset();
}
private void HexDecimal_CheckedChanged(object sender, EventArgs e)
{
m_value = "";
lbRes.Text = "";
btn0.Enabled = true;
btn1.Enabled = true;
btn2.Enabled = true;
btn3.Enabled = true;
btn4.Enabled = true;
btn5.Enabled = true;
btn6.Enabled = true;
btn7.Enabled = true;
btn8.Enabled = true;
btn9.Enabled = true;
HexA.Enabled = true;
HexB.Enabled = true;
HexC.Enabled = true;
HexD.Enabled = true;
HexE.Enabled = true;
HexF.Enabled = true;
}
private void Decimal_CheckedChanged(object sender, EventArgs e)
{
m_value = "";
lbRes.Text = "";
btn0.Enabled = true;
btn1.Enabled = true;
btn2.Enabled = true;
btn3.Enabled = true;
btn4.Enabled = true;
btn5.Enabled = true;
btn6.Enabled = true;
btn7.Enabled = true;
btn8.Enabled = true;
btn9.Enabled = true;
HexA.Enabled = false;
HexB.Enabled = false;
HexC.Enabled = false;
HexD.Enabled = false;
HexE.Enabled = false;
HexF.Enabled = false;
}
private void Octal_CheckedChanged(object sender, EventArgs e)
{
m_value = "";
lbRes.Text = "";
btn0.Enabled = true;
btn1.Enabled = true;
btn2.Enabled = true;
btn3.Enabled = true;
btn4.Enabled = true;
btn5.Enabled = true;
btn6.Enabled = true;
btn7.Enabled = true;
btn8.Enabled = false;
btn9.Enabled = false;
HexA.Enabled = false;
HexB.Enabled = false;
HexC.Enabled = false;
HexD.Enabled = false;
HexE.Enabled = false;
HexF.Enabled = false;
}
private void Binary_CheckedChanged(object sender, EventArgs e)
{
m_value = "";
lbRes.Text = "";
btn0.Enabled = true;
btn1.Enabled = true;
btn2.Enabled = false;
btn3.Enabled = false;
btn4.Enabled = false;
btn5.Enabled = false;
btn6.Enabled = false;
btn7.Enabled = false;
btn8.Enabled = false;
btn9.Enabled = false;
HexA.Enabled = false;
HexB.Enabled = false;
HexC.Enabled = false;
HexD.Enabled = false;
HexE.Enabled = false;
HexF.Enabled = false;
}
private void QWord_CheckedChanged(object sender, EventArgs e)
{
}
private void DWord_CheckedChanged(object sender, EventArgs e)
{
}
private void Word_CheckedChanged(object sender, EventArgs e)
{
}
private void Byte_CheckedChanged(object sender, EventArgs e)
{
}
}
}
[/code]
<br/><hr class="sig michael r demulling
View the full article