Around and About .NET World

Il blog di Marco Minerva
posts - 1671, comments - 2232, trackbacks - 2135

My Links

News

Contattami su Live Messenger:


MCTS: Windows, Web, Distributed Applications & SQL Server

MCPD: Enterprise Applications

Tag Cloud

Archives

Post Categories

Links

Visualizzare automaticamente la descrizione dei comandi di menu

In molte applicazioni, quando si sposta il puntatore del mouse sopra un comando di menu, nella barra di stato appare un messaggio che ne illustra la funzionalità. Per aggiungere questa caratteristica ai propri programmi, basta specificare la descrizione del comando nella sua proprietà Tag, quindi inserire nel form il seguente codice:

1 #region Status Bar Message Methods 2 3 private ToolStripStatusLabel _statusBar; 4 5 private void InitMenuDescription(ToolStripStatusLabel statusBar) 6 { 7 foreach (ToolStripMenuItem menuItem in this.MainMenuStrip.Items) 8 { 9 menuItem.MouseEnter += new EventHandler(menu_MouseEnter); 10 menuItem.MouseLeave += new EventHandler(menu_MouseLeave); 11 foreach (ToolStripItem item in menuItem.DropDownItems) 12 { 13 item.MouseEnter += new EventHandler(menu_MouseEnter); 14 item.MouseLeave += new EventHandler(menu_MouseLeave); 15 if (item is ToolStripMenuItem) 16 this.AddStatusBarMenuMessage((ToolStripMenuItem)item); 17 } 18 } 19 _statusBar = statusBar; 20 } 21 22 private void AddStatusBarMenuMessage(ToolStripMenuItem item) 23 { 24 foreach (ToolStripItem subItem in item.DropDownItems) 25 { 26 subItem.MouseEnter += new EventHandler(menu_MouseEnter); 27 subItem.MouseLeave += new EventHandler(menu_MouseLeave); 28 if (subItem is ToolStripMenuItem) 29 this.AddStatusBarMenuMessage((ToolStripMenuItem)subItem); 30 } 31 } 32 33 private void menu_MouseEnter(object sender, EventArgs e) 34 { 35 ToolStripItem menu = ((ToolStripItem)sender); 36 if (menu.Tag != null) 37 { 38 string message = menu.Tag.ToString(); 39 if (!message.EndsWith(".")) 40 message += "."; 41 _statusBar.Text = message; 42 } 43 else 44 { 45 _statusBar.Text = string.Empty; 46 } 47 } 48 49 private void menu_MouseLeave(object sender, EventArgs e) 50 { 51 _statusBar.Text = string.Empty; 52 } 53 54 #endregion

La routine InitMenuDescription deve essere richiamata nel costruttore del fom, specificando l'oggetto su cui visualizzare le descrizioni dei menu. Ad esempio:

public Form1() { InitializeComponent(); InitMenuDescription(toolStripStatusLabel1); }

Il funzionamento dell'algoritmo è semplice. Il metodo InitMenuDescription esamina il MainMenu del form e, ricorsivamente per ogni elemento, registra gli eventi MouseEnter e MouseLeave (righe 7-18). La routine di gestione dell'evento MouseEnter verifica che la proprietà Tag del comando su cui si trova il mouse sia impostata e, in tal caso, ne visualizza il contenuto all'interno della barra di stato (righe 36-46).

Print | posted on venerdì 2 febbraio 2007 17:53 | Filed Under [ C# ]

Comments have been closed on this topic.

Powered by:
Powered By Subtext Powered By ASP.NET