回 帖 发 新 帖 刷新版面

主题:[讨论]关于启动界面时用的timer控件问题[求助]

this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Elapsed);

运行后错误提示为上句中的:“System.Windows.Forms.Timer”并不包含“Elapsed”的定义。

这就很郁闷了,如果系统自己没定义timer的这个Elapsed事件的话偶该怎么办??

偶用的是vs.net2005..生成的主要3段:

From1.cs:

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

namespace qidongjiemian
{
    public partial class start : Form
    {
        public start()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender,EventArgs e)
        {

        }

        private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (this.Opacity < 1)
            {
                this.Opacity += 0.01;
                progressBar1.Value = (int)(this.Opacity * 100);
            }
            else
            {
                timer1.Enabled = false; Dispose(false); (new MainWindow()).Show();
            }
        }
    }
}

Form1.Designer.cs:

namespace qidongjiemian
{
    partial class start
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(start));
            this.progressBar1 = new System.Windows.Forms.ProgressBar();
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.SuspendLayout();
            // 
            // progressBar1
            // 
            this.progressBar1.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.progressBar1.Location = new System.Drawing.Point(0, 419);
            this.progressBar1.Name = "progressBar1";
            this.progressBar1.Size = new System.Drawing.Size(557, 12);
            this.progressBar1.TabIndex = 0;
            // 
            // timer1
            //
            this.timer1.Enabled = true;
            this.timer1.Interval = 50;
            this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Elapsed);/*就是这句出了错....*/
            // 
            // start
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
            this.ClientSize = new System.Drawing.Size(557, 431);
            this.Controls.Add(this.progressBar1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.MinimizeBox = false;
            this.Name = "start";
            this.Opacity = 0;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Form1";
            this.TopMost = true;
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.ProgressBar progressBar1;
        private System.Windows.Forms.Timer timer1;


    }
}

Program.cs:

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace qidongjiemian
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new start());
        }
    }
}

--------------------
至于新添加的MainWindow.cs就不用写出来了....


偶的错误主要出在 Form1.Desinger.cs  中的那句中.....

希望各位大大们能给我提示,帮助偶解决这个问题...谢谢了.....

回复列表 (共6个回复)

沙发

大侠们帮帮忙啊.....谢谢拉....

板凳

System.Windows.Forms.Timer是Tick事件
System.Timers.Timer是Elapsed事件
不要用混了

3 楼

谢谢大大的提示..

但我每次在vs.net2005的工具箱向form1中拖timer控件时,它都把我放在System.Windows.Forms这个类(能说是类吗?)里。怎么拖放才能被放在System.Timers这个类里呢?我不能每次用timer时自己去定义啊.....

汗....这就是菜鸟自学的种种苦恼.....

非常谢谢大家给我提示....

4 楼

System.Windows.Forms.Timer是控件
System.Timers.Timer是类

控件用拖或者动态创建都可以
类只有手动创建了new Timer().....什么的

5 楼

哦...原来是这个样子哦...

6 楼

转载自:
[url=http://www.msproject.cn/article/SplashForm.aspx]另一种SplashForm[/url]
zhang7cy 著于2007-11-13 13:51:27
本文介绍如何在.NET下实现闪屏Splash Screen。选择一个你比较喜欢的用在闪屏的图片,并选择其中一部分做为显示的部分,同时你可以设定特殊的颜色去填充不显示的部分。

使用闪屏窗体

这个界面在类中使用是非常简单的。在程序的开始时调用StartSplash的方法并指定图片的位置和显示为透明的颜色的值。在你的程序的初始化的过程中,这个控件会很快的建立和显示。

using System;
using Abhinaba.Splasher;
using System.Drawing;

SplashForm.StartSplash(@".\splash.bmp", 
        Color.FromArgb(128, 128, 128));

所有的初始化被完成,调用静态方法CloseSplash关闭闪屏窗体。

SplashForm.CloseSplash();

如果想显示一个如About dialog窗体一样的窗体可以使用 ShowModal 方法。

SplashForm.ShowModal(@".\splash.bmp", 
             Color.FromArgb(128, 128, 128));

在顶端

在StartSplash里 , 创建一个新的线程调用MySplashThreadFunc方法

// Create and Start the splash thread Thread InstanceCaller = new Thread(new ThreadStart(MySplashThreadFunc));
InstanceCaller.Start();

这个方法将在一个新的线程里建立splash form

private static void MySplashThreadFunc()
{
    m_instance = new SplashForm(m_imageFile, m_transColor);
    m_instance.TopMost = false;
    m_instance.ShowDialog();
}

下面是构造窗体并对图片透明的部分进行处理。
 

public SplashForm(String imageFile, Color col)
{
    this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
    this.ShowInTaskbar = false;
    this.TopMost = true;

    // make form transparent     this.TransparencyKey = this.BackColor;

    // tie up the events     //...          // load and make the bitmap transparent     m_bmp = new Bitmap(imageFile);
    m_bmp.MakeTransparent(col);

    // resize the form to the size of the iamge     this.Width = m_bmp.Width;
    this.Height = m_bmp.Height;

    // center the form     this.StartPosition = 
       System.Windows.Forms.FormStartPosition.CenterScreen;

    // thread handling. Used because the window
     // will be closed from a different thread
     m_delegateClose = new DelegateCloseSplash(InternalCloseSplash);}

打印事件中将显示图片。

private void SplashForm_Paint(object sender, 
               System.Windows.Forms.PaintEventArgs e)
{
    e.Graphics.DrawImage(m_bmp, 0,0);
}

关闭窗体要用到 CloseSplash 方法. 因为这个 splash form 被运行在不同的线程里,所以它不可能立即被关闭,所以要用到下面的代码。 

public delegate void DelegateCloseSplash();
private DelegateCloseSplash m_delegateClose;

public SplashForm(String imageFile, Color col)
{
    // ...     m_delegateClose = new DelegateCloseSplash(InternalCloseSplash);
    // ... }  public static void CloseSplash()
{
    m_instance.Invoke(m_instance.m_delegateClose);
}

我来回复

您尚未登录,请登录后再回复。点此登录或注册