Visual Studio.NET - SilverLight Application For Basic Calculator Application
Here is the SilverLight Application Source code and downloadable for Basic Calculator Application
Click here to download the SilverLight Project in VS2010
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
namespace SLBasicCalculator
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
private bool reset = true;
private bool masterreset = true;
private int optype = -1;
private double oper1 = 0;
private double oper2 = 0;
private int opcount = 0;
private void AddString(string s)
{
if (reset == true)
{
reset = false;
textBox1.Text = "";
}
if (masterreset == true)
{
masterreset = false;
textBoxStack.Text = "";
}
textBox1.Text += s;
}
private void button0_Click(object sender, RoutedEventArgs e)
{
AddString("0");
}
private void button1_Click(object sender, RoutedEventArgs e)
{
AddString("1");
}
private void button2_Click(object sender, RoutedEventArgs e)
{
AddString("2");
}
private void button3_Click(object sender, RoutedEventArgs e)
{
AddString("3");
}
private void button4_Click(object sender, RoutedEventArgs e)
{
AddString("4");
}
private void button5_Click(object sender, RoutedEventArgs e)
{
AddString("5");
}
private void button6_Click(object sender, RoutedEventArgs e)
{
AddString("6");
}
private void button7_Click(object sender, RoutedEventArgs e)
{
AddString("7");
}
private void button8_Click(object sender, RoutedEventArgs e)
{
AddString("8");
}
private void button9_Click(object sender, RoutedEventArgs e)
{
AddString("9");
}
private void buttonComp_Click(object sender, RoutedEventArgs e)
{
string s = textBox1.Text;
oper1 = Convert.ToDouble(textBox1.Text);
oper1 = -oper1;
textBox1.Text = oper1.ToString();
}
private void buttonDot_Click(object sender, RoutedEventArgs e)
{
string s = textBox1.Text;
bool bdotfound = false;
for (int i = 0; i < s.Length; i++)
{
if (s[i] == '.')
bdotfound = true;
}
if (bdotfound == false)
AddString(".");
}
private void buttonsl_Click(object sender, RoutedEventArgs e)
{
if (opcount == 0)
{
opcount = 1;
oper1 = Convert.ToDouble(textBox1.Text);
textBoxStack.Text += oper1.ToString();
textBoxStack.Text += "/";
textBox1.Text = "";
}
else if (opcount == 1)
{
opcount = 1;
textBoxStack.Text += Convert.ToDouble(textBox1.Text).ToString();
textBoxStack.Text += "/";
if (optype == 1)
oper1 = oper1 + Convert.ToDouble(textBox1.Text);
else if (optype == 2)
oper1 = oper1 - Convert.ToDouble(textBox1.Text);
else if (optype == 3)
oper1 = oper1 * Convert.ToDouble(textBox1.Text);
else if (optype == 4)
oper1 = oper1 / Convert.ToDouble(textBox1.Text);
textBox1.Text = "";
}
optype = 4;
}
private void buttonmul_Click(object sender, RoutedEventArgs e)
{
if (opcount == 0)
{
opcount = 1;
oper1 = Convert.ToDouble(textBox1.Text);
textBoxStack.Text += oper1.ToString();
textBoxStack.Text += "*";
textBox1.Text = "";
}
else if (opcount == 1)
{
opcount = 1;
textBoxStack.Text += Convert.ToDouble(textBox1.Text).ToString();
textBoxStack.Text += "*";
if (optype == 1)
oper1 = oper1 + Convert.ToDouble(textBox1.Text);
else if (optype == 2)
oper1 = oper1 - Convert.ToDouble(textBox1.Text);
else if (optype == 3)
oper1 = oper1 * Convert.ToDouble(textBox1.Text);
else if (optype == 4)
oper1 = oper1 / Convert.ToDouble(textBox1.Text);
textBox1.Text = "";
}
optype = 3;
}
private void buttonsub_Click(object sender, RoutedEventArgs e)
{
if (opcount == 0)
{
opcount = 1;
oper1 = Convert.ToDouble(textBox1.Text);
textBoxStack.Text += oper1.ToString();
textBoxStack.Text += "-";
textBox1.Text = "";
}
else if (opcount == 1)
{
opcount = 1;
textBoxStack.Text += Convert.ToDouble(textBox1.Text).ToString();
textBoxStack.Text += "-";
if (optype == 1)
oper1 = oper1 + Convert.ToDouble(textBox1.Text);
else if (optype == 2)
oper1 = oper1 - Convert.ToDouble(textBox1.Text);
else if (optype == 3)
oper1 = oper1 * Convert.ToDouble(textBox1.Text);
else if (optype == 4)
oper1 = oper1 / Convert.ToDouble(textBox1.Text);
textBox1.Text = oper1.ToString();
reset = true;
}
optype = 2;
}
private void buttonAdd_Click(object sender, RoutedEventArgs e)
{
if (opcount == 0)
{
opcount = 1;
oper1 = Convert.ToDouble(textBox1.Text);
textBoxStack.Text += oper1.ToString();
textBoxStack.Text += "+";
textBox1.Text = "";
}
else if (opcount == 1)
{
opcount = 1;
textBoxStack.Text += Convert.ToDouble(textBox1.Text).ToString();
textBoxStack.Text += "+";
if (optype == 1)
oper1 = oper1 + Convert.ToDouble(textBox1.Text);
else if (optype == 2)
oper1 = oper1 - Convert.ToDouble(textBox1.Text);
else if (optype == 3)
oper1 = oper1 * Convert.ToDouble(textBox1.Text);
else if (optype == 4)
oper1 = oper1 / Convert.ToDouble(textBox1.Text);
textBox1.Text = oper1.ToString();
reset = true;
}
optype = 1;
}
private void buttonEQ_Click(object sender, RoutedEventArgs e)
{
if (opcount == 0)
{
opcount = 1;
oper1 = Convert.ToDouble(textBox1.Text);
textBox1.Text = oper1.ToString();
reset = true;
}
else if (opcount == 1)
{
opcount = 0;
textBoxStack.Text += Convert.ToDouble(textBox1.Text).ToString();
if (optype == 1)
oper1 = oper1 + Convert.ToDouble(textBox1.Text);
else if (optype == 2)
oper1 = oper1 - Convert.ToDouble(textBox1.Text);
else if (optype == 3)
oper1 = oper1 * Convert.ToDouble(textBox1.Text);
else if (optype == 4)
oper1 = oper1 / Convert.ToDouble(textBox1.Text);
textBox1.Text = oper1.ToString();
reset = true;
masterreset = true;
}
}
private void buttonper_Click(object sender, RoutedEventArgs e)
{
//System.Diagnostics.Process p = new System.Diagnostics.Process();
//p.StartInfo.FileName = "http://www.softwareandfinance.com";
//p.Start();
}
private void buttonclear_Click(object sender, RoutedEventArgs e)
{
}
private void buttonoff_Click(object sender, RoutedEventArgs e)
{
reset = true;
masterreset = true;
textBox1.Text = "";
textBoxStack.Text = "";
}
}
}
<UserControl x:Class="SLBasicCalculator.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="220" d:DesignWidth="200" Background="#eeeeee"
>
<Grid Margin="0,0,0,0" Background="#eeeeee">
<Grid.RowDefinitions>
<RowDefinition Height="30"></RowDefinition>
<RowDefinition Height="30"></RowDefinition>
<RowDefinition Height="30"></RowDefinition>
<RowDefinition Height="30"></RowDefinition>
<RowDefinition Height="30"></RowDefinition>
<RowDefinition Height="30"></RowDefinition>
<RowDefinition Height="30"></RowDefinition>
</Grid.RowDefinitions>
<TextBox Grid.Row="1" HorizontalAlignment="Left" Margin="15,0,0,0" Height="22" Name="textBoxStack" Width="170" TextAlignment="Right" IsReadOnly="True" IsEnabled="False" />
<TextBox Grid.Row="2" HorizontalAlignment="Left" Margin="15,0,0,0" Height="22" Name="textBox1" Width="170" TextAlignment="Right" />
<Button Grid.Row="3" HorizontalAlignment="Left" BorderThickness="1" Content="7" Name="button7" Margin="15,0,0,0" Width="30" Height="25" Click="button7_Click" />
<Button Grid.Row="3" HorizontalAlignment="Left" BorderThickness="1" Content="8" Name="button8" Margin="50,0,0,0" Width="30" Height="25" Click="button8_Click" />
<Button Grid.Row="3" HorizontalAlignment="Left" BorderThickness="1" Content="9" Name="button9" Margin="85,0,0,0" Width="30" Height="25" Click="button9_Click" />
<Button Grid.Row="3" HorizontalAlignment="Left" BorderThickness="1" Content="/" Name="buttonsl" Margin="120,0,0,0" Width="30" Height="25" Click="buttonsl_Click" />
<Button Grid.Row="3" HorizontalAlignment="Left" BorderThickness="1" Content="AC" Name="buttonoff" Margin="155,0,0,0" Width="30" Height="25" Click="buttonoff_Click"/>
<Button Grid.Row="4" HorizontalAlignment="Left" Content="4" Name="button4" Margin="15,0,0,0" Width="30" Height="25" Click="button4_Click" />
<Button Grid.Row="4" HorizontalAlignment="Left" Content="5" Name="button5" Margin="50,0,0,0" Width="30" Height="25" Click="button5_Click" />
<Button Grid.Row="4" HorizontalAlignment="Left" Content="6" Name="button6" Margin="85,0,0,0" Width="30" Height="25" Click="button6_Click" />
<Button Grid.Row="4" HorizontalAlignment="Left" Content="*" Name="buttonmul" Margin="120,0,0,0" Width="30" Height="25" Click="buttonmul_Click" />
<Button Grid.Row="4" HorizontalAlignment="Left" Content="C" Name="buttonclear" Margin="155,0,0,0" Width="30" Height="25" Click="buttonclear_Click"/>
<Button Grid.Row="5" HorizontalAlignment="Left" Content="1" Name="button1" Margin="15,0,0,0" Width="30" Height="25" Click="button1_Click" />
<Button Grid.Row="5" HorizontalAlignment="Left" Content="2" Name="button2" Margin="50,0,0,0" Width="30" Height="25" Click="button2_Click" />
<Button Grid.Row="5" HorizontalAlignment="Left" Content="3" Name="button3" Margin="85,0,0,0" Width="30" Height="25" Click="button3_Click" />
<Button Grid.Row="5" HorizontalAlignment="Left" Content="-" Name="buttonsub" Margin="120,0,0,0" Width="30" Height="25" Click="buttonsub_Click" />
<Button Grid.Row="5" HorizontalAlignment="Left" Content="S&F" Name="buttonper" Margin="155,0,0,0" Width="30" Height="25" Click="buttonper_Click" />
<Button Grid.Row="6" HorizontalAlignment="Left" Content="0" Name="button0" Margin="15,0,0,0" Width="30" Height="25" Click="button0_Click" />
<Button Grid.Row="6" HorizontalAlignment="Left" Content="+/-" Name="buttonComp" Margin="50,0,0,0" Width="30" Height="25" Click="buttonComp_Click" />
<Button Grid.Row="6" HorizontalAlignment="Left" Content="." Name="buttonDot" Margin="85,0,0,0" Width="30" Height="25" Click="buttonDot_Click" />
<Button Grid.Row="6" HorizontalAlignment="Left" Content="+" Name="buttonAdd" Margin="120,0,0,0" Width="30" Height="25" Click="buttonAdd_Click" />
<Button Grid.Row="6" HorizontalAlignment="Left" Content="=" Name="buttonEQ" Margin="155,0,0,0" Width="30" Height="25" Click="buttonEQ_Click" />
</Grid>
</UserControl>
|