curiosity
curiosity

Reputation: 1233

How to set the min/max distance for the Splitter control?

I want to set the minimum and maximum distance for the splitterdistance to move around... how to set that... i tried using panelMinSize it didnt worked...

Actually in one of the panel of the splitter i have a control and the splitter should adjust based on the control..

Upvotes: 3

Views: 8643

Answers (3)

John89
John89

Reputation: 57

I know this is really old, but for any future visitors an alternative option might be to set the Cursor.Clip property. This what I did for my application:

C#

    private bool VSplitterMouseDown = false;

    private void SplitContainerMouseDown(object sender, MouseEventArgs e)
    {
        VSplitterMouseDown = false;
    }

    private void SplitContainerMouseUp(object sender, MouseEventArgs e)
    {
        Cursor.Clip = null;
    }

    private void SplitContainerSplitterMoved(object sender, SplitterCancelEventArgs e)
    {
        if (VSplitterMouseDown)
        {
            if (!VSplitterMoving)
            {
                VSplitterMoving = true;
                Rectangle bounds = RectangleToScreen(VSplitter.Bounds);
                Cursor.Clip = new Rectangle(bounds.X, bounds.Y, bounds.Width, 120);
            }
        }
    }

VB.Net

    Private VSplitterMoving As Boolean = False

    Private Sub SplitContainerMouseDown(sender As Object, e As MouseEventArgs) Handles VSplitter.MouseDown
        VSplitterMoving = False
    End Sub

    Private Sub SplitContainerMouseUp(sender As Object, e As MouseEventArgs) Handles VSplitter.MouseUp
        Cursor.Clip = Nothing
    End Sub

    Private Sub SplitContainerSplitterMoved(sender As Object, e As SplitterCancelEventArgs) Handles VSplitter.SplitterMoving
        If Not VSplitterMoving Then
            VSplitterMoving = True
            Dim bounds As Rectangle = RectangleToScreen(VSplitter.Bounds)
            Cursor.Clip = New Rectangle(bounds.X, bounds.Y, bounds.Width, 120)
        End If
    End Sub

Upvotes: 1

SwDevMan81
SwDevMan81

Reputation: 49988

You could set the SplitContainer's SplitterDistance to the control's width (assuming you have a vertical split container, use the height if you have a horizontal split container) in the constructor after InitializeComponent();

Then you can attach to the SplitterMoved event and make sure the SplitterDistance is larger than the Controls width.

For example:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SplitterDistanceTest
{
   public partial class Form1 : Form
   {
      public Form1()
      {
         InitializeComponent();
         splitContainer1.SplitterDistance = groupBox1.Width;
         splitContainer1.SplitterMoved += new SplitterEventHandler(splitContainer1_SplitterMoved);
      }

      void splitContainer1_SplitterMoved(object sender, SplitterEventArgs e)
      {
         ResizeSplitterDistance();
      }

      private const int MAXIMUM_SIZE = 200;

      private void ResizeSplitterDistance()
      {    
         if (splitContainer1.SplitterDistance < groupBox1.Width)
         {
            splitContainer1.SplitterDistance = groupBox1.Width;
         }
         if (splitContainer1.SplitterDistance > MAXIMUM_SIZE)
         {
            splitContainer1.SplitterDistance = MAXIMUM_SIZE;
         }

         // You could also do max/min percentages.  Ive shown this below
         // but commented out
         /*int minimum_percent = 30;
         int minimum_size = (int)((minimum_percent / 100m) * (decimal)splitContainer1.Width);
         int maximum_percent = 50;
         int maximum_size = (int)((maximum_percent / 100m) * (decimal)splitContainer1.Width);
         if (splitContainer1.SplitterDistance < minimum_size)
         {
            splitContainer1.SplitterDistance = minimum_size;
         }
         if (splitContainer1.SplitterDistance > maximum_size)
         {
            splitContainer1.SplitterDistance = maximum_size;
         }*/
      }
   }
}

Upvotes: 3

serhio
serhio

Reputation: 28586

If you don't use a FixedPanel, play with MinSize of panel1 and panel2.

Say, Panel1MinSize = 20 and panel2MinSize = 50

Upvotes: 6

Related Questions