Articles in this section
Category / Section

How to prevent the diagram area from scrolling down while adding a new element in WinForms Diagram?

2 mins read

In our WinForms Diagram control, you can prevent the diagram area from scrolling down while adding a new element by overriding the GetBoundingRect method in the custom DiagramController class. By updating the bounds of the model, you can maintain the original behavior where the top-left corner of the diagram remains fixed. We have provided a code snippet to demonstrate how to achieve this.


this.diagram1.Controller = new CustomDiagramController();

public class CustomDiagramController : DiagramController
{
   public override System.Drawing.RectangleF GetBoundingRect(System.Collections.ICollection nodes, MeasureUnits units)
   {
       Model model = this.Model;

       // get model node collection
       NodeCollection modelNodes = Model.Nodes;

       // get content bounds
       RectangleF rcNodeBounds;

       SizeF m_originoffset = new SizeF(new PointF(0, 0));
       RectangleF returnBounds = new RectangleF(m_originoffset.ToPointF(), SizeF.Empty);
       foreach (Node node in modelNodes)
       {
           //Caluculates the total bounds of the model content
           rcNodeBounds = ((IUnitIndependent)node).GetBoundingRectangle(model.MeasurementUnits, false);
           returnBounds = RectangleF.Union(returnBounds, rcNodeBounds);
       }
       //Updates the position of the origin based on the model content.
       m_originoffset.Width -= returnBounds.X;
       m_originoffset.Height -= returnBounds.Y;

       //Updates the Model's size
       returnBounds.Width = Math.Max(model.MinimumSize.Width + m_originoffset.Width, returnBounds.Width);
       returnBounds.Height = Math.Max(model.MinimumSize.Height + m_originoffset.Height, returnBounds.Height);
       return returnBounds;
   }

}

ScreenCapture_1-22-2025113116AM-ezgifcom-video-to-gif-converter.gif

Conclusion:

I hope you enjoyed learning about how to prevent the diagram area from scrolling down while adding a new element in WinForms Diagram

You can refer to our WinForms Diagram feature tour page to learn about its other groundbreaking feature representations and documentation. You can also explore our WinForms Diagram example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied