| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2591 人关注过本帖
标题:求助:我的Timer好象启动不了
只看楼主 加入收藏
yanhaolang
Rank: 1
等 级:新手上路
帖 子:60
专家分:7
注 册:2007-3-12
收藏
 问题点数:0 回复次数:4 
求助:我的Timer好象启动不了
public partial class Form1 : Form
    {
        int flag = 1;
        Label lab1 ;
        char[] randchar;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            randchar = new char[52];
            for (int i = 0; i < 26; i++)
            {
                randchar[i] = (char)('a' + i);
               
            }
            for (int i = 26; i < 52; i++)
            {
                randchar[i] = (char)('A' + i - 26);
               
            }   
            
  

        }
        #region 随机生成label
        private void createLab()
        {
            lab1 = new Label();
            Random rand = new Random();
            int randNum= rand.Next(52);
            this.lab1.Text = randchar[randNum].ToString();
            lab1.ForeColor = Color.Red;
            lab1.BackColor = Color.Transparent;
            lab1.Font = new Font("黑体", 18);
            Point p = new Point(rand.Next(0,this.Width), 0);
            this.lab1.Location=p;
            lab1.AutoSize = true;
            this.Controls.Add(lab1);
            Thread td = new Thread(new ThreadStart(movedown));
            td.Start();
        }
        void movedown()
        {
            while (true)
            {
                Point p = new Point(this.lab1.Location.X, this.lab1.Location.Y + 1);
                this.lab1.Location = p;
                this.timer1.Enabled = false;
                if (this.lab1.Location.Y == this.Height)
                {
                   this.timer1.Enabled = true;
                    this.lab1.Dispose();
                    break;                  
                }    //这之后好象 没有触发timer事件,都没有字母掉掉下来,createLab()不执行 ?为什么呢     

                if (flag == 0)
                    break;
               

            }
           // this.lab1.Location.Y = this.lab1.Location.Y + 1;
        }
        #endregion
        string s = "";
        private void timer1_Tick(object sender, EventArgs e)
        {
            createLab();
         
        }      
        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            flag = 0;
        }   


    }
搜索更多相关主题的帖子: Timer int randchar public 
2008-04-10 14:40
sgliang1988
Rank: 1
来 自:西安
等 级:新手上路
帖 子:45
专家分:0
注 册:2007-9-20
收藏
得分:0 
你的代码不全啊,我没给你测出来,下面是我以前做的,你可以参考下:
using System;
using System.Drawing;
using System.Collections;
using
using System.Windows.Forms;
using System.Data;
using System.Threading;

namespace sgl
{
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class frmTypedGame : System.Windows.Forms.Form
    {
        private System.Windows.Forms.Panel panLetterArea;
        private System.Windows.Forms.Button btnStart;
        private System.Windows.Forms.Timer timer;
        private components;
        private System.Windows.Forms.Label labGrade;
        private System.Windows.Forms.Label labGradeVaule;
        int count = 0;

        public frmTypedGame()
        {
            //
            // Windows 窗体设计器支持所必需的
            //
            InitializeComponent();

            //
            // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
            //
        }

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows 窗体设计器生成的代码
        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
             = new ();
            this.panLetterArea = new System.Windows.Forms.Panel();
            this.btnStart = new System.Windows.Forms.Button();
            this.timer = new System.Windows.Forms.Timer();
            this.labGrade = new System.Windows.Forms.Label();
            this.labGradeVaule = new System.Windows.Forms.Label();
            this.SuspendLayout();
            //
            // panLetterArea
            //
            this.panLetterArea.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(224)), ((System.Byte)(224)), ((System.Byte)(224)));
            this.panLetterArea.Location = new System.Drawing.Point(16, 8);
            this.panLetterArea.Name = "panLetterArea";
            this.panLetterArea.Size = new System.Drawing.Size(552, 288);
            this.panLetterArea.TabIndex = 0;
            //
            // btnStart
            //
            this.btnStart.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
            this.btnStart.Location = new System.Drawing.Point(456, 312);
            this.btnStart.Name = "btnStart";
            this.btnStart.Size = new System.Drawing.Size(104, 32);
            this.btnStart.TabIndex = 1;
            this.btnStart.Text = "开始";
            this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
            //
            // timer
            //
            this.timer.Interval = 1500;
            this.timer.Tick += new System.EventHandler(this.timer_Tick);
            //
            // labGrade
            //
            this.labGrade.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
            this.labGrade.Location = new System.Drawing.Point(16, 312);
            this.labGrade.Name = "labGrade";
            this.labGrade.Size = new System.Drawing.Size(56, 24);
            this.labGrade.TabIndex = 2;
            this.labGrade.Text = "分数:";
            this.labGrade.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            //
            // labGradeVaule
            //
            this.labGradeVaule.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
            this.labGradeVaule.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(0)), ((System.Byte)(192)));
            this.labGradeVaule.Location = new System.Drawing.Point(80, 312);
            this.labGradeVaule.Name = "labGradeVaule";
            this.labGradeVaule.Size = new System.Drawing.Size(72, 24);
            this.labGradeVaule.TabIndex = 3;
            this.labGradeVaule.Text = "0";
            this.labGradeVaule.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            //
            // frmTypedGame
            //
            this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
            this.ClientSize = new System.Drawing.Size(592, 350);
            this.Controls.Add(this.labGradeVaule);
            this.Controls.Add(this.labGrade);
            this.Controls.Add(this.btnStart);
            this.Controls.Add(this.panLetterArea);
            this.KeyPreview = true;
            this.MaximizeBox = false;
            this.Name = "frmTypedGame";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "打字游戏";
            this.Closing += new (this.frmTypedGame_Closing);
            this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.frmTypedGame_KeyPress);
            this.ResumeLayout(false);

        }
        #endregion

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.Run(new frmTypedGame());
        }
        //开始按钮。
        private void btnStart_Click(object sender, System.EventArgs e) {
            this.timer.Enabled = !this.timer.Enabled;
            this.btnStart.Text = !this.timer.Enabled ? "开始" : "停止";
        }
        //定时器。
        private void timer_Tick(object sender, System.EventArgs e) {
            Label lab = new Label();
            Random ran = new Random((int)DateTime.Now.Ticks);
            int x = ran.Next(this.panLetterArea.Width+10);
            int ch = ran.Next(97,123);
            lab.Location = new System.Drawing.Point(x,0);
            lab.Name = "lab";
            lab.Size = new System.Drawing.Size(40, 40);
            lab.Font = new System.Drawing.Font("宋体", 22F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
            lab.ForeColor = Color.Purple;
            lab.TabIndex = 0;
            lab.Text = (char)ch + "";
            this.panLetterArea.Controls.Add(lab);
            LabLetter ll = new LabLetter(lab,this.panLetterArea.Height);
        }
        //键盘监听。
        private void frmTypedGame_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) {
            char ch = e.KeyChar;
            foreach(Label la in this.panLetterArea.Controls){
                if( la.Text == ch.ToString() ){
                    this.panLetterArea.Controls.Remove(la);
                    count += 10;
                    this.labGradeVaule.Text = count.ToString();
                    break;
                }
            }
        }
        //关闭。
        private void frmTypedGame_Closing(object sender, e) {
            Application.Exit();
        }
        
    }
    //label类。
    class LabLetter{
        private Label _lab;
        private int _hgt;
        Thread thr;
        //构造。
        public LabLetter( Label lab,int hgt){
            this._lab = lab;
            this._hgt = hgt;
            thr = new Thread(new ThreadStart(Move));
            thr.IsBackground = true;
            thr.Start();
        }
        //移动方法。
        public void Move(){
            lock(this){
                while(true){
                    this._lab.Top += 10;
                    Thread.Sleep(400);
                    if( this._lab.Top == this._hgt ){
                        this._lab = null;
                        this._lab.Dispose();
                        thr.Abort();
                    }
                }
            }
        }
    }
}

认真的对待每一天!
2008-04-10 15:20
yanhaolang
Rank: 1
等 级:新手上路
帖 子:60
专家分:7
注 册:2007-3-12
收藏
得分:0 
你把 代码复制过去,你 做个窗体 就可以调试啊,我的问题是 timer启动不了,
干脆不用 timer ,用多线程 做就不会有问题。。
2008-04-10 15:46
hack214
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2008-4-8
收藏
得分:0 
调试一下!!!!!
if (this.lab1.Location.Y == this.Height)
                {
                   this.timer1.Enabled = true;
                    this.lab1.Dispose();
                    break;     }              
  应该是这一段有问题!!  条件可能不成立!!那么timer1就没法变为true了!!自然就没法往下运行了!建议楼主调试一下!!若不会调试!在这个if段加个:messagebox.show("已经到这里了")来测试一下,是否程序已经运行到这了!!!
2008-04-10 21:37
yanhaolang
Rank: 1
等 级:新手上路
帖 子:60
专家分:7
注 册:2007-3-12
收藏
得分:0 
调试过,设置断点跟踪,监视变量,那里没有问题,而且 this.timer1.Enabled = true 执行了,但是 就是不产生字母往下掉。。。
2008-04-11 09:32
快速回复:求助:我的Timer好象启动不了
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021573 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved