SplitContainer,如何停止光标键输入

本文关键字:输入 光标 何停止 SplitContainer | 更新日期: 2023-09-27 17:49:19

我想要一个c#分割容器,它忽略光标键,只能用鼠标控制。我该怎么做呢?这样我就可以在一侧面板上使用键盘输入,而不用同时移动分割线。

SplitContainer,如何停止光标键输入

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

您可以禁用键盘输入处理控件的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,您可以通过取消SplitContainer控制的SplitterMoving事件来阻止分离器移动

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
}

谢谢,
希望对你有帮助。