C# WindowsForms UserControl 的控件设计器支持 [英] C# WindowsForms UserControl's Controls Designer Support

查看:34
本文介绍了C# WindowsForms UserControl 的控件设计器支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找的是对用户控件内部控件的相同类型的设计器支持.即 - 调整文本框大小,移动标签,在用户控件内将用户控件放置到表单上后.

What I am looking for is the same type of designer support for controls inside a usercontrol. ie - resizing a textbox, moving a label, that are inside a usercontrol after placeing the usercontrol on to a form.

我能做什么...

  1. 创建用户控件
  2. 使用设计器向它添加控件
  3. 创建一个新的窗体应用程序
  4. 将用户控件添加到工具箱
  5. 将控件拖放到表单上

我被卡住的地方...

  1. 编辑用户控件控件.IE - 能够使用设计器调整用户控件内的文本框的大小.

我在堆栈上发现了一个从未有人回答过的类似问题.因此,如果我太含糊,您可以点击此链接 https://stackoverflow.com/questions/10359772/example-make-constituent-controls-in-a-usercontrol-editable.

I found a similar question on stack that was never answered. So if I am being too vague you can follow this link https://stackoverflow.com/questions/10359772/example-make-constituent-controls-in-a-usercontrol-editable.

谢谢.

推荐答案

在阅读 Nikita 的评论后,我找到了关于为控件创建自定义设计器的 Microsoft 支持页面.

After reading Nikita's comment I was able to find Microsoft support page on creating a custom designer for controls.

如果您对设计时支持的工作方式感兴趣,请点击此处

Here's a quote if your interested on how the designed-time support works

然而,.NET Framework 中组件的设计时支持并不是由 Microsoft Visual Studio .NET 等设计工具专门定义的.相反,开发环境支持类(例如为组件提供设计时支持的设计器)对设计时行为的扩展和定义.对可扩展和可自定义的设计模式行为的支持是 .NET Framework 的一个集成部分.Visual Studio .NET 等工具还提供了设计人员可以使用的一系列设计时服务.

The design-time support for components in the .NET Framework, however, is not defined exclusively by a design tool such as Microsoft Visual Studio .NET. Rather, the development environment supports the extension and definition of design-time behavior by classes such as designers that provide design-time support for components. Support for extensible and customizable design mode behavior is an integrated part of the .NET Framework. Tools such as Visual Studio .NET also provide a range of design-time services that designers can use.

如果您想继续阅读和查看来自 Microsoft 的示例,请访问此网页

This is the webpage if you like to continue reading and view samples from Microsoft

加强设计时支持

当你刚开始学习时,一切似乎都很复杂,这里有一个 UserControl 的工作代码示例,上面有一个图片框和一个标签.这两个控件都可以在设计时进行编辑,即.调整大小和重新定位,并在您点击它们时显示它们的所有事件和属性.

Everything seems complicated when you just start learning it, heres a working code sample for a UserControl that has a PictureBox and a Label on it. Both controls can be edited during design time, ie. resizing and repositioning, and expose all their events and properties if you click on them.

您需要添加对 System.Design 的引用,只有当您不以.Net 客户端配置文件"为目标时才能引用该引用.您可以在 Proprieties/Application/TargetFramework 中更改您的目标配置文件.

You will need to add a reference to System.Design, which can only be referenced if you are not targeting ".Net Client Profile." You can change you target profile in Proprieties/Application/TargetFramework.

向您的项目添加一个用户控件并添加一个类来处理它的设计器.双击用户控件,然后从工具栏中添加标签和图片框.

Add a usercontrol to your project and add a class to handle it's designer. Double click the usercontrol and then add a label and picture box from the toolbar.

接下来打开你创建的那个类作为它的设计者.添加这个...

Next open that class you create to be it's designer. Add this...

using System.Windows.Forms;
using System.Windows.Forms.Design;

    public override void Initialize(IComponent component)
    {
        base.Initialize(component);

        if (this.Control is MyUserControl)  // replace this with your usercontrol type
        {
            // cast this.Control to you type of usercontrol to get at it's
            // controls easier
            var i = this.Control as MyUserControl; // replace ***

            this.EnableDesignMode(i.label1, "unique_name1");
            this.EnableDesignMode(i.pictureBox1, "unique_name2");
        }
    }

这篇关于C# WindowsForms UserControl 的控件设计器支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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