是否可以避免Focus on SplitContainer?

时间:2022-08-31 23:16:54

The WinForm SplitContainer gets the focus when it's dragged or clicked, while the Splitter does not. The side-effect of this, is that dragging a SplitContainer bar fires Leave/Validate on other controls, and I need to avoid this.

WinForm SplitContainer在拖动或单击时获得焦点,而Splitter则不会。这样做的副作用是,拖动SplitContainer栏会激活其他控件上的Leave / Validate,我需要避免这种情况。

I already tried setting TabStop and CausesValidation to False, but with no success.

我已经尝试将TabStop和CausesValidation设置为False,但没有成功。

Is there a way to stop the SplitContainer from getting focused? (not a big deal, I can still use the old Splitter, but I lose some nice VS properties...)

有没有办法阻止SplitContainer聚焦? (不是什么大不了的,我仍然可以使用旧的Splitter,但是我失去了一些不错的VS属性...)

3 个解决方案

#1


Remove the SplitContainer control and replace it manually with Panel and Splitter controls. A little more effort, but a much cleaner outcome.

删除SplitContainer控件并使用Panel和Splitter控件手动替换它。多一点努力,但结果更清晰。

#2


Try with this code:

试试这段代码:

//This code will move the focus from the splitContainer to TreeView shortly after moved.
private void splitContainer1_SplitterMoved(object sender, SplitterEventArgs e) {
    if(this.splitContainer1.CanFocus) {
       this.splitContainer1.ActiveControl = this.treeView1;
    }
}

#3


Filini,

The only time that the splitcontainer would have focus is when you are actually moving the splitter. So I would so something like this in your validating and leave events.

拆分容器唯一能够聚焦的时间是实际移动分离器的时间。所以我在验证和离开事件时会这样。

private void Button_Leave(object sender, EventArgs e)
{
    if(SplitContainer.ContainsFocus)
        return;
}

I reproduced your issue and when I added the above it still calls the event of course, but the code execution doesn't occur because the SplitContainer has focus while you are moving the splitter.

我重现了你的问题,当我添加上面的内容时,它仍然会调用事件,但代码执行不会发生,因为SplitContainer在移动分割器时具有焦点。

Hope that helps.

希望有所帮助。

#1


Remove the SplitContainer control and replace it manually with Panel and Splitter controls. A little more effort, but a much cleaner outcome.

删除SplitContainer控件并使用Panel和Splitter控件手动替换它。多一点努力,但结果更清晰。

#2


Try with this code:

试试这段代码:

//This code will move the focus from the splitContainer to TreeView shortly after moved.
private void splitContainer1_SplitterMoved(object sender, SplitterEventArgs e) {
    if(this.splitContainer1.CanFocus) {
       this.splitContainer1.ActiveControl = this.treeView1;
    }
}

#3


Filini,

The only time that the splitcontainer would have focus is when you are actually moving the splitter. So I would so something like this in your validating and leave events.

拆分容器唯一能够聚焦的时间是实际移动分离器的时间。所以我在验证和离开事件时会这样。

private void Button_Leave(object sender, EventArgs e)
{
    if(SplitContainer.ContainsFocus)
        return;
}

I reproduced your issue and when I added the above it still calls the event of course, but the code execution doesn't occur because the SplitContainer has focus while you are moving the splitter.

我重现了你的问题,当我添加上面的内容时,它仍然会调用事件,但代码执行不会发生,因为SplitContainer在移动分割器时具有焦点。

Hope that helps.

希望有所帮助。