Hello and some help for a newb
Howdy all,
I'm fairly new to C# and am in need of some advice on a project I've started working on. I'm trying to create a functioning calculator (similar to the one that comes with windows) yet only used the add/subtract/multiply/divide and decimal functions.
I have the form already designed including the 0-9 buttons, the equal button, a button to clear the label(s) as well as buttons for the functions mentioned above.
So far I've declared class level variables for the 0-9 buttons, and each button's click event displays the correct number in a label.
Now I'm at the part where I need to code for the add/subtract/multiply/divide/decimal/equal buttons. Should I be creating separate methods for each of these functions?
Also, once you click a number button and then you click one of the add/subtract buttons etc., should the first number I entered be stored in an invisible label to be included in a calculation later?
I just haven't got down the logic behind doing this yet.
PS I am using MS VisualStudio.Net 2003 and I can email a zipped version of the project if anyone wants to take a look at it and add code for me to review.
Thanks!
DerikPosted Feb 16, 2007, 6:43 AM
GregPosted Feb 16, 2007, 6:13 AM
My code is:
ColorDialog colorDlg1 = new ColorDialog();
if (colorDlg1 != null)
{
colorDlg1.Color = BackColor; if (colorDlg1.ShowDialog() == DialogResult.OK)
{
BackColor = colorDlg1.Color;
}
}
DerikPosted Feb 16, 2007, 5:58 AM
GregPosted Feb 16, 2007, 5:52 AM
Then when the Equals button is pressed, you can retrieve the action previously selected (e.g. Add), and then use the member variable previously stored. Retrieve the value from the textBox, and perform the calculation, updating the label.
I imagine the code behind (for example) the add button should be:
OnAddButtonClick()
{
m_fOriginalNumber = numberLabel.Text;
m_iAction = 1; // should be an enumerated type for each type of action, I'm using 1 = Add
// Up to you how you handle the entry of a new number, I'm just clearing the control
// here to keep this example fairly simple...
textBox1.Text = "";
}
OnEqualButtonClick()
{
if (m_iOriginalNumber != 0)
{
if (m_iAction == 1) // Add
{
int iTemp = textBox1.Text;
int iResult = m_fOriginalNumber + iTemp;
numberLabel.Text = iResult.ToString();
m_fOriginalNumber = 0;
m_iAction = 0; // defaulting to be no action defined...
}
}
}
Hope this helps a little, let me know if any other questions/problems...
PS. I've not tried this code in any way, so it may not work, just what I've thought up in my head!