在声明中缺少部分修饰符..这种类型的另一部分声明存在".我是初学者,只是跟着这本书

use*_*237 14 c# declaration partial winforms form-control

我是初学者,我正在按照一本书做练习.下面是我的代码,我得到了这个

"Missing partial modifier on declaration of type 'Windowsform.Form1'; another partial declaration of this type exists".

我该怎么办?我的代码如下:

using System;
using System.Windows.Forms;

namespace Windowsform
{
    public class Form1 : Form
    {
       private TextBox txtEnter;
       private Label lblDisplay;
       private Button btnOk;

       public Form1()

       {
           this.txtEnter = new TextBox();
           this.lblDisplay = new Label();
           this.btnOk = new Button();
           this.Text = "My Hellowin app!";


           //txtEnter
           this.txtEnter.Location = new System.Drawing.Point(16, 32);
           this.txtEnter.Size = new System.Drawing.Size(264, 20);

           //lblDisplay
           this.lblDisplay.Location = new System.Drawing.Point(16, 72);
           this.lblDisplay.Size = new System.Drawing.Size(264, 128);

           //btnOK
           this.btnOk.Location = new System.Drawing.Point(88, 224);
           this.btnOk.Text = "OK";
           this.btnOk.Click +=
               new System.EventHandler(this.btnOK_Click); 

           //MyForm
           this.Controls.AddRange(new Control[] {
                            this.txtEnter, this.lblDisplay, this.btnOk});

       }

       static void Main()
       {
           Application.Run(new Form1());
       }

       private void btnOK_Click(object sender, System.EventArgs e)
       {
           lblDisplay.Text = txtEnter.Text + "\n" + lblDisplay.Text;
       }
    }
}
Run Code Online (Sandbox Code Playgroud)

Adr*_*tti 30

在另一个地方,你声明了一个具有相同名称(Form1)的类,并使用partial修饰符声明它.

如果您将类拆分为两个不同的文件(例如,此文件用于UI布局,另一个文件用于逻辑),则只需将partial修饰符添加到声明中:

public partial class Form1
{
}
Run Code Online (Sandbox Code Playgroud)


Iga*_*hka 5

创建新表单时,实际上是在两个不同的位置创建的.声明类的一个地方是实现所有管道的地方,以及您在此处粘贴的文件,以编写所有功能.为了使这成为可能,c#具有部分类特征,如果在程序集的某个位置使用partial修饰符定义类,则可以定义该类的多个声明,只要它具有该修饰符即可.

更多关于msdn的内容