【问题标题】:SplitContainer, how do stop cursor key input?SplitContainer,如何停止光标键输入?
【发布时间】:2012-12-22 03:23:47
【问题描述】:

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

【问题讨论】:

  • 我猜这是一个 Winforms 应用程序,但如果你真的告诉我们,那我就不需要猜测了。
  • 这里有一个更大的问题,这些面板无法接收焦点,因此它们也不会接受键盘输入。目前还不清楚您要达到的目标。
  • 它是一个winform应用程序。该程序是一种地图编辑器,一侧是地图,另一侧是一些 gui 小工具。目前,如果我按下光​​标键,地图会收到输入并按预期滚动,但拆分也是如此。

标签: c# winforms keyboard cursor splitcontainer


【解决方案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;
    }

【讨论】:

    【解决方案2】:

    您可以禁用处理控件的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
    }
    

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

    【讨论】:

    • 我尝试了第一种方法,但它似乎不起作用。似乎拆分容器已经使用了关键事件,因为它到达了该回调。第二种方法虽然更成功。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-29
    • 2016-06-15
    • 1970-01-01
    • 2011-10-08
    • 1970-01-01
    • 2020-02-25
    相关资源
    最近更新 更多