C#控件使用小汇总【二】Timer的使用与Random 随机数的生成

前言

本人在校学生,刚接触到c#开发没多久,随着所学内容增加,渐微察觉大脑不够用,所以开通博客,一来整理学习所得,二来交流分享。小白一个,对于程序开发应用理解有很大不足,对于代码或如有引用不当之处,可以提出改正。今天整理的是Timer的用法与Random随机数的生成,它们的用法比较简单。

Timer的使用

Timer控件和其他控件有不同,它不是一个可视化的控件。它的使用可以从工具箱中拖拽,当然,也可以在代码区直接使用Timer方法声明。Timer控件常用的两个属性分别是:Interval和Enabled。Interval是每次执行Tick事件的时间间隔,例如:当Interval=1000;时,它代表的意义是每隔1秒执行一次Tick事件。Enabled属性是指控件是否启用。

代码以及说明

注:此代码片段完整应用在后面,此处为代码使用说明。

int j = 0, k = 0;//定义两个整型变量
        private void timer2_Tick(object sender, EventArgs e)
        {
            if (j < 356)
            {
                k++;
                int s = 24 + k;
                pictureBox2.Location = new Point(300, s);
                j = k;
                //自增,达到pictureBox2位置下降的目的
            }
            else
            {
                k--;
                int r = 24 + k;
                pictureBox2.Location = new Point(300, r);
                /	自减,达到pictureBox2位置上升的目的
                if (k == 0)
                {j = 0;}//回到初始变量值
            }
        }

Random随机数的生成

Random随机数使用Random方法声明,声明方式为:

Random r = new Random();

当然,还可以定义随机数r的取值范围

Random r = new Random();//声明随机数r
  int i1 = r.Next(0, 480);
  int i2 = r.Next(0, 390);
//Next(0, 480);括号内定义随机数r的取值范围

它配合Timer的使用可以快速得到很多随机数。当然,随机数的获取还有很多方法,这只是其中之一。

代码以及说明

注:此代码片段完整应用在后面,此处为代码使用说明。

private void timer3_Tick(object sender, EventArgs e)
        {
            Random r = new Random();//声明随机数r
            int i1 = r.Next(0, 480);
            int i2 = r.Next(0, 390);
            //Next(0, 480);括号内定义随机数r的取值范围
            pictureBox3.Location = new Point(i2, i1);
            //pictureBox3在窗体中出现的位置
            if (i2 < 160 && i1 < 200)
            {
                label2.Text = "我在左上角";
            }
            else if (i2 > 160 && i1 < 200)
            {
                label2.Text = "我在右上角";
            }
            else if (i2 < 160 && i1 > 200)
            {
                label2.Text = "我在左下角";
            }
            else if (i2 > 160 && i1 > 200)
            {
                label2.Text = "我在右下角";
            }
        }

例子

利用上面提到的控件和方法,做一个小程序。

例子说明

控件:三个pictureBox,两个label。使用Timer控件让三个pictureBox动起来,并且,其中一个pictureBox的位置是随机的。一个label会对随机的pictureBox的位置作出说明,一个label会实时监控pictureBox的位置坐标。

例子效果图

在这里插入图片描述

完整代码区

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;
using System.IO;

namespace timer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        int n = 0, i = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (n < 356)
            {
                i++;
                int s = 24 + i;
                pictureBox1.Location = new Point(13, s);
                label1.Text = i.ToString();
                n = i;
            }
            else
            {
                i--;
                int r = 24 + i;
                pictureBox1.Location = new Point(13, r);
                label1.Text = i.ToString();
                if (i == 0)
                {
                    n = 0;
                }
            }
        }

        int j = 0, k = 0;
        private void timer2_Tick(object sender, EventArgs e)
        {
            if (j < 356)
            {
                k++;
                int s = 24 + k;
                pictureBox2.Location = new Point(300, s);
                j = k;
            }
            else
            {
                k--;
                int r = 24 + k;
                pictureBox2.Location = new Point(300, r);
                if (k == 0)
                {
                    j = 0;
                }
            }
        }

        private void timer3_Tick(object sender, EventArgs e)
        {
            Random r = new Random();
            int i1 = r.Next(0, 480);
            int i2 = r.Next(0, 390);

            pictureBox3.Location = new Point(i2, i1);
            if (i2 < 160 && i1 < 200)
            {
                label2.Text = "我在左上角";
            }
            else if (i2 > 160 && i1 < 200)
            {
                label2.Text = "我在右上角";
            }
            else if (i2 < 160 && i1 > 200)
            {
                label2.Text = "我在左下角";
            }
            else if (i2 > 160 && i1 > 200)
            {
                label2.Text = "我在右下角";
            }

        }
        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            timer2.Enabled = true;
            timer3.Enabled = true;
        }
    }
}

小结:

计算机语言的学习很多时候有必要自己动手去敲代码,这样往往会有很多新收获,而且能够从中的到灵感。锻炼自己的动手能力以及实践能力是一件很快乐的事情。比如,例子窗体中使用到的动态图片都是我自己使用Ps工具做的,在敲完代码后做一些这样的事情是一件很有趣很放松的一件事情。:)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值