⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 c基本语法的学习
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace dazi
{
    public partial class Form1 : Form
    {      
        Random r = new Random();
        public Form1()
        {
            InitializeComponent();
            Form1.CheckForIllegalCrossThreadCalls = false;
        }
        private void start1()
        {
            if (state)
            {
                //timer1.Enabled=false;
                state = false;
                button1.Text = "开始";
                Application.Exit();
            }
            else
            {
                timer1.Start();
                state = true;
                button1.Text = "停止";
            }
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            //产生label
            Label a = new Label();
            a.Text = Convert.ToChar(r.Next(97, 123)).ToString();
            a.Left = r.Next(0, this.Width);
            a.Font = new Font("宋体", 16F);
            this.Controls.Add(a);//this.Controls返回的是ControlCollection类型,调用此类型的Add()方法
            //初始化线程
            Class1 c = new Class1(a, this);
            Thread T = new Thread(c.move);
            a.Tag = T;//把线程绑到当前产生的label上
            T.Start();
        }
        private bool state=false;
        private void button1_Click(object sender, EventArgs e)
        {
            start1();
        }
        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            foreach (Control con in this.Controls)
            {
                if (con is Label)
                {
                    if (e.KeyChar == Convert.ToChar(con.Text))
                    {
                        con.Dispose();
                        Thread c1 = (Thread)con.Tag;
                        c1.Abort();
                        this.Controls.Remove(con);
                    }
                }
            }
        }

        private void startToolStripMenuItem_Click(object sender, EventArgs e)
        {
            start1();
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -