Reputation: 33
I'm creating an application in C# using Windows Forms. I'm using a ListBox to display some data. For some reason which is too long to explain here, I want to hide the standard right-click menu of a scrollbar on the ListBox (Scroll here, Scroll up, Scroll down etc.)
This standard context menu is displayed on every scrollbar on every control.
Is there any way to completely disable it or replace it with an empty context menu?
Upvotes: 3
Views: 1168
Reputation: 941565
Winforms almost always makes these kind of tweaks very easy to implement. You'll need a minimum understanding of how Windows works, the first handful of chapters in Petzold's "Programming Windows" gets you a long way. And leveraging the Spy++ utility that's included with Visual Studio, it shows you the messages that Windows sends to a window.
You'll see it sending the WM_CONTEXTMENU message when you right-click the scrollbar, that's the one that triggers the context menu. All you have to do is making sure that the native ListBox control cannot see that message. That's a one-liner. Add a new class to your project and copy/paste the code shown below. Compile. And drag the new control from the top of the toolbox onto your form, replacing the old listbox. Presto-chango, no more context menu.
using System;
using System.Windows.Forms;
class MyListBox : ListBox {
protected override void WndProc(ref Message m) {
// Intercept WM_CONTEXTMENU
if (m.Msg != 0x7b) base.WndProc(ref m);
}
}
Upvotes: 5