将用户控件从工具箱拖到 Windows 表单时,如何强制用户控件具有固定高度?

How do I force a user control to have a fixed height already when dragging it from the toolbox onto a Windows form?

我希望我新设计的用户控件有一定的固定高度,当在其中双击或从工具箱拖放到其父窗体时。

目前,执行任一操作都会在用户控件的设计器恰好具有的高度显示用户控件。

使用组成用户控件启动应用程序时,UC 显示在所需的高度,停止应用程序时,它会保留该高度。这是由于以下代码:

Private Sub UTest_Load(sender As Object, e As EventArgs) _
    Handles MyBase.Load

    ...
    Me.Height = 20
    ...
End Sub

我需要在哪里初始化 UC 的高度,以便在从工具箱中拖动时正确应用它?

给 UC 的设计师一个初始固定高度是没有的 option:the 以上 20 像素只是一个例子,正确的高度是可变的并且基于计算。这些计算涉及 ParentForm.Font.

听起来您想在将用户控件放置在 WinForm 设计图面上时自动计算它的大小。

将控件添加到设计器后,将设置其 Site 属性。 属性 允许您访问 System.ComponentModel.Design Namespace. These services are accessed using the IServiceProvider.GetService Method 中定义的各种设计服务。 Site 属性 是 ISite 类型,ISite 实现 IServiceProvider.

感兴趣的服务是 IDesignerHost 服务,因为它允许您获取正在设计的根组件 (Control)(在本例中为 Form)。

以下是覆盖用户控件的 Site 属性 以获得对设计器服务的访问权限的简单方法。

Imports System.ComponentModel.Design

Public Class UCTest
   Public Overrides Property Site() As System.ComponentModel.ISite
      Get
         Return MyBase.Site
      End Get
      Set(ByVal value As System.ComponentModel.ISite)
         MyBase.Site = value
         If value IsNot Nothing Then SetDesignerSize()
      End Set
   End Property

   Private Sub SetDesignerSize()
      Dim host As IDesignerHost = DirectCast(Me.Site.GetService(GetType(IDesignerHost)), IDesignerHost)
      If host IsNot Nothing Then
         ' host.RootComponent is typically the form but can be another design surface like a usercontrol
         Dim parent As Control = TryCast(host.RootComponent, Control)
         If parent IsNot Nothing Then
            Dim frm As Form = parent.FindForm
            If frm IsNot Nothing Then
               Me.Height = frm.Font.Height * 5
               Me.Width = frm.Font.Height * 10
            End If
         End If
      End If
   End Sub
End Class

编辑:回应寻求自定义控件 design-time 功能学习资源的评论。

通常,我会告诉张贴者自己研究这个,但这些信息充其量很难找到,而且我从未遇到过有关 WinForm 设计器功能的权威资源。

不过,这里有一些 articles/blog 我觉得有用的帖子。跟踪这些在 MS 不断变化的文档系统中的位置是一件痛苦的事情。因此,我建议您将您认为有用的任何内容都制作一份个人副本,因为 link 可能会完全改变或消失。

Building Windows Forms Controls and Components with Rich Design-Time Features (by Michael Weinhardt and Chris Sells), MSDN Magazine April 2003 Download

Writing Custom Designers for .NET Components (by Shawn Burke)

Walkthrough: Creating a Windows Forms Control That Takes Advantage of Visual Studio Design-Time Features

Tailor Your Application by Building a Custom Forms Designer with .NET (by Sayed Y. Hashimi), MSDN Magazine December 2004 Download

Leverage Custom Controls (By Stephen Perry) - Part1

Leverage Custom Controls (By Stephen Perry) - Part2

How to: Access Design-Time Services

Walkthrough: Debugging Custom Windows Forms Controls at Design Time

最后这篇文章有一节 "Setting Up the Project for Design-Time Debugging",非常重要。对于许多 design-time 功能,您可以尝试在使用它们的项目中开发它们,但是这样做有几个陷阱;其中最少的是失去适当的调试能力。

首先,您可能会使 Visual Studio 在某些时候变得不稳定。这可以通过在修改控件代码之前关闭所有活动设计界面并在修改后执行 Re-Build 来最小化,但最终事情会变得不稳定,您将不得不重新启动 Visual Studio。

第二个也是更隐蔽的问题是,我发现某些类型的设计器代码在开发时 in-situ 与推荐方法的工作方式不同。自定义 TypeConverter 将使用适当的技术完美运行,并在开发时悲惨地失败 in-situ.