在设计视图的 WinForms 应用程序中显示以编程方式添加的控件? [英] Show controls added programmatically in WinForms app in Design view?

查看:29
本文介绍了在设计视图的 WinForms 应用程序中显示以编程方式添加的控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Visual Studio(任何版本都可以),是否可以在切换回设计视图时显示以编程方式(而不是通过设计视图)添加的控件?

Using Visual Studio (any version will do), is it possible for controls added programmatically (rather than via the Design view) to then be shown when switching back to the Design view?

我尝试了一些非常简单的方法,例如:

I've tried something ridiculously simple like:

public Form1()
{
    AddCtrl();
    InitializeComponent();
    AddCtrl();
}

private void AddCtrl()
{
    this.SuspendLayout();
    this.Controls.Add(new TextBox());
    this.ResumeLayout(false);
    this.PerformLayout();
}

...但无济于事.

推荐答案

设计者是否运行我的代码?

当表单在设计器中显示时,设计器反序列化您的表单代码(Form1.Designer.cs 或 Form1.cs 中的第一类)并创建表单基类的实例并反序列化 InitializeComponent 并创建您在类中声明的控件并设置它们的属性.

Does the designer run my code?

When a form shows in designer, the designer deserialize the code of your form (Form1.Designer.cs or first class in Form1.cs) and creates an instance of the base class of your form and deserialize InitializeComponent and creates controls that you declared in your class and set their properties.

所以 Constructor 中的代码不会运行.设计器只创建表单基类的实例,不会查看表单的构造函数.

So the codes in Constructor won't run. The designer only creates an instance of the base class of your form and don't look in constructor of your form.

看下面的代码,注意这个问题:

Look at below code and pay attention to this problems:

  • ; 在哪里?
  • Form1 的构造函数 Form111111?
  • 什么是NotDefinedFunction()?
  • 如何int i = "xxxxxxxxxx"?
  • Where are ;s?
  • Constructor Form111111 for Form1?
  • What is NotDefinedFunction()?
  • How can int i = "xxxxxxxxxx"?

即使你创建了这样的文件,设计器也会正确显示.

Even if you create such file, the designer will show correctly.

using System
using System.Collections.Generic
using System.Drawing
using System.Windows.Forms
namespace Sample
{
    public class Form1:Form
    {
        public Form111111()
        {
            NotDefinedFunction()
            InitializeComponent()
        }
        public void InitializeComponent()
        {
            int i = "xxxxxxxxxx"
            this.textBox1 = new System.Windows.Forms.TextBox()
            this.SuspendLayout()
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(0, 0)
            this.textBox1.Name = "textBox1"
            this.textBox1.Text = "text of text box 1";
            // 
            // Form1
            // 
            this.Controls.Add(this.textBox1)
            this.Name = "Form1"
            this.Text = "Form1"
            this.Size= new Size(250,100)
            this.ResumeLayout(false)
            this.PerformLayout()
        }
        private TextBox textBox1
    }
}

您将在设计器中看到表单:

And you will see the form in designer:

如果您需要这样的功能,您可以在表单的基类构造函数中创建动态控件,因为基类的构造函数将在您在设计器中打开子表单时运行,然后它会在设计时运行.

If you need such functionality, you can create your dynamic controls in constructor of a base class for your form, since constructor of base class will run when you open a child form in designer, then it will run at design time.

但是您应该知道这些控件是继承的,不能使用子窗体的设计器进行更改.

But you should know these controls are inherited and can't be changed using designer of child form.

所以只需创建一个 Form2:

So simply create a Form2:

public Form2()
{
    InitializeComponent();
    AddDynamicControls();
}

private void AddDynamicControls()
{
    this.Controls.Add(
       new TextBox() { 
          Name = "TextBox1", Text = "Dynamic", Location = new Point(100, 0) });
}

构建项目,然后将Form1的基类更改为继承自Form2:

Build the project and then change base class of Form1 to inherit from Form2:

public class Form1:Form2

结果将是:

如果你真的想在设计时生成一些控件,我认为你应该看看T4 文本模板.

If you want to generate some controls really at design time, I think you should take a look at T4 Text Templates.

您可以使用 t4 模板在设计时生成代码.您可能已经看过实体框架 .tt 模板文件.您可以将新的 Text Template 项目添加到您的项目中,并将在设计时生成项目的逻辑放在您的 t4 模板中.

You can use t4 templates to generate code at design-time. Probably you have seen Entity Framework .tt template files. You can add new Text Template item to your project and put the logic for generating items at design-time in your t4 template.

这篇关于在设计视图的 WinForms 应用程序中显示以编程方式添加的控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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