How to show a ContextMenuStrip instead of cthe default ContextMenuStrip,when you rightclick on the non client area of a window's Form or when alt+space keys are pressed
Answer / Aarti Rani
To display a custom ContextMenuStrip instead of the default one in Windows Forms application when right-clicking on the non-client area (e.g., title bar) or pressing Alt+Space, you can use the PreviewMouseDown event and set the Cancel property to true if certain conditions are met. Here's an example:
1) Create a ContextMenuStrip object with your custom menu items.
2) In the Form constructor, subscribe to the PreviewMouseDown event:
```csharp
this.PreviewMouseDown += new MouseEventHandler(Form_PreviewMouseDown);
```
3) Implement the PreviewMouseDown event handler:
```csharp
private void Form_PreviewMouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right && !this.IsHandleCreated || (e.X >= this.ClientRectangle.Left && e.Y >= this.ClientRectangle.Top && e.X <= this.ClientRectangle.Right && e.Y <= this.ClientRectangle.Bottom))
{
if ((GetAsyncKeyState(0x12) & 0x8000) != 0) // check if Alt key is pressed (Alt+Space)
{
cts.Show(e.Location); // show the custom ContextMenuStrip
e.Handled = true; // cancel the default context menu
}
}
}
```
| Is This Answer Correct ? | 0 Yes | 0 No |
Which dialog box allows users to switch to another area of the application?
Which Isolation level is helps to read uncommitted data?
What is form record?
What is a database form?
Which method grants a lock on a resource?
What is the max size of textbox?
What are the forms of graphics?
Which is the global event handler for unhandled exceptions in an ASP.NET applications?
What is form and its uses?
What are windows based applications examples?
i already displaying one datagrid. now i want to make change to particular column header i.e i want to split that column header and it includes one more header.... write a code for that in windows application using C#.net
What is a fillable form?