Serialcoder en Français Serialcoder in English
TEL : +33 (0)9 72 13 15 17

Windows Forms FAQ resources

10. Windows Forms Menus

10.14 How can I make the context menu to close after a set time interval?


To automatically close the context menu after a set time interval, you can use a Timer and send a ESC key stroke after the desired time interval as shown:


[C#]
private void timer1_Tick(object sender, System.EventArgs e)
{
     SendKeys.Send("{ESC}");
     timer1.Stop();
}
private void contextMenu1_Popup(object sender, System.EventArgs e)
{
     //set interval to 5 seconds
     timer1.Interval = 5000;
     timer1.Start();
}

[VB.Net]

Private Sub timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs)
     SendKeys.Send("{ESC}")
     timer1.Stop()
End Sub
Private Sub contextMenu1_Popup(ByVal sender As Object, ByVal e As System.EventArgs)
     'set interval to 5 seconds
     timer1.Interval = 5000
     timer1.Start()
End Sub