Disable focus signals on SplitContainer

How do I disable focus signals on a SplitContainer? I ask because I would rather use them myself using OnPaint to make it smoother.

I've tried this:

    protected override bool ShowFocusCues
    {
        get
        {
            return false;
        }
    }

      

And this is my control:

    public class cSplitContainer : SplitContainer
    {
        private bool IsDragging;

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            if (!IsSplitterFixed) IsDragging = true;
            Invalidate();
        }
        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            if (IsDragging)
            {
                IsDragging = false;
                IsSplitterFixed = false;
            }
        }
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            if (IsDragging)
            {
                IsSplitterFixed = true;
                if (e.Button == MouseButtons.Left)
                {
                    if (Orientation == Orientation.Vertical)
                    {
                        if (e.X > 0 && e.X < Width) SplitterDistance = e.X;
                    }
                    else
                    {
                        if (e.Y > 0 && e.Y < Height) SplitterDistance = e.Y;
                    }
                }
                else
                {
                    IsDragging = false;
                    IsSplitterFixed = false;
                }
            }
        }
        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            base.OnPaint(e);
            if (IsDragging)
            {
                e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(127, 0, 0, 0)), Orientation == Orientation.Horizontal ? new Rectangle(0, SplitterDistance, Width, SplitterWidth) : new Rectangle(SplitterDistance, 0, SplitterWidth, Height));
            }
        }
    }

      

but it didn't work. I have also tried some of the other methods mentioned earlier, but I am still getting focus signals.

+3


source to share


5 answers


SplitContainer code is like:

protected override void OnPaint(PaintEventArgs e) {
  base.OnPaint(e);
  if (Focused) {
    DrawFocus(e.Graphics,SplitterRectangle);
  }
}

      

DrawFocus is not virtual. Therefore, you cannot override it.
Focused virtually. Perhaps you can set to false when called base.OnPaint(...)

in your override OnPaint

.



So, you can add the following code (I haven't tested it if it works):

private bool _painting;
public override bool Focused
{
  get { return _painting ? false : base.Focused; }
}

protected override void OnPaint(PaintEventArgs e)
{
  _painting = true;

  try
  {
    base.OnPaint(e);
  }
  finally
  {
    _painting = false;
  }
}

      

This is more of a hack than a simple solution.

+4


source


I don't think you see the FocusCue in the same way as the floating window that is used to move the slider.

If keyboard access is not important, you can try making it unavailable:



public class MySplit : SplitContainer {

  public MySplit() {
    this.SetStyle(ControlStyles.Selectable, false);
  }

  protected override void OnPaint(PaintEventArgs e) {
    e.Graphics.Clear(Color.Red);
  }
}

      

This prevents the SplitContainer from focusing, but your mouse can still interact with it.

+6


source


I searched for this issue and this question rose to the top.

There is a solution and an interesting discussion on the Microsoft forum about stealing the splitter focus without good reason . Next comment: "


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


It might be "by design", but it's not very good. What spitters have you ever seen in any Microsoft production app that even temporarily focuses on the panels they share? I also added the code you suggest and it keeps me from constantly losing focus for the splitter, but I still don't like the fact that my panels hide and show their selection while manipulating the splitters.

This distracting flash of choice is simply not available in most professional applications. It's good enough that it probably isn't worth the time fixing, but not something most people really want. If you respect the TabStop property, or even add the AcceptsFocus property, most people will want it. I think you should add this option to the project in a future version.

- Brendan

+3


source


Easy solution: give focus immediately when you receive it!

Three steps:

  • Create a handler GotFocus

    forSplitContainer

  • Move focus to another control with AnotherControl.Focus()

    .
  • Install TabStop

    inFalse

It's all. The ugly focus mark is never displayed.

Now, one subtlety: what's the other focus control? This is for you. Just grab the first control in the tab order, or the top left control in the right pane of the SplitContainer (TextBox in the ASCII chart below). The ideal solution would be a previous control that had focus, but unfortunately this is not easy to find out: find out the control with last focus , but IMHO - top-left focusable control is a very good answer.

       left pane            right pane
------------------------------------------------
:                     ::                       :
:                     ::   [TextBox] [Button]  :
:                     ::                       :
:                     ::   [Combobox V]        :
:                     ::                       :
------------------------------------------------

      

+2


source


this is a similar question asked on stackoveflow , one solution is used as the one you use along with the showfocuscues override property, you need to override the draw method as well.

0


source







All Articles