Question

I have a toolStrip1 placed on a Form (System.Windows.Forms) in C# and added five toolStrip buttons to it. Now I wonder how to let the user reorder these buttons by dragging them on another positions within the toolStrip1. I set the toolStrip1.AllowItemReorder to true and the AllowDrop to false as Microsoft in an article suggests.

Now shall be enabled automatic handling of item reorder in the toolStrip1. But it doesn't work - only if I hold ALT-Key pressed the toolStrip1 reacts to the reordering attempts by the user. Have I really to handle the DragEvent, DragEnter, DragLeave myself for avoid holding the Alt Key during reordering the items?

If it so, please give me an example how this events would look like on a toolStrip with toolStripButtons, if I wish to drag the one item on a different position within the toolStrip1 without holding any ALT Keys (like Internet Explorer Favorites does). I am not experienced in this matter.

Was it helpful?

Solution

Well, you may have to use this solution which is a little hacky. The whole idea is you have to press and hold the Alt key by code. I've tried with MouseDown event (even in a PreFilterMessage handler) but it failed. The only event is suitable to hold down the Alt key when it's fired is MouseEnter. You have to register the MouseEnter event handler for all the ToolStripItems, when the mouse leaves one of these items, you have to release the Alt key in the MouseLeave event handler. After the Alt key is released, we have to send the ESC key to make the form active (otherwise, all the hover effects seem to be ignored, even on the Control buttons including Minimize, Maximize, Close). Here is the code which works:

public partial class Form1 : Form {
  [DllImport("user32.dll", SetLastError = true)]
  static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
  public Form1(){
     InitializeComponent();
     //Register event handlers for all the toolstripitems initially
     foreach (ToolStripItem item in toolStrip1.Items){
        item.MouseEnter += itemsMouseEnter;
        item.MouseLeave += itemsMouseLeave;
     }
     //We have to do this if we add/remove some toolstripitem at runtime
     //Otherwise we don't need the following code
     toolStrip1.ItemAdded += (s,e) => {
        item.MouseEnter += itemsMouseEnter;
        item.MouseLeave += itemsMouseLeave;
     };
     toolStrip1.ItemRemoved += (s,e) => {
        item.MouseEnter -= itemsMouseEnter;
        item.MouseLeave -= itemsMouseLeave;
     };
  }
  bool pressedAlt;
  private void itemsMouseEnter(object sender, EventArgs e){
        if (!pressedAlt) {
            //Hold the Alt key
            keybd_event(0x12, 0, 0, 0);//VK_ALT = 0x12
            pressedAlt = true;
        }
  }
  private void itemsMouseLeave(object sender, EventArgs e){
        if (pressedAlt){
            //Release the Alt key
            keybd_event(0x12, 0, 2, 0);//flags = 2  -> Release the key
            pressedAlt = false;
            SendKeys.Send("ESC");//Do this to make the GUI active again
        }            
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top