简体   繁体   中英

Reverse order of control alignment in VS 2010 designer

This one is a pure matter of habits. I've been using Visual Studio C++ dialog designer for years. So now I'm working with a .NET C# form designer in VS 2010.

Say, I have the following two controls:

在此输入图像描述

and I need to align the top one to match the bottom. So in VS C++ designer I would first click on the control that I wanted to adjust, then Ctrl+click on the control that I wanted to adjust to, and then select "Align Lefts" toolbar button:

在此输入图像描述

which perfectly made sense.

But now in VS 2010, I have to do this process totally in reverse, ie first select the control to align to and then select the control being aligned. I know that I'm nitpicking here, but this totally messes me up. I'm constantly doing it in the wrong order, and then have to undo and redo it again. The rhetorical yell that usually comes out is, " Why, Microsoft! Why? " But that is not my question here.

Is there some setting, or may a registry fix to make it act like VS C++ designer used to work?

There is an ISelectionService which helps to get or set selected components and also notifies when selection changes.

You can achieve the desired behavior by handling SelectionChanged event of the service as follows:

private void SelectionSVC_SelectionChanged(object sender, EventArgs e)
{
    var selectionSVC = sender as ISelectionService;
    if (selectionSVC == null) return;
    try
    {
        selectionSVC.SelectionChanged -= SelectionSVC_SelectionChanged;
        if ((Control.ModifierKeys & (Keys.Shift | Keys.Control)) > Keys.None)
        {
            var selection = selectionSVC.GetSelectedComponents()
                .Cast<object>().LastOrDefault();
            if (selection != null)
                selectionSVC.SetSelectedComponents(new[] { selection },
                    SelectionTypes.Primary | SelectionTypes.Add);
        }
    }
    finally
    {
        selectionSVC.SelectionChanged += SelectionSVC_SelectionChanged;
    }
}

Getting an instance of the service, depends to the context. For example using a VS Package in a designer window , having an instance of IdesignerHost :

var selectionSVC = (ISelectionService)host.GetService(typeof(ISelectionService));
if (selectionSVC != null)
    selectionSVC.SelectionChanged += SelectionSVC_SelectionChanged;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM