无法查看编码在C#中的表单设计时 [英] Can't view designer when coding a form in C#

查看:168
本文介绍了无法查看编码在C#中的表单设计时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面的上的WinForms本教程,到目前为止,本教程的编码形式不使用工具箱。我相信它会在更深入介绍工具箱不久。

I'm following this tutorial on winforms, and so far the tutorial is coding the form without using the toolbox. I believe it'll introduce the toolbox in more depth shortly.

继本教程中,我在code以下两件了部分类:

Following the tutorial, I've made a partial class in the following two pieces of code:

第一个文件的:

using System;
using System.Windows.Forms;

public class Numeric : System.Windows.Forms.TextBox
{
    public Numeric()
    {
    }
}

public partial class Exercise
{
    private Numeric txtbox;
    System.ComponentModel.Container components;
}

第二个文件的:

using System;
using System.Windows.Forms;

public partial class Exercise : Form
{
    private void InitializeComponent()
    {
        txtbox = new Numeric();
        Controls.Add(txtbox);
    }

    public Exercise()
    {
        InitializeComponent();
    }
}

public class program
{
    public static int Main()
    {
        Application.Run(new Exercise());
        return 0;
    }
}

当我运行code。与F5,一切都看起来不错:形式与文本框弹出

When I run the code with F5, everything looks fine: The form pops up with the textbox.

但由于某些原因,在第二个文件,当我点击右键并选择视图设计器,我得到它说变量txtbox'要么是未声明或从未分配的错误。我可以选择忽略并继续,这使我一个表格上没有文本框。

But for some reason, when I right-click on the second file and choose "View Designer", I get an error which says "The variable 'txtbox' is either undeclared or was never assigned". I can choose to "Ignore and Continue", which leads me to a form with no textbox on it.

为什么会发生这种情况?我知道有些人认为我应该只使用工具箱,它可能做的最明智的事情,但我还是想明白为什么会这样。

Why does this occur? I know some of you think I should just use the toolbox, and it's probably the most sensible thing to do, but I would still like to understand why this happens.

推荐答案

当你打开一个表格在Windows窗体设计,设计师眺望文件中的第一类。如果文件中有一个 Designer.cs 包含类的其他部分的一部分,还包括它并试图为反序列化这些文件的内容。在反序列化和加载表单的设计时间的过程中,它会创建表单的基类的一个实例,并期待这些文件中的组件声明和 InitializeComponents 方法。如果发现他们创建的组件和使用反序列化codeS和组件添加到其创建的基类的实例设置它们的属性。

When you open a Form in windows forms designer, the designer looks into the first class in the file. If the file has a Designer.cs containing the other partial part of the class, also includes it and tries to deserialize those file contents. In the process of deserialization and loading the design time of your form, it creates an instance of the base class of your form and looks in those files for component declarations and InitializeComponents method. If find them creates components and sets properties of them using deserialized codes and add components to the instance of base class which created.

一些有用的事实:


  • codeS在表格在设计时将不会执行,但基类的形式的构造函数会在设计时执行的构造函数。

  • codeS在的InitializeComponent 在设计时将不会执行,但这些codeS将被反序列化,将用于创建表单的设计。

  • 设计者不能显示它有一个抽象基类的形式。 (<一href=\"http://stackoverflow.com/questions/6817107/abstract-usercontrol-inheritance-in-visual-studio-designer\">solution)

  • 设计者不能显示它有泛型类的形式。例如,它不能显示 MyForm的:SomeForm&LT; SomeClass的&GT; ,但它可以显示 SomeForm&LT; T&GT;:表单。 (<一href=\"http://stackoverflow.com/questions/33112724/windows-forms-generic-inheritance\">solution)

  • 如果你定义一个新的属性为窗体,属性不会在属性窗口中显示。属性窗口,显示基类,但与表单值的属性。

  • 当一个文件包含2类,如果表单不是一流的设计师无法加载,并且您收到,说的形式应该是设计师展示一流的警告。

  • 以上规则也适用于用户控件。

  • Codes in constructor of your Form will not execute at design-time, but the constructor of base class of your form will execute in design-time.
  • Codes in InitializeComponent will not execute at design-time, but those codes will be deserialized and will be used to create designer of the form.
  • The designer can not show a form which has an abstract base class. (solution)
  • The designer can not show a form which has generic class. For example it can not show MyForm:SomeForm<SomeClass>, but it can show SomeForm<T>:Form. (solution)
  • If you define a new property for your form, the properties will not show in properties window. The properties window, shows the properties of base class but with values of your form.
  • When a file contains 2 class, if the form was not the first class the designer can not load and you receive a warning that says the form should be first class to show in designer.
  • Above rules will apply also to UserControls.

示例

在以低于code一看,里面有一些严重的问题:

Take a look at below code, which has some serious problems:


  • 类有不同的构造比类的名称

  • 语句 INT I =X;

  • 有没有分号,而这是一个C#类

  • 的InitializeComponent 方法构造不叫

  • The class has different constructor than class name
  • The statement int i="x";
  • There is no semicolons while this is a C# class
  • The InitializeComponent method didn't call in constructor

但有趣的消息是,你可以看到设计师的形式,即使有这些错误!

But the interesting news is you can see the form in designer, even with those errors!

只需在项目中创建一个文件,并在文件中提出以下codeS和保存文件并关闭它。然后不尝试构建解决方案,在设计器中打开窗体。这里是code:

Just create a file in your project and put below codes in the file and save the file and close it. Then without trying to build the solution, open the form in designer. Here is code:

using System
using System.Windows.Forms
namespace SampleApplication
{
    public class MyForm:Form
    {
        public NotMyForm()
        {
        }
        public void InitializeComponent()
        {
            int i="x";
            textBox1 = new TextBox()
            textBox1.Text = "Hi"
            this.Controls.Add(textBox1)
        }
        private TextBox textBox1
    }
}

这是设计师的截图:

在这里输入的形象描述

更多信息

要找到更多的信息,看看此链接:

To find more information, take a look at this link:

  • How does the Windows Forms designer in Visual Studio load a Form?

解决您的问题

作为一个解决方案,这是足以让你移动私人数字txtbox; 并把它放在你的seccond文件中的运动类。

As a solution, it is enough for you to move private Numeric txtbox; and put it your seccond file in Exercise class.

这篇关于无法查看编码在C#中的表单设计时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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