2012-12-22 55 views
0

我想要一个忽略光标键并且只能用鼠标控制的c#拆分容器。我怎样才能做到这一点?这样我就可以在一个侧面板上使用键盘输入,而无需同时移动分割。SplitContainer,如何停止光标键输入?

+0

我猜这是一个Winforms应用程序,但如果你真的_told_我们,那么我不需要猜测。 –

+0

这里面临更大的问题,那些面板无法接收焦点,因此它们也不会接受键盘输入。你试图达到的目标很不明确。 –

+0

它是一个winform应用程序。该程序是一种地图编辑器,一边是地图,另一边是一些GUI小工具。此时如果按下光标键,地图会按照预期接收输入和滚动,但分割也是如此。 – user540062

回答

0

您可以禁用键盘输入来处理控件的KeyDown事件,如果需要,您可以在输入与特定键匹配时处理事件。

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 
    // } 
} 

此外,可以根据从其KeyDown事件

收集的 KeyCode从通过取消 SplitterMoving事件 SplitContainer控制的移动停止的分离器
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 
} 

谢谢,
我希望对您有所帮助:)

+0

我尝试了第一种方法,但它似乎不起作用。似乎拆分容器已经使用了关键事件,因为它获得了该回调。第二种方法虽然更成功。谢谢。 – user540062

1

使用e.Handled = true或e.SuppressKeyPress = true来防止密钥从调整分离器,我没有工作。

我能够通过在KeyDown上设置IsSplitterFixed = true和在MouseDown/MouseMove上设置IsSplitterFixed = false(允许通过鼠标调整大小)来实现。

例如

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; 
    }