ASP.NET Hesap Makinesi Yapımı

Bu yazımızda ASP.NET’ te hesap makinesi oluşturan örneği oluşturacağız. Örneğimizde oluşturacağımız hesap makinesi aşağıdaki gibi olacaktır.

hesapasp

Projemize ait WebForm1.aspx dosyası:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication3.WebForm1" %>
 
<!DOCTYPE html>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>hesap makinesi</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Panel ID="Panel1" runat="server" GroupingText="Hesap Makinesi"
            Width="280px">
            <table class="style1">
                <tr>
                    <td colspan="4">
                        <asp:TextBox ID="TextBox1" runat="server" Height="60px" Width="250px"
                            CssClass="kutu"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Button ID="Button16" runat="server" Height="60px" Text="C" Width="60px" OnClick="Button16_Click"  />
                    </td>
                    <td>
                        <asp:Button ID="Button10" runat="server" Height="60px" 
                            Text="/" Width="60px" OnClick="Button10_Click" />
                    </td>
                    <td>
                        <asp:Button ID="Button11" runat="server" Height="60px" 
                            Text="*" Width="60px" OnClick="Button11_Click" />
                    </td>
                    <td>
                        <asp:Button ID="Button12" runat="server" Height="60px" 
                            Text="-" Width="60px" OnClick="Button12_Click" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Button ID="Button7" runat="server" Height="60px" 
                            Text="7" Width="60px" OnClick="Button7_Click" />
                    </td>
                    <td>
                        <asp:Button ID="Button8" runat="server" Height="60px" 
                            Text="8" Width="60px" OnClick="Button8_Click" />
                    </td>
                    <td>
                        <asp:Button ID="Button9" runat="server" Height="60px" 
                            Text="9" Width="60px" OnClick="Button9_Click" />
                    </td>
                    <td rowspan="2">
                        <asp:Button ID="Button13" runat="server" Height="120px" 
                            Text="+" Width="60px" OnClick="Button13_Click" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Button ID="Button4" runat="server" Height="60px" 
                            Text="4" Width="60px" OnClick="Button4_Click" />
                    </td>
                    <td>
                        <asp:Button ID="Button5" runat="server" Height="60px" 
                            Text="5" Width="60px" OnClick="Button5_Click" />
                    </td>
                    <td>
                        <asp:Button ID="Button6" runat="server" Height="60px" 
                            Text="6" Width="60px" OnClick="Button6_Click" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Button ID="Button1" runat="server" Height="60px" 
                            Text="1" Width="60px" OnClick="Button1_Click" />
                    </td>
                    <td>
                        <asp:Button ID="Button2" runat="server" Height="60px" 
                            Text="2" Width="60px" OnClick="Button2_Click" />
                    </td>
                    <td>
                        <asp:Button ID="Button3" runat="server" Height="60px" 
                            Text="3" Width="60px" OnClick="Button3_Click" />
                    </td>
                    <td rowspan="2">
                        <asp:Button ID="Button14" runat="server" Height="120px"
                            Text="=" Width="60px" OnClick="Button14_Click" />
                    </td>
                </tr>
                <tr>
                    <td colspan="3">
                        <asp:Button ID="Button15" runat="server" Height="60px" 
                            Text="0" Width="199px" OnClick="Button15_Click" />
                    </td>
                </tr>
            </table>
        </asp:Panel>
     
    </div>
    </form>
</body>
</html>

Örneğimize ait WebForm1.aspx.cs kodları:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
namespace WebApplication3
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        static double a, b;
        static string d;
        protected void Page_Load(object sender, EventArgs e)
        {
 
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (TextBox1.Text == "")
            { TextBox1.Text = "1"; }
            else
            { TextBox1.Text = TextBox1.Text + "1"; }
        }
        protected void Button15_Click(object sender, EventArgs e)
        {
            if (TextBox1.Text == "")
            { TextBox1.Text = "0"; }
            else
            { TextBox1.Text = TextBox1.Text + "0"; }
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            if (TextBox1.Text == "")
            { TextBox1.Text = "2"; }
            else             
            { TextBox1.Text = TextBox1.Text + "2"; }
        }
        protected void Button3_Click(object sender, EventArgs e)
        {
            if (TextBox1.Text == "")
            { TextBox1.Text = "3"; }
            else
            { TextBox1.Text = TextBox1.Text + "3"; }
        }
        protected void Button4_Click(object sender, EventArgs e)
        {
            if (TextBox1.Text == "")
            { TextBox1.Text = "4"; }
            else
            { TextBox1.Text = TextBox1.Text + "4"; }
        }
        protected void Button5_Click(object sender, EventArgs e)
        {
            if (TextBox1.Text == "")
            { TextBox1.Text = "5"; }
            else
            { TextBox1.Text = TextBox1.Text + "5"; }
        }
        protected void Button6_Click(object sender, EventArgs e)
        {
            if (TextBox1.Text == "")
            { TextBox1.Text = "6"; }
            else
            { TextBox1.Text = TextBox1.Text + "6"; }
        }
        protected void Button7_Click(object sender, EventArgs e)
        {
            if (TextBox1.Text == "")
            { TextBox1.Text = "7"; }
            else             
            { TextBox1.Text = TextBox1.Text + "7"; }
        }
        protected void Button8_Click(object sender, EventArgs e)
        {
            if (TextBox1.Text == "")
            { TextBox1.Text = "8"; }
            else
            { TextBox1.Text = TextBox1.Text + "8"; }
        }
        protected void Button9_Click(object sender, EventArgs e)
        {
            if (TextBox1.Text == "")
            { TextBox1.Text = "9"; }
            else
            { TextBox1.Text = TextBox1.Text + "9"; }
        }
        protected void Button13_Click(object sender, EventArgs e)
        {
            d = "+";
            a = Convert.ToInt16(TextBox1.Text);
            TextBox1.Text = "";
        }
        protected void Button12_Click(object sender, EventArgs e)
        {
            d = "-";
            a = Convert.ToInt16(TextBox1.Text);
            TextBox1.Text = "";
        }
        protected void Button11_Click(object sender, EventArgs e)
        {
            d = "*";
            a = Convert.ToInt16(TextBox1.Text);
            TextBox1.Text = "";
        }
        protected void Button10_Click(object sender, EventArgs e)
        {
            d = "/";
            a = Convert.ToInt16(TextBox1.Text);
            TextBox1.Text = "";          
        }
        protected void Button14_Click(object sender, EventArgs e)
        {
            b = Convert.ToInt16(TextBox1.Text);
 
            if (d == "+")
                TextBox1.Text = Convert.ToString(a + b);
            if (d == "-")
                TextBox1.Text = Convert.ToString((sbyte)(a - b));
            if (d == "*")
                TextBox1.Text = Convert.ToString(a * b);
            if (d == "/")
                TextBox1.Text = Convert.ToString(a / b);
        }
 
        protected void Button16_Click(object sender, EventArgs e)
        {
            TextBox1.Text = "";
        }
    }
}

 

Paylaştıkça Artan Tat...