C# Windows 窗体用户控件控件设计器支持

标签 c# winforms user-controls windows-forms-designer editing

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

我能做的...

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

我被困在哪里......

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

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

谢谢。

最佳答案

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

如果您对设计时支持的工作原理感兴趣,请查看这里的报价

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 的示例,请访问此网页

Enhancing Design-Time Support

当您刚开始学习时,一切似乎都很复杂,这是一个 UserControl 的工作代码示例,上面有一个 PictureBox 和一个 Label。这两个控件都可以在设计时进行编辑,即。调整大小和重新定位,并在您单击它们时公开它们的所有事件和属性。

您将需要添加对 System.Design 的引用,只有当您不以“.Net Client Profile”为目标时才能引用它。您可以在 Proprieties/Application/TargetFramework 中更改您的目标配置文件。

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

接下来打开您创建的类作为它的设计器。添加这个...

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# Windows 窗体用户控件控件设计器支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14330625/

相关文章:

c# - List.clear 内存问题

javascript - 如果未填写控件,则自定义验证器不会触发

asp.net - 不明确的引用错误

WPF:文本框文本未更新

c# - HttpClient 在下载时不写入流

c# - 使用 linq 删除代码时出错

winforms - Infragistics 中的复选框列赢得了 ultragrid

winforms - Mono的WinForms如何确定系统字体大小?

wpf - 错误 XLS0502 'WindowsFormsHost' 类型不支持直接内容

c# - 如何修复嵌套的winform控件闪烁问题