类,结构或接口必须具有返回类型-C#

时间:2019-07-18 14:49:57

标签: c#

我正在用C#开发一个简单的时钟应用程序。编译代码时,出现以下错误:EasyClock.cs(14,16):错误CS1520:类,结构或接口方法必须具有返回类型。这是我的代码:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace EasyClock
{
    class Clock : System.Windows.Forms.Form
    {
        Label timeLbl;
        Label dateLabel;
        Button btnClose;
        Timer timer1;

        public EasyClock()
        {
            this.components = new System.ComponentModel.Container();
            this.timeLbl = new System.Windows.Forms.Label();
            this.dateLbl = new System.Windows.Forms.Label();
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            timer1.Tick += new EventHandler(timer1_tick);
            this.closeBtn = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // timeLbl
            // 
            this.timeLbl.AutoSize = true;
            this.timeLbl.Font = new System.Drawing.Font("Microsoft Sans Serif", 24F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.timeLbl.Location = new System.Drawing.Point(12, 9);
            this.timeLbl.Name = "timeLbl";
            this.timeLbl.Size = new System.Drawing.Size(101, 46);
            this.timeLbl.TabIndex = 0;
            this.timeLbl.Text = "0:00";
            // 
            // dateLbl
            // 
            this.dateLbl.AutoSize = true;
            this.dateLbl.Location = new System.Drawing.Point(17, 55);
            this.dateLbl.Name = "dateLbl";
            this.dateLbl.Size = new System.Drawing.Size(85, 17);
            this.dateLbl.TabIndex = 1;
            this.dateLbl.Text = "current date";
            // 
            // closeBtn
            // 
            this.closeBtn.Location = new System.Drawing.Point(12, 99);
            this.closeBtn.Name = "closeBtn";
            this.closeBtn.Size = new System.Drawing.Size(158, 42);
            this.closeBtn.TabIndex = 2;
            this.closeBtn.Text = "Close";
            this.closeBtn.UseVisualStyleBackColor = true;
            closeBtn.Click += new EventHandler(btnClose_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(182, 153);
            this.Controls.Add(this.closeBtn);
            this.Controls.Add(this.dateLbl);
            this.Controls.Add(this.timeLbl);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        void timer1_tick(object sender, EventArgs args)
        {
            timeLbl.Text = DateTime.Now.ToString("h:mm:ss tt");
            dateLbl.Text = DateTime.Now.ToString("dd/MM/yyyy");
        }

        void btnClose_Click(object sender, EventArgs args)
        {
            this.Close();
        }


        static void Main()
        {
            timer1.Start();
            Clock clock = new Clock();
            Application.Run( clock );
        }
    }
}

我知道之前曾有人问过这个问题。但是,没有任何解决方案对我有帮助。感谢所有帮助。

1 个答案:

答案 0 :(得分:1)

public EasyClock()
        {

public Clock()
        {

您的名称(构造方法)错误。 您的类名称为 Clock ,但您的名称空间 EasyClock 构造方法必须与该类名称相同

static void Main 方法常用的控制台应用程序和program.cs文件。它在做什么。通常不使用此类

请删除此文件并移至program.cs文件

static void Main()
    {
        timer1.Start();
        Clock clock = new Clock();
        Application.Run( clock );
    }

编辑:如果仅在其中包含代码,则擦除(program.cs)