计算器以及动画案例

本文介绍了如何将字符串转换为int类型,包括convert.ToInt32和int.Parse方法的使用。接着讲解了在计算器应用中涉及的图片处理,如背景图片、PictureBox控件和ImageList的运用。还讨论了如何在计算器启动时取消光标显示,通过设置TabIndex属性来实现。最后,文章探讨了动画案例,展示在软件中添加动态效果的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1,字符串(string)型转int型

(1)convert.ToInt32(textbox1.Text);

(2)int.Parse(textbox1.Text);

2,有关图片的内容

背景图片:Image

装图片:PictureBox

图片列表:Imagelist

3,计算器进入界面时,取消光标

TabIndex

4,计算器案例
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _1228_第五天
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
//计算器需求分析
//一、界面设计
//	1,做一个显示屏
//	2,17个按钮(0-9 + - * / %  = CE)
//二、实现功能
//	1,输入第一个数
//	2,选择运算符
//	3,输入第二个数
//	4,按下等号键,计算出结果,结果显示在显示屏上
//三、实现步骤
//	1,先做界面
//        a,显示屏 textbox listbox label都可以
//        b,使用17个button,button上的文本改成对应的数字符号
//	2,
//		申请两个int类的变量,第一个num1装第一个数字,第二个num2装第二个数字
//        a, 输入第一个数字,当点击第一个数字按钮时,屏幕上显示一个,之前显示的数字在前面待着;
//			a1,添加按钮的click事件
//            a2,事件触发,将按钮代表的数字显示textbox1的text
//        b, 当输入符号时,清除屏幕,但是后台必须记录好第一个数字
//             b1,添加符号按钮的click事件
//             b2,当点任何一个符号按钮用一个变量装刚才输入的textbox1中的数字
//         c, 输入第二个数字,当点击第二个数字按钮时,屏幕上显示一个,之前显示的数字在前面待着;
//		d,按下等号按钮,显示屏上面的文本改变成两个数字的运算结果
        //1
        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text += "1";
        }
        //2
        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text += "2";
        }
        //3
        private void button3_Click(object sender, EventArgs e)
        {
            textBox1.Text += "3";
        }
        //4
        private void button4_Click(object sender, EventArgs e)
        {
            textBox1.Text += "4";
        }
        //5
        private void button5_Click(object sender, EventArgs e)
        {
            textBox1.Text += "5";
        }
        //6
        private void button6_Click(object sender, EventArgs e)
        {
            textBox1.Text += "6";
        }
        //7
        private void button7_Click(object sender, EventArgs e)
        {
            textBox1.Text += "7";
        }
        //8
        private void button8_Click(object sender, EventArgs e)
        {
            textBox1.Text += "8";
        }
        //9
        private void button9_Click(object sender, EventArgs e)
        {
            textBox1.Text += "9";
        }
        //0
        private void button10_Click(object sender, EventArgs e)
        {
            textBox1.Text += "0";
        }
        //num1表示第一个数,num2表示第二个数
        int num1;
        int num2;
        //表示的是装运算符号
        string fun = "";
        //+
        private void button11_Click(object sender, EventArgs e)
        {
            jisuanqi();
            fun = "+";
        }

        private void jisuanqi()
        {
            num1 = Convert.ToInt32(textBox1.Text);
            textBox1.Text = "";
        }
        //方法 一个代码模块
        //-
        private void button12_Click(object sender, EventArgs e)
        {
            jisuanqi();
            fun = "-";
        }
        //*
        private void button13_Click(object sender, EventArgs e)
        {
            jisuanqi();
            fun = "*";
        }
        // ÷
        private void button14_Click(object sender, EventArgs e)
        {
            jisuanqi();
            fun = "/";
        }
        //%
        private void button15_Click(object sender, EventArgs e)
        {
            jisuanqi();
            fun = "%";
        }
        //=
        private void button17_Click(object sender, EventArgs e)
        {
            //记录第二个数字
            num2 = Convert.ToInt32(textBox1.Text);
            textBox1.Text = "";
            //判断运用了哪个运算符
            if (fun=="+")
            {
                textBox1.Text =/*num1+"+"+num2+"="+*/(num1 + num2).ToString();
            }
            if (fun == "-")
            {
                textBox1.Text = /*num1+"-"+num2+"="+*/(num1 - num2).ToString();
            }
            if (fun == "*")
            {
                textBox1.Text = /*num1+"*"+num2+"="+*/(num1 * num2).ToString();
            }
            if (fun == "/")
            {
                textBox1.Text = /*num1 + "/" + num2 + "=" + */(num1 / num2).ToString();
            }
            if (fun == "%")
            {
                textBox1.Text = num1 + "%" + num2 + "=" + (num1 % num2).ToString();
            }
        }

        private void button16_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            textBox1.ReadOnly = true;
            //textBox1.TabStop = false;
            //button17.Select();
            button17.TabIndex = 0;
        }
    }
}

5,动画案例

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _1228_第五天
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            //复合的索引,默认索引从0开始
            pictureBox1.Image = imageList1.Images[0];
            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
        }
        int i = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            i++;
            pictureBox1.Image = imageList1.Images[i];
            if (i==4)
            {
                i = -1;
            }
        }

        private void Form2_KeyDown(object sender, KeyEventArgs e)
        {
            pictureBox1.Left +=2;
            if (pictureBox1.Left+pictureBox1.Width>=this.Width)
            {
                pictureBox1.Left = 0;
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值