none
SplitContainer Splitter Gets Focus Rectangle?

    Question

  • Hello,

    I'm developing a Windows Forms application using tjhe VS .NET 2005 July CTP bits. What a tremendous piece of software! Miles ahead of previous versions.

    One thing I'm noticing is that the new SplitContainer control gets a "Focus Rectangle" during moving that persists even after the resize is complete. I believe that this is a bug, since I've seen no other application ever with a focus rectangle on the splitter...

    Can anyone comment? Is this normal UI behavior, and/or is it an open bug?


    Thank you,

    --Mike
    Friday, August 05, 2005 12:23 AM

Answers

  • Hi Mike,

    The focus issue you mentioned is by design, however to get the performance you want, you can use the following workaround:



    // Temp variable to store a previously focused control
    private Control focused = null;

    private void splitContainer1_MouseDown(object sender, MouseEventArgs e)
    {
       // Get the focused control before the splitter is focused
       focused = getFocused(this
    .Controls);
    }

    private Control getFocused(Control.ControlCollection controls)
    {
       foreach (Control c in
    controls)
       {
          if (c.Focused
    )
          {
             // Return the focused control
             return
    c;
          }
          else if
    (c.ContainsFocus)
          {
             // If the focus is contained inside a control's children
             // return the child
             return
    getFocused(c.Controls);
          }
       }
       // No control on the form has focus
       return null
    ;
    }

    private void splitContainer1_MouseUp(object sender, MouseEventArgs e)
    {
       // If a previous control had focus
       if (focused != null
    )
       {
          // Return focus and clear the temp variable for 
          // garbage collection
          
    focused.Focus();
          focused =
    null
    ;
       }
    }

     



    Let me know if you need any more help with this.

    Scott Morrison
    WinForms PM
    Microsoft Corp.

    Monday, August 08, 2005 8:22 PM

All replies

  • BTW I have taken a screenshot of my app showing to focus rectangle, but can't seem to figure out how to post images on this forum... If an image is needed, I can provide one.

    --Mike


    Friday, August 05, 2005 12:31 AM
  • Another quick follow up.

    Worse than the focus rectangle, the splitter is actually STEALING the focus! This means that if you're in an edit control, typing, and resize the splitter to make the edit control larger, and then resume typing--the edit control won't get your keystrokes because it no longer has the focus! I've confirmed that focus is taken away from both edit controls (one panel) and a listview (the other panel).

    This focus behavior occurs even if I set "TabStop" on the SplitContainer to false.

    --Mike

    Friday, August 05, 2005 8:37 PM
  • I've posted a bug:


    SplitContainer gets focus rectangle after splitter resize, and thus steals focus
    http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=5c1e1b16-750c-468a-bdbe-32c3fbf30253



    Sunday, August 07, 2005 4:15 AM
  • Hi Mike,

    The focus issue you mentioned is by design, however to get the performance you want, you can use the following workaround:



    // Temp variable to store a previously focused control
    private Control focused = null;

    private void splitContainer1_MouseDown(object sender, MouseEventArgs e)
    {
       // Get the focused control before the splitter is focused
       focused = getFocused(this
    .Controls);
    }

    private Control getFocused(Control.ControlCollection controls)
    {
       foreach (Control c in
    controls)
       {
          if (c.Focused
    )
          {
             // Return the focused control
             return
    c;
          }
          else if
    (c.ContainsFocus)
          {
             // If the focus is contained inside a control's children
             // return the child
             return
    getFocused(c.Controls);
          }
       }
       // No control on the form has focus
       return null
    ;
    }

    private void splitContainer1_MouseUp(object sender, MouseEventArgs e)
    {
       // If a previous control had focus
       if (focused != null
    )
       {
          // Return focus and clear the temp variable for 
          // garbage collection
          
    focused.Focus();
          focused =
    null
    ;
       }
    }

     



    Let me know if you need any more help with this.

    Scott Morrison
    WinForms PM
    Microsoft Corp.

    Monday, August 08, 2005 8:22 PM
  • Scott:

    Your solution worked perfectly. Thank you so much for taking the time to provide the code... It's modular enough to where it just dropped into the right locations in my project.

    The behavior is now exactly as desired.

    Thank you again. I'll update the "bug" with your solution on the msdn product feedback site. EDIT: The bug on the lab.msdn site has been updated to reflect this. I think this behavior and the workaround code provided should be included in updated documentation for the SplitContainer control.

    --Mike
    Monday, August 08, 2005 9:11 PM
  • Scott,

    You wouldn't happen to have any insight on my other SplitContainer "issue"?

    SplitContainer "Continuous Layout" or "Show Contents During Reizing" Option

    Link:

    http://forums.microsoft.com/msdn/ShowPost.aspx?PostID=68177


    --Mike
    Monday, August 08, 2005 9:16 PM
  • It has been resolved, and code for a workaround has been attached.

    Scott Morrison
    WinForms PM
    Microsoft Corp.
    Tuesday, August 09, 2005 4:28 AM
  • It may be "by design", but it is not a very good one.  What spitters have you ever seen in any Microsoft production application that even temporarily take the focus from the panes they split?  I also added the code you suggest, and it does keep me from permanently losing the focus to the splitter, but I still don't like the fact that my panes hide and show their selections during splitter manipulation.

    This distracting selection flash just is not present in most professional applications.  It is just good enough that it probably won't be worth my time to fix for a while, but not what most people really want.  If you respected the TabStop property or even added a AcceptsFocus property, most people would want this off.  I think you should add this option to the design in a future version.

    --Brendan
    Tuesday, January 13, 2009 3:49 PM
  • If you respected the TabStop property or even added a AcceptsFocus property, most people would want this off.  I think you should add this option to the design in a future version.

    I second that.

    The earlier Splitter control does not steal the focus, which is a correct or expected behavior. SplitContainer allows to use keyboard. But this should be optional as it is less likely to be used. Loosing and setting focus looks unprofessional.

    Does anyone have a better workaround?


    • Edited by NiranjanMK Thursday, March 22, 2012 1:48 PM
    Thursday, March 22, 2012 1:47 PM