C# task bar icon question

Discussion in 'OT Technology' started by D1G1T4L, Jul 7, 2005.

  1. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area
    i have an icon for my program running in a task bar once the program is minimized and i want my program to be able to maximize once the user double clicks on the icon

    this is my code
    Code:
    		private void notifyIcon1_DoubleClick(object sender, System.EventArgs e)
    		{
    			//Hide The icon in the System tray:
    			notifyIcon1.Visible = false;
    			//Show the application in the task bar again:
    			this.ShowInTaskbar = true;
    			//Set the window state back to normal:
    			this.WindowState = FormWindowState.Normal;
    		}
    
    and of course
    Code:
    notifyIcon1.DoubleClick += new EventHandler(notifyIcon1_DoubleClick);
    
    this code works but however the program doesnt return to it's original position, it's maximizes near start (windows) button and i have to move it on my own to the center of the screen, is there a way to return it to original positon?
     
  2. Zirconium

    Zirconium OT Supporter

    Joined:
    Jun 21, 2005
    Messages:
    2,236
    Likes Received:
    1
    Location:
    Webster, MA
    How about saving the form's position (and maybe size) when the form is minimized and restore the values when the icon is double clicked.

    Code:
      // Add variables to class/form
      Point oldLocation;
      Size oldSize;
     
      			
      // On minimize
      oldSize = this.Size;
      oldLocation = this.Location;
     
      
      // On maximize
      this.Location = oldLocation;
      this.Size = oldSize;
      
     
  3. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area

    just tried that, doesnt work for some reason =(

    for some reason i minimize it, then i double click on the icon to bring it up, but it only shows the program name in the task bar, but doesnt show the program itself
     
  4. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area
    here is the whole code

    Code:
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    
    namespace DigiBot
    {
    	/// <summary>
    	/// Summary description for Form1.
    	/// </summary>
    	public class MainForm : System.Windows.Forms.Form
    	{
    		private System.Windows.Forms.GroupBox groupBox1;
    		private System.Windows.Forms.GroupBox groupBox2;
    		private System.Windows.Forms.Button buttonAim;
    		private System.Windows.Forms.Button buttonIRC;
    		private System.Windows.Forms.Button buttonBoth;
    		private System.Windows.Forms.Button buttonCreate;
    		private System.Windows.Forms.Button buttonEdit;
    		private System.Windows.Forms.MainMenu mainMenu1;
    		private System.Windows.Forms.MenuItem menuItem1;
    		private System.Windows.Forms.MenuItem menuItem2;
    		private System.Windows.Forms.NotifyIcon notifyIcon1;
    		private System.Windows.Forms.ContextMenu contextMenu1;
    		private System.Windows.Forms.MenuItem menuItem3;
    		private System.Windows.Forms.MenuItem menuItem4;
    		private System.ComponentModel.IContainer components = null;
    
    		Point oldLocation;
    		Size oldSize;
    
    		public MainForm()
    		{
    		
    			// Required for Windows Form Designer support
    			InitializeComponent();
    
    			
    			// TODO: Add any constructor code after InitializeComponent call
    		
    		}
    
    		/// <summary>
    		/// Clean up any resources being used.
    		/// </summary>
    		protected override void Dispose( bool disposing )
    		{
    			if( disposing )
    			{
    				if (components != null) 
    				{
    					components.Dispose();
    				}
    			}
    			base.Dispose( disposing );
    		}
    
    		#region Windows Form Designer generated code
    		/// <summary>
    		/// Required method for Designer support - do not modify
    		/// the contents of this method with the code editor.
    		/// </summary>
    		private void InitializeComponent()
    		{
    			this.components = new System.ComponentModel.Container();
    			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(MainForm));
    			this.buttonAim = new System.Windows.Forms.Button();
    			this.buttonIRC = new System.Windows.Forms.Button();
    			this.buttonBoth = new System.Windows.Forms.Button();
    			this.groupBox1 = new System.Windows.Forms.GroupBox();
    			this.groupBox2 = new System.Windows.Forms.GroupBox();
    			this.buttonEdit = new System.Windows.Forms.Button();
    			this.buttonCreate = new System.Windows.Forms.Button();
    			this.mainMenu1 = new System.Windows.Forms.MainMenu();
    			this.menuItem1 = new System.Windows.Forms.MenuItem();
    			this.menuItem2 = new System.Windows.Forms.MenuItem();
    			this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
    			this.contextMenu1 = new System.Windows.Forms.ContextMenu();
    			this.menuItem3 = new System.Windows.Forms.MenuItem();
    			this.menuItem4 = new System.Windows.Forms.MenuItem();
    			this.groupBox1.SuspendLayout();
    			this.groupBox2.SuspendLayout();
    			this.SuspendLayout();
    			// 
    			// buttonAim
    			// 
    			this.buttonAim.Location = new System.Drawing.Point(16, 40);
    			this.buttonAim.Name = "buttonAim";
    			this.buttonAim.Size = new System.Drawing.Size(88, 32);
    			this.buttonAim.TabIndex = 0;
    			this.buttonAim.Text = "AIM";
    			this.buttonAim.Click += new System.EventHandler(this.buttonAim_Click);
    			// 
    			// buttonIRC
    			// 
    			this.buttonIRC.Enabled = false;
    			this.buttonIRC.Location = new System.Drawing.Point(112, 40);
    			this.buttonIRC.Name = "buttonIRC";
    			this.buttonIRC.Size = new System.Drawing.Size(88, 32);
    			this.buttonIRC.TabIndex = 1;
    			this.buttonIRC.Text = "IRC";
    			// 
    			// buttonBoth
    			// 
    			this.buttonBoth.Enabled = false;
    			this.buttonBoth.Location = new System.Drawing.Point(208, 40);
    			this.buttonBoth.Name = "buttonBoth";
    			this.buttonBoth.Size = new System.Drawing.Size(88, 32);
    			this.buttonBoth.TabIndex = 2;
    			this.buttonBoth.Text = "Both";
    			// 
    			// groupBox1
    			// 
    			this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
    																					this.buttonAim,
    																					this.buttonIRC,
    																					this.buttonBoth});
    			this.groupBox1.Location = new System.Drawing.Point(48, 16);
    			this.groupBox1.Name = "groupBox1";
    			this.groupBox1.Size = new System.Drawing.Size(312, 104);
    			this.groupBox1.TabIndex = 4;
    			this.groupBox1.TabStop = false;
    			this.groupBox1.Text = "Choose the network";
    			// 
    			// groupBox2
    			// 
    			this.groupBox2.Controls.AddRange(new System.Windows.Forms.Control[] {
    																					this.buttonEdit,
    																					this.buttonCreate});
    			this.groupBox2.Location = new System.Drawing.Point(56, 136);
    			this.groupBox2.Name = "groupBox2";
    			this.groupBox2.Size = new System.Drawing.Size(296, 80);
    			this.groupBox2.TabIndex = 5;
    			this.groupBox2.TabStop = false;
    			this.groupBox2.Text = "Accounts";
    			// 
    			// buttonEdit
    			// 
    			this.buttonEdit.Enabled = false;
    			this.buttonEdit.Location = new System.Drawing.Point(152, 32);
    			this.buttonEdit.Name = "buttonEdit";
    			this.buttonEdit.Size = new System.Drawing.Size(72, 32);
    			this.buttonEdit.TabIndex = 1;
    			this.buttonEdit.Text = "Edit";
    			// 
    			// buttonCreate
    			// 
    			this.buttonCreate.Enabled = false;
    			this.buttonCreate.Location = new System.Drawing.Point(56, 32);
    			this.buttonCreate.Name = "buttonCreate";
    			this.buttonCreate.Size = new System.Drawing.Size(72, 32);
    			this.buttonCreate.TabIndex = 0;
    			this.buttonCreate.Text = "Create";
    			// 
    			// mainMenu1
    			// 
    			this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
    																					  this.menuItem1});
    			// 
    			// menuItem1
    			// 
    			this.menuItem1.Index = 0;
    			this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
    																					  this.menuItem2});
    			this.menuItem1.Text = "File";
    			// 
    			// menuItem2
    			// 
    			this.menuItem2.Index = 0;
    			this.menuItem2.Text = "Close";
    			this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click_2);
    			// 
    			// notifyIcon1
    			// 
    			this.notifyIcon1.ContextMenu = this.contextMenu1;
    			this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));
    			this.notifyIcon1.Text = "notifyIcon1";
    			this.notifyIcon1.Visible = true;
    			// 
    			// contextMenu1
    			// 
    			this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
    																						 this.menuItem3,
    																						 this.menuItem4});
    			// 
    			// menuItem3
    			// 
    			this.menuItem3.Index = 0;
    			this.menuItem3.Text = "Maximize";
    			this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
    			// 
    			// menuItem4
    			// 
    			this.menuItem4.Index = 1;
    			this.menuItem4.Text = "Exit";
    			this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);
    			// 
    			// MainForm
    			// 
    			this.AcceptButton = this.buttonAim;
    			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    			this.ClientSize = new System.Drawing.Size(400, 241);
    			this.Controls.AddRange(new System.Windows.Forms.Control[] {
    																		  this.groupBox2,
    																		  this.groupBox1});
    			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
    			this.MaximizeBox = false;
    			this.Name = "MainForm";
    			this.Text = "MainForm";
    			this.Load += new System.EventHandler(this.MainForm_Load);
    			this.groupBox1.ResumeLayout(false);
    			this.groupBox2.ResumeLayout(false);
    			this.ResumeLayout(false);
    
    		}
    		#endregion
    
    		/// <summary>
    		/// The main entry point for the application.
    		/// </summary>
    		[STAThread]
    		static void Main() 
    		{
    			Application.Run(new MainForm());
    		}
    
    		private void buttonAim_Click(object sender, System.EventArgs e)
    		{
    			this.Hide();
    			new AimLogIn(this).Show();
    			
    		}
    
    		private void menuItem2_Click(object sender, System.EventArgs e)
    		{
    			Application.Exit();
    		}
    
    		private void menuItem1_Click(object sender, System.EventArgs e)
    		{
    			 MessageBox.Show("Hello World. This a tray icon application. Bye!");
    		
    		}
    
    		private void menuItem2_Click_1(object sender, System.EventArgs e)
    		{
    			// exits the application
    			Application.Exit();
    		}
    
    		private void menuItem2_Click_2(object sender, System.EventArgs e)
    		{
    			Application.Exit();
    		}
    
    		private void MainForm_Load(object sender, System.EventArgs e)
    		{
    			this.Resize += new EventHandler(MainForm_Resize);
    			notifyIcon1.DoubleClick += new EventHandler(notifyIcon1_DoubleClick);
    		}
    		private void MainForm_Resize(object sender, System.EventArgs e)
    		{
    			//Check to see if the window has been Minimized:
    			if (this.WindowState == FormWindowState.Minimized)
    			{
    				oldSize = this.Size;
    				oldLocation = this.Location;
    
    				//Remove the Program from the Task Bar:
    				this.ShowInTaskbar = false;
    				//Show the Icon in the system tray:
    				notifyIcon1.Visible = true;
    			}
    		}
    		private void notifyIcon1_DoubleClick(object sender, System.EventArgs e)
    		{
    			this.Location = oldLocation;
    			this.Size = oldSize;
    
    			//Hide The icon in the System tray:
    			notifyIcon1.Visible = false;
    			//Show the application in the task bar again:
    			this.ShowInTaskbar = true;
    			//Set the window state back to normal:
    			this.WindowState = FormWindowState.Normal;
    		}
    
    		private void menuItem3_Click(object sender, System.EventArgs e)
    		{
    			this.Location = oldLocation;
    			this.Size = oldSize;
    			//Hide The icon in the System tray:
    			notifyIcon1.Visible = false;
    			//Show the application in the task bar again:
    			this.ShowInTaskbar = true;
    			//Set the window state back to normal:
    			this.WindowState = FormWindowState.Normal;
    		
    		}
    
    		private void menuItem4_Click(object sender, System.EventArgs e)
    		{
    			Application.Exit();
    		}
    
    
    
    	
    
    	
    
    	
    
    	
    	}
    }
    
    
     
  5. kingtoad

    kingtoad OT Supporter

    Joined:
    Sep 2, 2003
    Messages:
    55,923
    Likes Received:
    11
    Location:
    Los Angeles
    I feel like someone is holding my hand when I code in Visual Studio. :embd:
     
  6. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area

    thats great, but how does that help me
     
  7. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area
    anyways i did

    this.CenterToScreen();
    and that works, it brings it to the center

    for some reason this.WindowState = FormWindowState.Normal;

    doesnt show the program
     
    Last edited: Jul 8, 2005
  8. SLED

    SLED build an idiot proof device and someone else will

    Joined:
    Sep 20, 2001
    Messages:
    28,118
    Likes Received:
    0
    Location:
    AZ, like a bauce!
    have you tried calling Form.Show() ?
     

Share This Page