您的位置:首页 > 移动开发 > Objective-C

简单计算器

2010-06-29 22:26 127 查看
最近在学习配置文件的读取,网上查找了相关资料,写了这么一个程序来,还有不完善的地方 提个建议吧 嘿嘿 大家一起学习学习

 

界面布局:



 

配置文件设置:

 

在程序中添加新建项添加一个添加一个 应用程序配置文件App.config,右击项目点属性→设置,如下图所示

 



 

 

 

 

以下是代码:

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using 计算器.Properties;
using Microsoft.Win32;

namespace 计算器
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        class FormPositionHelper
        {
            // 在HKEY_CURRENT_USER 设置注册表的路径         
            public static string RegPath = @"Software/App/";
            public static void SaveSize(System.Windows.Forms.Form frm)
            {
                //Create or retrieve a reference to a key where the settings             
                //will be stored.             
                RegistryKey key;
                key = Registry.LocalMachine.CreateSubKey(RegPath + frm.Name);
                key.SetValue("Height", frm.Height);
                key.SetValue("Width", frm.Width);
                key.SetValue("Left", frm.Left);
                key.SetValue("Top", frm.Top);
            }
            public static void SetSize(System.Windows.Forms.Form frm)
            {
                RegistryKey key;
                key = Registry.LocalMachine.OpenSubKey(RegPath + frm.Name);
                if (key != null)
                {
                    frm.Height = (int)key.GetValue("Height");
                    frm.Width = (int)key.GetValue("Width");
                    frm.Left = (int)key.GetValue("Left");
                    frm.Top = (int)key.GetValue("Top");
                }
            }
        }

        bool a = true;
        bool b = true;
        bool c = true;
        bool d = true;
        public void timer()
        {
            if (timer1.Enabled == true)
            {
                a = true;
                b = true;
                c = true;
                d = true;
                textBox1.Text = "";
                timer1.Enabled = false;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.textBox1.Focus();
            textBox1.Text = "0";
            if (Settings.Default.WindowLocation != null)
            {
                this.Location = Settings.Default.WindowLocation;
            }
            if (Settings.Default.WindowSize != null)
            {
                this.Size = Settings.Default.WindowSize;
            }
        }

        private void btnC_Click(object sender, EventArgs e)
        {
            this.textBox1.Focus();
            textBox1.Text = "0";
            Settings.Default.shuzi1 = "0";
            Settings.Default.shuzi2 = "0";
            timer1.Enabled = false;
        }

        private void btn7_Click(object sender, EventArgs e)
        {
            timer();
            this.textBox1.Focus();
            //this.comboBox1.Focus();
            SendKeys.Send("7");
            System.Threading.Thread.Sleep(100);
            SendKeys.Flush();
        }

        private void btn8_Click(object sender, EventArgs e)
        {
            timer();
            this.textBox1.Focus();
            //this.comboBox1.Focus();
            SendKeys.Send("8");
            System.Threading.Thread.Sleep(100);
            SendKeys.Flush();
        }

        private void btn9_Click(object sender, EventArgs e)
        {
            timer();
            this.textBox1.Focus();
            //this.comboBox1.Focus();
            SendKeys.Send("9");
            System.Threading.Thread.Sleep(100);
            SendKeys.Flush();
        }

        private void btn4_Click(object sender, EventArgs e)
        {
            timer();
            this.textBox1.Focus();
            //this.comboBox1.Focus();
            SendKeys.Send("4");
            System.Threading.Thread.Sleep(100);
            SendKeys.Flush();
        }

        private void btn5_Click(object sender, EventArgs e)
        {
            timer();
            this.textBox1.Focus();
            //this.comboBox1.Focus();
            SendKeys.Send("5");
            System.Threading.Thread.Sleep(100);
            SendKeys.Flush();
        }

        private void btn6_Click(object sender, EventArgs e)
        {
            timer();
            this.textBox1.Focus();
            //this.comboBox1.Focus();
            SendKeys.Send("6");
            System.Threading.Thread.Sleep(100);
            SendKeys.Flush();
        }

        private void btn1_Click(object sender, EventArgs e)
        {
            timer();
            this.textBox1.Focus();
            //this.comboBox1.Focus();
            SendKeys.Send("1");
            System.Threading.Thread.Sleep(100);
            SendKeys.Flush();
        }

        private void btn2_Click(object sender, EventArgs e)
        {
            timer();
            this.textBox1.Focus();
            //this.comboBox1.Focus();
            SendKeys.Send("2");
            System.Threading.Thread.Sleep(100);
            SendKeys.Flush();
        }

        private void btn3_Click(object sender, EventArgs e)
        {
            timer();
            this.textBox1.Focus();
            //this.comboBox1.Focus();
            SendKeys.Send("3");
            System.Threading.Thread.Sleep(100);
            SendKeys.Flush();
        }

        private void btn0_Click(object sender, EventArgs e)
        {
            timer();
            this.textBox1.Focus();
            //this.comboBox1.Focus();
            SendKeys.Send("0");
            System.Threading.Thread.Sleep(100);
            SendKeys.Flush();
        }

        private void btnD_Click(object sender, EventArgs e)
        {
            timer();
            this.textBox1.Focus();
            //this.comboBox1.Focus();
            SendKeys.Send(".");
            System.Threading.Thread.Sleep(100);
            SendKeys.Flush();
        }

        private void btnJia_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            a = false;
            b = true;
            c = true;
            d = true;
            if (textBox1.Text != "")
            {
                Settings.Default.shuzi1 = this.textBox1.Text;
            }
            else
            { Settings.Default.shuzi1 = "0"; }
            Settings.Default.Save();
            System.Threading.Thread.Sleep(10);
            this.textBox1.Text = "";
        }

        private void btnJian_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            a = true;
            b = false;
            c = true;
            d = true;
            if (textBox1.Text != "")
            {
                Settings.Default.shuzi1 = this.textBox1.Text;
            }
            else
            { Settings.Default.shuzi1 = "0"; }
            Settings.Default.Save();
            System.Threading.Thread.Sleep(10);
            this.textBox1.Text = "";
        }

        private void btnCheng_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            a = true;
            b = true;
            c = false;
            d = true;
            if (textBox1.Text != "")
            {
                Settings.Default.shuzi1 = this.textBox1.Text;
            }
            else
            { Settings.Default.shuzi1 = "0"; }
            Settings.Default.Save();
            System.Threading.Thread.Sleep(10);
            this.textBox1.Text = "";
        }

        private void btnChu_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            a = true;
            b = true;
            c = true;
            d = false;
            if (textBox1.Text != "")
            {
                Settings.Default.shuzi1 = this.textBox1.Text;
            }
            else
            { Settings.Default.shuzi1 = "0"; }
            Settings.Default.Save();
            System.Threading.Thread.Sleep(10);
            this.textBox1.Text = "";
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            this.textBox1.Focus();
            if (textBox1.Text == "")
            { textBox1.Text = "0"; }
            Settings.Default.shuzi2 = this.textBox1.Text;
            Settings.Default.Save();
            System.Threading.Thread.Sleep(10);
            timer1.Enabled = true;
        }

        string s1 = "";
        string s2 = "";
        float s = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (a == false)
            {
                try
                {
                    s1 = Settings.Default.shuzi1;
                    s2 = Settings.Default.shuzi2;
                    s = Convert.ToSingle(s1) + Convert.ToSingle(s2);
                    //s = int.Parse(s1) + int.Parse(s2);
                    textBox1.Text = s.ToString();
                }
                catch (Exception)
                { }
            }
            else if (b == false)
            {
                try
                {
                    s1 = Settings.Default.shuzi1;
                    s2 = Settings.Default.shuzi2;
                    s = Convert.ToSingle(s1) - Convert.ToSingle(s2);
                    //s = int.Parse(s1) - int.Parse(s2);
                    textBox1.Text = s.ToString();
                }
                catch (Exception)
                { }
            }
            else if (c == false)
            {
                try
                {
                    s1 = Settings.Default.shuzi1;
                    s2 = Settings.Default.shuzi2;
                    s = Convert.ToSingle(s1) * Convert.ToSingle(s2);
                    //s = int.Parse(s1) * int.Parse(s2);
                    textBox1.Text = s.ToString();
                }
                catch (Exception)
                { }
            }
            else if (d == false)
            {
                try
                {
                    s1 = Settings.Default.shuzi1;
                    s2 = Settings.Default.shuzi2;
                    s = Convert.ToSingle(s1) / Convert.ToSingle(s2);
                    //s = int.Parse(s1) * int.Parse(s2);
                    textBox1.Text = s.ToString();
                }
                catch (Exception)
                { }
            }
        }

        private void btnCE_Click(object sender, EventArgs e)
        {
            this.textBox1.Focus();
            textBox1.Text = "0";
            Settings.Default.shuzi1 = "0";
            Settings.Default.shuzi2 = "0";
        }
       
        private void btnT_Click(object sender, EventArgs e)
        {
            this.textBox1.Focus();

            if (textBox1.Text.Length < 2)
            {
                textBox1.Text = "0";
            }
            else
            {
                SendKeys.Send("{BACKSPACE}");
            }
        }

        private void 复制ToolStripMenuItemCopy_Click(object sender, EventArgs e)
        {
           
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Settings.Default.WindowLocation = this.Location;
            if (this.WindowState == FormWindowState.Normal)
            {
                Settings.Default.WindowSize = this.Size;
            }
            else
            {
                Settings.Default.WindowSize = this.RestoreBounds.Size;
            }
            if (this.WindowState != FormWindowState.Minimized)
                Settings.Default.Save();//使用Save方法保存更改 

            //必须加上这个判断,否则窗体最小化后无法恢复和还原了      
            if (this.WindowState != FormWindowState.Minimized)
                FormPositionHelper.SaveSize(this);
        }
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息