0

I want a c# split container that ignores cursor keys and can only be controlled with the mouse. How can I do this? This is so I can use the keyboard input in one of the side panels without simultaneously moving the split.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
user540062
  • 61
  • 1
  • 3
  • You have a bigger problem here, those panels cannot receive the focus so they won't take keyboard input either. It is very unclear what you are trying to achieve. – Hans Passant Dec 22 '12 at 06:25
  • Its a winform application. The program is a sort of map editor with a map on one side and some gui gadget down the other. At the moment if I press the cursor keys, the map recieves the input and scrolls as expected but also so does the split. – user540062 Dec 22 '12 at 08:38

2 Answers2

1

Using e.Handled = true or e.SuppressKeyPress = true to prevent the keys from resizing the splitter didn't work for me.

I was able to do it by setting IsSplitterFixed = true on KeyDown, and IsSplitterFixed = false on MouseDown/MouseMove (to allow resizing by mouse).

e.g.

    public Form1()
    {
        InitializeComponent();

        splitContainer1.MouseMove += splitContainer1_MouseMove;
        splitContainer1.KeyDown += splitContainer1_KeyDown;
        splitContainer1.MouseDown += splitContainer1_MouseDown;
    }

    void splitContainer1_MouseDown(object sender, MouseEventArgs e)
    {
        splitContainer1.IsSplitterFixed = false;
    }

    void splitContainer1_MouseMove(object sender, MouseEventArgs e)
    {
        splitContainer1.IsSplitterFixed = false;
    }

    void splitContainer1_KeyDown(object sender, KeyEventArgs e)
    {
        splitContainer1.IsSplitterFixed = true;
    }
RickL
  • 2,811
  • 3
  • 22
  • 35
0

You may disable the keyboard input handling the KeyDown event of the control and if required, you may handle the event if the input matches specific keys.

Example

splitContainer1.KeyDown += new KeyEventHandler(splitContainer1_KeyDown); //Link the KeyDown event of splitContainer1 to splitContainer1_KeyDown

private void splitContainer1_KeyDown(object sender, KeyEventArgs e)
{
  //  if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Down || e.KeyCode == Keys.Left || e.KeyCode == Keys.Right) //Continue if one of the arrow keys was pressed
  //  {

          e.Handled = true; //Handle the event
  //  }
}

Moreover, you may stop the splitter from moving by canceling the SplitterMoving event of the SplitContainer control according to the KeyCode gathered from its KeyDown event

Example

Keys KeyCode; //This is the variable we will use to store the KeyCode gathered from the KeyDown event into. Then, check if it matches any of the arrow keys under SplitterMoving event canceling the movement if the result was true
splitContainer1.KeyDown += new KeyEventHandler(splitContainer1_KeyDown); //Link the KeyDown event of splitContainer1 to splitContainer1_KeyDown
splitContainer1.SplitterMoving += new SplitterCancelEventHandler(splitContainer1_SplitterMoving); //Link the SplitterMoving event of splitContainer1 to splitContainer1_SplitterMoving

private void splitContainer1_SplitterMoving(object sender, SplitterCancelEventArgs e)
{
    if (KeyCode == Keys.Up || KeyCode == Keys.Down || KeyCode == Keys.Left || KeyCode == Keys.Right) //Continue if one of the arrow keys was pressed
    {
        KeyCode = Keys.A; //Reset the KeyCode
        e.Cancel = true; //Cancel the splitter movement
    }
}
private void splitContainer1_KeyDown(object sender, KeyEventArgs e)
{
    KeyCode = e.KeyCode; //Set KeyCode to the KeyCode of the event
 // e.Handled = true; //Handle the event
}

Thanks,
I hope you find this helpful :)

Picrofo Software
  • 5,475
  • 3
  • 23
  • 37
  • I tried the first method but it doesn't seem to work. Seems that the split container has already used the key event by it gets to that callback. The second method is more succesful though. Thanks. – user540062 Dec 22 '12 at 08:30