Let's begin:
- Open Microsoft Visual C# 2008 Express Edition.
- Create new project. You can choose between these two methods:
- From Start Page go to Recent Projects box and double click on link Create: Project...
- Choose menu File - New Project...
- In the New Project dialog choose Windows Forms Application from installed templates, insert name of the application Calculator and press OK.
- Now create the GUI (Graphical User Interface), it looks similar to this one:
- First things first, let's put initialitations to our form. We will have to save our numbers, that is why we need two doubles (n1 and n2). I have put three booleans, which will be used as flags, setNew is used to define if there has to be new number in textbox, commaInserted is used to define if somebody has pressed the comma-button, and ready is used to inform that all components can be calculated. The constructor will be used to initialize all variables and the load-method will be used to initialize our GUI components, in this case we have to initialize our textbox.
double n1;
double n2;
bool setNew;
bool commaInserted;
bool ready;
char op;
public Form1()
{
InitializeComponent();
setNew = true;
commaInserted = false;
ready = false;
}
private void Form1_Load(object sender, EventArgs e)
{
this.textBox1.Text = "0,";
}
- We will add some logic to our input buttons, namely buttons with label 0 - 9. We use the same method for all of those buttons. You will have to modify the EventHandler for those buttons in the designer.cs so it use this btClick.
private void btClick(object sender, EventArgs e)
{
if (setNew)
{
this.textBox1.Text = "";
}
else
{
if (!commaInserted)
{
this.textBox1.Text = this.textBox1.Text.Remove(this.textBox1.Text.Length - 1);
}
}
this.textBox1.Text += ((Button)sender).Text;
if (!commaInserted)
{
this.textBox1.Text += ",";
}
setNew = false;
}
- Now we add an event for +/- button:
private void minus_Click(object sender, EventArgs e)
{
if(this.textBox1.Text.StartsWith("-"))
{
this.textBox1.Text=this.textBox1.Text.Remove(0, 1);
}
else{
if (!textBox1.Text.Equals("0,"))
{
this.textBox1.Text = this.textBox1.Text.Insert(0, "-");
}
}
}
- This is how we can implement comma-button click-event:
private void btnComma_Click(object sender, EventArgs e)
{
commaInserted = true;
if (setNew)
{
this.textBox1.Text = "0,";
setNew = false;
}
}
- After we have programmed the input, now it is time for the brain of our calculator. opClick() is used for operator buttons, namely +,-.*,and /. equal_Click() is used for equal button.
private double calculate()
{
switch (op)
{
case '+':
return n1 + n2;
case '-':
return n1 - n2;
case '*':
return n1 * n2;
case '/':
return n1 / n2;
default:
return 0.0;
}
}
private void opClick(object sender, EventArgs e)
{
if (ready && !setNew)
{
n2 = Convert.ToDouble(textBox1.Text);
textBox1.Text = calculate().ToString();
if (!textBox1.Text.Contains(","))
{
this.textBox1.Text += ",";
}
}
n1 = Convert.ToDouble(textBox1.Text);
setNew = true;
op = ((Button)sender).Text.ToCharArray()[0];
ready = true;
commaInserted = false;
}
private void equal_Click(object sender, EventArgs e)
{
if (ready)
{
n2 = Convert.ToDouble(textBox1.Text);
}
textBox1.Text = calculate().ToString();
if (!textBox1.Text.Contains(","))
{
this.textBox1.Text += ",";
}
n1 = Convert.ToDouble(textBox1.Text);
setNew = true;
ready = false;
commaInserted = false;
}
- And finally, some code for backspace and C buttons.
private void backspace_Click(object sender, EventArgs e)
{
if (this.textBox1.Text[this.textBox1.Text.Length - 1] == ','
&& !commaInserted)
{
this.textBox1.Text = this.textBox1.Text.Remove(this.textBox1.Text.Length - 2, 1);
}
else
{
if (this.textBox1.Text[this.textBox1.Text.Length - 1] == ',')
{
this.commaInserted = false;
}
else
{
this.textBox1.Text = this.textBox1.Text.Remove(this.textBox1.Text.Length - 1, 1);
}
}
if (this.textBox1.Text.Length <= 1)
{
this.textBox1.Text = "0,";
setNew = true;
}
}
private void btnC_Click(object sender, EventArgs e)
{
this.textBox1.Text = "0,";
this.n1 = 0;
this.n2 = 0;
this.op = ' ';
setNew = true;
}
- Save the project using menu File - Save All.
- Your program is now ready. Build the project by choosing menu Build - Build Solution.
- Run the application by selecting green triangle/play button on the upper toolbar or choosing menu Debug - Start Debugging.
No comments:
Post a Comment