Reputation: 93
I have created a toolstipmenuitem in which i added a lot of toolstripmenuitems as sub menus. And then i restricted the maximum size of the toolstripmenuitem which results in:
Vertical scrolling automatically becomes enabled as shown in the image.
But I need a horizontal scrolling. Is there any way to achieve this?
Upvotes: 0
Views: 1028
Reputation: 1
A working solution:
MouseWheel
, Opened
, Closed
event of your ToolStripDropDown
in the Load event of the form dropDown.Opened+= new EventHandler(dropDown_Opened);
dropDown.Closed+= new ToolStripDropDownClosedEventHandler(dropDown_Closed);
dropDown.MouseWheel+= new MouseEventHandler(dropDown_MouseWheel);
public static class Keyboard
{
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern uint keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
const byte VK_UP = 0x26; // Arrow Up key
const byte VK_DOWN = 0x28; // Arrow Down key
const int KEYEVENTF_EXTENDEDKEY = 0x0001; //Key down flag, the key is going to be pressed
const int KEYEVENTF_KEYUP = 0x0002; //Key up flag, the key is going to be released
public static void KeyDown()
{
keybd_event(VK_DOWN, 0, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(VK_DOWN, 0, KEYEVENTF_KEYUP, 0);
}
public static void KeyUp()
{
keybd_event(VK_UP, 0, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(VK_UP, 0, KEYEVENTF_KEYUP, 0);
}
}
Opened
, Closed
, MouseWheel
events: bool IsMenuStripOpen = false;
void dropDown_MouseWheel(object sender, MouseEventArgs e)
{
if (IsMenuStripOpen)
{
if (e.Delta > 0)
{
Keyboard.KeyUp();
}
else
{
Keyboard.KeyDown();
}
}
}
void dropDown_Closed(object sender, ToolStripDropDownClosedEventArgs e)
{
IsMenuStripOpen = false;
}
void dropDown_Opened(object sender, EventArgs e)
{
IsMenuStripOpen = true;
}
void dropDownMenuScrollWheel(ToolStripDropDown dropDown)
{
dropDown.Opened +=new EventHandler(dropDown_Opened);
dropDown.Closed +=new ToolStripDropDownClosedEventHandler(dropDown_Closed);
dropDown.MouseWheel += new MouseEventHandler(dropDown_MouseWheel);
}
ToolStripDropDown is ToolStripMenuItem.DropDown
Hope it help you.
Upvotes: 0