Mega Code Archive

 
Categories / C# Tutorial / GUI Windows Forms
 

Use Timer to update NotifyIcon

using System; using System.Drawing; using System.Windows.Forms; public class NotifyIconTimerUpdate : Form {     private Icon[] images = new Icon[8];     int index = 0;     public NotifyIconTimerUpdate()     {         InitializeComponent();         images[0] = new Icon("1.ico");         images[1] = new Icon("2.ico");         images[2] = new Icon("3.ico");         images[3] = new Icon("4.ico");         images[4] = new Icon("5.ico");         images[5] = new Icon("1.ico");         images[6] = new Icon("2.ico");         images[7] = new Icon("3.ico");     }     private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)     {         notifyIcon.Icon = images[index];         index++;         if (index > 7)             index = 0;     }     [STAThread]     public static void Main(string[] args)     {         Application.Run(new NotifyIconTimerUpdate());     }     private System.Windows.Forms.NotifyIcon notifyIcon;     private System.Timers.Timer timer;          private System.ComponentModel.IContainer components = null;     private void InitializeComponent()     {         this.components = new System.ComponentModel.Container();         this.notifyIcon = new System.Windows.Forms.NotifyIcon(this.components);         this.timer = new System.Timers.Timer();         ((System.ComponentModel.ISupportInitialize)(this.timer)).BeginInit();         this.SuspendLayout();         //          // notifyIcon         //          this.notifyIcon.Text = "notifyIcon1";         this.notifyIcon.Visible = true;         //          // timer         //          this.timer.Enabled = true;         this.timer.Interval = 1000;         this.timer.SynchronizingObject = this;         this.timer.Elapsed += new System.Timers.ElapsedEventHandler(this.timer_Elapsed);         //          // NotifyIconTimerUpdate         //          this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);         this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;         this.ClientSize = new System.Drawing.Size(340, 174);         ((System.ComponentModel.ISupportInitialize)(this.timer)).EndInit();         this.ResumeLayout(false);     } }