Microsoft Communities

Welcome to WindowsClient.net | Sign in | Join

Here are some frequently asked questions about Windows Forms and their answers.

Windows Forms FAQs

How can I make the SplitContainer continuously update its contents while the splitter is moving?

Note: Continuously updating a SplitContainer's contents can lead to poor performance, and should be used sparingly.

By default, when moving a SplitContainer's splitter a preview is shown of the splitter's future location. Then when the splitter is released, the SplitContainer resizes to that position. If you want the SplitContainer to be constantly resizing as the splitter is moving, you can do one of two things:

  1. Use the custom control defined in the ContinuousUpdate sample
  2. Insert the following code in your project, and attach these events to all of the SplitContainers that you want to continuously update.
private void splitContainer_MouseDown(object sender, MouseEventArgs e)
{
   // This disables the normal move behavior
   ((SplitContainer)sender).IsSplitterFixed = true;
}

private void splitContainer_MouseUp(object sender, MouseEventArgs e)
{
   // This allows the splitter to be moved normally again
   ((SplitContainer)sender).IsSplitterFixed = false;
}

private void splitContainer_MouseMove(object sender, MouseEventArgs e)
{
   // Check to make sure the splitter won't be updated by the 
   // normal move behavior also
   if (((SplitContainer)sender).IsSplitterFixed)
   {
      // Make sure that the button used to move the splitter 
      // is the left mouse button
      if (e.Button.Equals(MouseButtons.Left))
      {
         // Checks to see if the splitter is aligned Vertically
         if (((SplitContainer)sender).Orientation.Equals(Orientation.Vertical))
         {
            // Only move the splitter if the mouse is within 
            // the appropriate bounds
            if (e.X > 0 && e.X < ((SplitContainer)sender).Width)
            {
               // Move the splitter
               ((SplitContainer)sender).SplitterDistance = e.X;
            }
         }
         // If it isn't aligned vertically then it must be 
         // horizontal
         else
         {
            // Only move the splitter if the mouse is within 
            // the appropriate bounds
            if (e.Y > 0 && e.Y < ((SplitContainer)sender).Height)
            {
               // Move the splitter
               ((SplitContainer)sender).SplitterDistance = e.Y;
            }
         }
      }
      // If a button other than left is pressed or no button 
      // at all
      else
      {
         // This allows the splitter to be moved normally again
         ((SplitContainer)sender).IsSplitterFixed = false;
      }
   }
}


Page view counter