将用户控件从工具箱拖到 Windows 窗体上时,如何强制用户控件具有固定高度? [英] How do I force a user control to have a fixed height already when dragging it from the toolbox onto a Windows form?

查看:96
本文介绍了将用户控件从工具箱拖到 Windows 窗体上时,如何强制用户控件具有固定高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want my newly designed user control to have a certain fixed height, right when being double-clicked within or drag'n'dropped from the toolbox onto its parent form.

就目前而言,无论是在用户控件的设计者碰巧拥有的高度上,还是在其中显示用户控件.

For now, doing either does display the user control just in the height the user control's designer happened to have.

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

When starting the application with the constituent user control, the UC is displayed in the desired height, when stopping the app, it retains that height. This is due to the following code:

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

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

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

Where do I need to initialize the UC's height, so that it is applied right when dragged from the toolbox?

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

Giving the UC's designer an initial fixed height is no option:the 20 pixels above are just an example, the correct height is variable and based on calculations. These calculations involve ParentForm.Font.

推荐答案

听起来您希望在将用户控件放置在 WinForm 设计图面上时自动计算其大小.

It sounds like you want to automatically compute the user control's size when it is placed on the WinForm design surface.

当一个控件被添加到设计器时,它的 Site 属性被设置.此属性允许您访问 System.ComponentModel.Design 命名空间.使用 IServiceProvider 访问这些服务.GetService 方法.Site 属性属于 ISite 类型,ISite 实现了 IServiceProvider.

When a control is added to the designer, its Site property is set. This property allows you to access various design services defined in the System.ComponentModel.Design Namespace. These services are accessed using the IServiceProvider.GetService Method. The Site property is of type ISite and ISite implements IServiceProvider.

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

The service of interest is the IDesignerHost service as it allows you to obtain the root component (Control) that is being designed (in this case the Form).

以下是覆盖用户控件的 Site 属性以访问设计器服务的简单方法.

The following a simplistic way of overriding the usercontrol's Site property to gain access to the designer services.

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

<小时>

响应评论寻求自定义控件设计时功能的学习资源.


In response to comment seeking learning resources for custom control design-time features.

通常,我会告诉海报自己研究这个,但这些信息最多很难找到,而且我从来没有遇到过关于 WinForm 设计器功能的明确资源.

Normally, I would tell a poster to research this them self, but this information is at best difficult to locate and I have never come across a definitive resource on the WinForm's designer features.

但是,这里有一些我觉得有用的文章/博文.跟踪这些在 MS 不断变化的文档系统中的最终位置是一种痛苦.因此,我建议您将您认为有用的任何内容制作个人副本,因为链接可能会更改或完全消失.

However, here are some articles/blog posts that I have found useful. Keeping track of where these end up in MS's ever changing documentation system is a pain. So I advise that you make a personal copy of anything you find useful, because link will likely change or disappear completely.

建筑具有丰富设计时功能的 Windows 窗体控件和组件(Michael Weinhardt 和 Chris Sells),MSDN 杂志 2003 年 4 月下载

为 .NET 组件编写自定义设计器(Shawn Burke)

演练:创建一个利用 Visual Studio 设计时功能的 Windows 窗体控件

裁缝您的应用程序通过使用 .NET 构建自定义表单设计器(作者 Sayed Y. Hashimi),MSDN 杂志 2004 年 12 月下载

利用自定义控件(作者 Stephen Perry)- 第 1 部分

利用自定义控件(作者 Stephen Perry) - 第 2 部分

如何:访问设计时服务

演练:在设计时调试自定义 Windows 窗体控件

最后一篇文章有​​一节为设计时调试设置项目",非常重要.对于许多设计时功能,您可以尝试在使用它们的项目中开发它们,但这样做有几个陷阱;其中最少的是失去了适当的调试功能.

This last article has a section "Setting Up the Project for Design-Time Debugging" and is very important. For many design-time features, you can try to develop them in the project in which they are being used, but there are several pitfalls to doing so; the least of which is loss of proper debugging capabilities.

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

The first is that you likely make Visual Studio become unstable at some point. This can be minimized by closing all active design surfaces before modifying the control's code and performing a Re-Build after modification, but eventually things will go wonky and you will have to restart Visual Studio.

第二个也是更隐蔽的问题是,我发现某些类型的设计器代码在原位开发时与推荐的方法不同.自定义 TypeConverter 将使用适当的技术完美运行,但在原位开发时会失败.

The second and more insidious issue is that I have found that certain types of designer code works differently when developed in-situ versus the recommended method. A custom TypeConverter will function perfectly using the proper technique and fail miserably when developed in-situ.

这篇关于将用户控件从工具箱拖到 Windows 窗体上时,如何强制用户控件具有固定高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆