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 do I add custom snaplines?

I am trying to add custom snaplines to a UserControl, but I can't find any examples or documentation covering snaplines.

A simple example: I would like to create a compound UserControl, containing two controls: Label1 and TextBox1. Then the Parenting UserControl should expose the snaplines of each child control, or, rather, a subset of those.

The docs, as always, are a bit behind the implementation - that should be rectified in the next beta release.

For now, here's a quick sample for you, from the owning feature PM MHarsh:

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Windows.Forms.Design.Behavior;

namespace WindowsApplication1
{
  partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void snaplineControl1_Load(object sender, EventArgs e)
    {}
  }

  [ Designer( typeof( SnaplineControlDesigner ) ) ]
  public class SnaplineControl : UserControl
  {}

  public class SnaplineControlDesigner : ControlDesigner
  {
    public override IList SnapLines
    {
      get
      {
        ArrayList snapLines = base.SnapLines as ArrayList;
        snapLines.Add( 
          new SnapLine( SnapLineType.Baseline, 5, SnapLinePriority.Medium ) );
        return snapLines; 
      }
    }
  }
}



Page view counter