Visual Studio winforms设计器的隐藏功能

发布于 2024-10-01 06:24:28 字数 216 浏览 9 评论 0原文

视觉工作室最让人又爱又恨的功能之一一定是表单设计师。

创建简单的表单/用户控件布局通常很容易。设置属性和添加事件很容易。

设置工具箱以使用您自己的控件可能会有点困难,并且显示 ToolBoxIcons 可能会很痛苦。通过视觉继承使用第三方组件可能会让设计者感到困惑。在可设计对象上使用多重继承可能非常困难。

那么您最喜欢的“隐藏”和/或明显的视觉工作室设计师功能是什么。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

月竹挽风 2024-10-08 06:24:29

当 WinForms 项目设计器在窗体或用户控件上打开并且您有嵌套子控件时,您可以通过按 Escape 键将焦点从当前获得焦点的子控件移至其父控件。再次按 Escape 键会将焦点移至其父级,依此类推,直到最终到达顶部。

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.

一个人的夜不怕黑 2024-10-08 06:24:29

您知道您实际上可以调试设计器吗?

您可以启动单个解决方案两次,并使用“附加到进程”来调试运行设计器控件的 devenv 环境。这对于调试“奇怪”的设计问题特别有帮助,否则这些问题只能提供很少的信息。

我用它来查明一些远程处理的设计器问题,这些远程处理已启动但无法触发未处理的异常并破坏设计器。我在 desigerview 看到的只是一些 Visual Studio 内部辅助类崩溃了。

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.

小矜持 2024-10-08 06:24:29

您可以将自己的对齐线添加到用户控件中。

下面的类称为 CenterSquare,并添加了一个名为 CenterSquareSnapLines 的设计器,该设计器提供控件内的对齐线列表。如果您自己的控件渲染得比边框更小,您现在可以添加自己的对齐线。

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
}

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
}
待"谢繁草 2024-10-08 06:24:29

我尝试在表单初始化时编写自己的事件处理程序方法,而不是让设计者来处理它。我注意到设计师有时会丢失这些事件。通过编写自己的代码,您可以确保它们坚持下去。

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

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)
余生共白头 2024-10-08 06:24:29

用户控件上的所有属性将由设计器显示并填充数据类型的默认值。
添加具有 Hidden 作为值的 DesignerSerializationVisibility 属性不会使用任何默认值填充此属性。

<DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>

这是将设计器代码数量保持在最低限度的好方法。另一个选项是默认值。

<DefaultValue(somevalue)>

这将设置设计者使用给定值作为选择的默认值。

如果您不使用这些属性,设计器将覆盖支持字段值和初始值设定项值。

另一种可能很难找到:
菜单 - 视图 - Tab 键顺序

您可以通过单击字段来更改 Tab 键顺序。此选项仅在设计器处于活动状态时可见。

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.

明月松间行 2024-10-08 06:24:29

损坏的设计器:

有时 Visual Studio 不会显示切换到设计器/代码选项。我还没有确定到底是什么原因造成的,但在使用 devexpress 时混合 vb.net 和 c# 项目似乎是一个原因。

只要 Visual Studio 运行,它就不会突然发现我的表单/用户控件可以设计。关闭 Visual Studio 并更改项目文件通常会有所帮助。

最初,项目文件包含

<Compile Include="cboTargetGroupFlights.cs">

将其更改为并重新打开项目,导致 Visual Studio 重新检查所有表单/用户控件,并再次显示设计器。

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

您是否知道在双击解决方案视图中的某些内容后可以将代码视图设置为默认值。右键单击您的文件并选择打开方式...并使用设置为默认值按钮。

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.

紙鸢 2024-10-08 06:24:28
  • 按住 CTRL 并拖动控件以复制它。

注意:如果您的控件有
由创建的事件处理程序
设计师,它会关联你的
带有事件的新创建的控件
处理程序以及旧控件。

  • 使用键盘快捷键 F7 从设计器视图转到代码视图,使用 Shift-F7 从代码视图转到设计器视图。我经常使用这个

  • “文档大纲”窗口对于设计师的大量使用至关重要。这允许您对大纲中的控件重新排序,将它们置于前面或置于后面。当您使用停靠控件时,它特别有用。您还可以将子控件移动到不同的父控件,或将其从容器控件中取出,或将父控件移动到容器中,等等...

View ->其他窗口 ->文档大纲

  • “布局”工具栏非常适合排列内容以形成简洁的用户界面。

查看 ->工具栏->布局

  • 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文