开发者

Hidden Features of Visual Studio winforms designer [closed]

开发者 https://www.devze.com 2023-01-24 00:01 出处:网络
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references,or expertise, but this question will likely solicit debate, a
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 10 years ago.

One of the most loved and hated feautures of visual studio must be the form designer.

Creating a simple form/user control layout usually is a breeze. Setting properties and adding events is easy.

Setting up the toolbox to use you own controls can be a bit harder and getting the ToolBoxIcons to show up can be开发者_开发技巧 a pain. Using third party components by visual inheritance can throw of the designer. And using multiple inheritance on designerables can be really hard.

So what are your favorite 'hidden' and or obvious visual studio designer features.


  • Hold down CTRL and drag a control to duplicate it.

NOTE: Be careful, if your control has an event handler that was created by the designer, it will associate your newly created control with the event handler as well as the old control.

  • Use the keyboard shortcut F7 to go from the designer view to the code view, and Shift-F7 to go from the code view to the designer view. I use this constantly

  • The "Document Outline" window is essential for heavy designer use. This allows you to reorder the controls in the outline to bring them to the front or send to back. It's especially useful when you're working with docking controls. You can also move a child control to a different parent, or take it out of a container control, or move a parent into a container, etc...

View -> Other Windows -> Document Outline

  • The "Layout" toolbar is great for lining things up to make a clean looking user interface.

View -> Toolbars -> Layout


When the WinForms project designer is open on a form or user control and you have nested child controls, you can move the focus off of the currently focused child control to its parent control by pressing the Escape key. Pressing the Escape key again moves focus to its parent and so on until you finally reach the top.


Did you know you can actually debug the designer?

You can start up a single solution twice and use 'Attach to process' to debug a devenv environment running your designer controls. This can be especially helpfull to debug 'strange' designer problems that give very little information otherwise.

I used this to pinpoint a designer problem to some remoting that got started and didn't work triggering an unhandled exception and breaking the designer. All I saw at the desigerview was some visual studio internal helper classes crashing.


You can add your own snaplines to UserControls.

The class below here is called CenterSquare, and has a Designer added to it called CenterSquareSnapLines that supplies a list of snaplines inside the control. If you've got your own controls that render smaller then just their borders you can now add your own snaplines.

public class CenterSquareSnapLines : ControlDesigner
{

    public override System.Collections.IList SnapLines
    {
        get
        {
            ArrayList snapLines = base.SnapLines as ArrayList;
            CenterSquare c = this.Control as CenterSquare;
            if (c != null)
            {
                snapLines.Add(new SnapLine(SnapLineType.Left, c.BorderDistance));
                snapLines.Add(new SnapLine(SnapLineType.Right, c.Height - c.BorderDistance));
                snapLines.Add(new SnapLine(SnapLineType.Top, c.BorderDistance));
                snapLines.Add(new SnapLine(SnapLineType.Bottom, c.Width - c.BorderDistance));
                snapLines.Add(new SnapLine(SnapLineType.Top, 5));
            }
            else
            {
                //Debug message here!
            }
            return snapLines;
        }
    }

}

[Designer(typeof(CenterSquareSnapLines))]
public partial class CenterSquare : UserControl
{
 //implementation goes here
}


I try to code my own event handler methods when forms initializes instead of letting the designer take care of it. I have noticed that the designer will lose these events from time to time. By coding your own you make sure they stick.

myButton.Click += new System.EventHandler(this.myButtonClick)


All properties on user controls will be shown by the designer and filled with default values for datatype. Adding the DesignerSerializationVisibility attribute with Hidden as a value will not fill this property with any default values.

<DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>

This is a great way to keep the amound of designer code to a minimum. Another option is DefaultValue.

<DefaultValue(somevalue)>

This will set the designer to use the given value as a default value for the selection.

The designer will override backing field values and initializer values if you do not use these attributes.

Another one that can be hard to find: Menu - View - Tab Order

You can change the tab order by clicking your fields. This option is only visible while the designer is active.


Broken designer:

Sometimes visual studio won't show the switch to Designer/Code option. I haven't nailed down what causes this exactly but mixing vb.net and c# projects while using devexpress seems to be a reason.

As long as visual studio runs it won't suddenly see that my forms/usercontrols can be designed. Closing down visual studio and changing the project file usually helps.

Originally the project file contained

<Compile Include="cboTargetGroupFlights.cs">

Changing that to and reopening the project caused visual studio to recheck all forms/usercontrols and did show the designer again.

<Compile Include="cboTargetGroupFlights.cs">
  <SubType>Component</SubType>
</Compile>

Did you know you can set the code view as default after double clicking something in the solution view. Right click you file and choose Open with... and use the Set as Defaults button.

0

精彩评论

暂无评论...
验证码 换一张
取 消