3

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.

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

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...)

Ian Ringrose
  • 51,220
  • 55
  • 213
  • 317
Filini
  • 2,109
  • 2
  • 22
  • 32
  • Perhaps you could catch the event where it gets focus. Remember the previously focused object, and focus back on that on MouseUp. TabStop will take care of the keyboard part. – Timo Apr 22 '15 at 19:40

3 Answers3

2

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

Matt Hanson
  • 3,458
  • 7
  • 40
  • 61
1

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;
    }
}
new bie
  • 2,745
  • 6
  • 24
  • 26
0

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.

Hope that helps.

joshlrogers
  • 1,136
  • 1
  • 11
  • 20
  • unfortunately, it's not fine for our architecture. I have a usercontrol which validates with the standard Validating event, and I cannot change its behaviour because other external controls glitch (that's the problem, when u move the splitcontainer bar, the leave/validatong events are fired, but the focus then remains on the bar, and my usercontrol freezes for some reason). the thing that bugs me is that this doesn't happen with a normal splitter, which gets moved without getting focus. eventually, I think I will just use a splitter – Filini Jul 24 '09 at 00:43
  • I have tried and I can not replicate the behavior of the focus remaining on the bar. In all of my attempts the focus immediately returned to the control that had the focus before clicking on the splitter. Sorry I was not able to help. – joshlrogers Jul 24 '09 at 18:42
  • Even if it worked (but I think that Validating fires anyway), I would have to put this code in all my Details UserControls (about 15 of them). It's not manageable. I hoped to find a solution with some code put in the SplitContainer, not in the other controls. Thanks anyway :) – Filini Jul 28 '09 at 16:36
  • 1
    having focus trigger validation is a bad design. – Warren P Mar 17 '10 at 01:56