Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WinForms: Alternative to SplitContainer?

Are there any alternative controls someone can suggest to replace the WinForms SplitContainer? I don't like how the SplitContainer shows that weird, dotted strip when its selected and when its being dragged. I want to have the panels re-size as the user drags instead of on mouse up and not show any dotted strips when the splitter is being dragged. Basically like how all the re-sizing of panels is done in windows explorer on vista.

This is the dotted thing I'm talking about:

splitter
(source: bhslaughter.com)

like image 330
Telanor Avatar asked Apr 23 '10 19:04

Telanor


People also ask

Is winform free?

Windows Forms (WinForms) is a free and open-source graphical (GUI) class library included as a part of Microsoft .

What is WinForms good for?

Windows Forms is a UI framework for building Windows desktop apps. It provides one of the most productive ways to create desktop apps based on the visual designer provided in Visual Studio. Functionality such as drag-and-drop placement of visual controls makes it easy to build desktop apps.

What is split container in C#?

The SplitContainer control provides the functionality of a splitter to divide and resize two controls. In this article, we will discuss how to create and use a SplitContainer control in a Windows Forms application. Creating a SplitContainer. A SplitContainer has two built-in panels and a splitter.


2 Answers

Write your own split container UserControl. You basically just drop two Panels onto the control (for the left and right panels) and then let the space between them be the splitter. A little MouseDown, MouseMove and MouseUp logic on the UserControl itself will let you easily move the "splitter" left and right, and the two panels will properly block this everywhere but over the splitter, so your logic for checking if the mouse if over the splitter is as simple as can be.

It may be a little extra work getting the control to act the way you want it to act in design mode.

like image 161
MusiGenesis Avatar answered Nov 15 '22 07:11

MusiGenesis


I found this after I saw your question, so thought I would share: SplitContainer FAQ

The second link on there tells exactly what you need to do.

Here is the text from that just in case the link ever dies.

//1.  Use the custom control defined in the SplitContainerNoFocus sample
//2. Insert the following code in your project, and attach these events to all of the SplitContainers that you don't want stealing focus.

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

private void splitContainer_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 splitContainer_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;
   }
}
like image 24
Thymine Avatar answered Nov 15 '22 06:11

Thymine