【问题标题】:C#: Three build errors that I can't figure outC#:三个我无法弄清楚的构建错误
【发布时间】:2014-04-03 10:37:31
【问题描述】:

我遇到了三个阻止我的程序运行的错误,我无法弄清楚它们的含义?我一定很愚蠢,因为我什至有我的 C# 书在我旁边,但它仍然没有意义。任何帮助都会很棒。错误是:

  1. Projects\WindowsFormsApplication1\obj\x86\Release\WindowsFormsApplication1.exe' 定义了多个入口点:'WindowsFormsApplication1.Program.Main()'。使用 /main 编译以指定包含入口点的类型。

  2. 'WindowsFormsApplication1.Form1.Dispose(bool)':找不到合适的方法来覆盖

  3. Projects\WindowsFormsApplication1\obj\x86\Release\WindowsFormsApplication1.exe' 定义了多个入口点:'Text.Main()'。使用 /main 编译以指定包含入口点的类型。

代码:

 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Windows.Forms;

 public class Text : Form
 {

private Font arialBold24 = new Font("Arial", 24, FontStyle.Bold);

private Font timesItalic14 = new Font("Times New Roman", 14, FontStyle.Italic);

private Font courierPlain18 = new Font("Courier New", 18, FontStyle.Strikeout);

private Font genericSerifBI20 = new Font(FontFamily.GenericSerif, 20, FontStyle.Bold | FontStyle.Italic);

private Font verdanaPlain18 = new Font("Verdana", 18, FontStyle.Regular | FontStyle.Underline);


public Text()
{

    Size = new Size(400, 200);

    Text = "Text";

    BackColor = Color.White;

}



protected override void OnPaint(PaintEventArgs e)
{

    Graphics g = e.Graphics;

    int w = (int)g.MeasureString(arialBold24.Name, arialBold24).Width;

    int arialStart = (Width - w) / 2;

    int otherStart = Width / 4;

    int h = DisplayRectangle.Height;

    g.DrawString(arialBold24.Name, arialBold24, Brushes.Blue, arialStart, 0);

    g.DrawString(timesItalic14.Name, timesItalic14, Brushes.Blue, otherStart, h / 5);

    g.DrawString(courierPlain18.Name, courierPlain18, Brushes.Blue, otherStart, 2 * h / 5);

    g.DrawString(genericSerifBI20.Name, genericSerifBI20, Brushes.Blue, otherStart, 3 * h / 5);

    g.DrawString(verdanaPlain18.Name, verdanaPlain18, Brushes.Blue, otherStart, 4 * h / 5);

    base.OnPaint(e);

}

public static void Main()
{

    Application.Run(new Text());

}

}

program.cs 代码为:

 namespace WindowsFormsApplication1
 {
 static class Program
 {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
 }
 }

Form1的更新代码:

 namespace WindowsFormsApplication1
 {
partial class Form1
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.SuspendLayout();
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(284, 262);
        this.Name = "Form1";
        this.Text = "Form1";
        this.Load += new System.EventHandler(this.Form1_Load);
        this.ResumeLayout(false);

    }

    #endregion
}

}

Program.CS 代码:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Windows.Forms;

 namespace WindowsFormsApplication1
 {
static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Text());
    }
}

}

【问题讨论】:

  • 根据错误,您的项目中似乎还有一些其他代码 - Form1.cs 和 Program.cs。你也可以发这些吗?您可能可以删除它们或将它们从您的项目中排除以解决错误。
  • 查看你的项目,你可能有一个Program.cs文件,它已经定义了main方法(你放在上面代码末尾的那个)。您可以删除表单类中的 main 方法。

标签: c#


【解决方案1】:

首先,您不需要在类Text 中使用另一个Main 方法,因为如果您使用内置模板创建WinForms 应用程序,它已经在Program.cs 文件中定义。

我希望从 Text 类中删除多余的 Main 方法可能足以成功运行应用程序。


编辑

Program.cs 文件中,实例化Text 类而不是Form1,如下所示:

namespace WindowsFormsApplication1
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Text());
        }
    }
}

您还需要将Text 类包含在以下命名空间中:

namespace WindowsFormsApplication1
{
    public class Text : Form
    {
        // ...
    }
}

编辑 2:

请覆盖Form1 类中的Dispose() 方法,如下所示:

protected override void Dispose(bool disposing)
{
    if (disposing && (components != null))
    {
        components.Dispose();
    }
    base.Dispose(disposing);
}

【讨论】:

  • 现在我收到错误:'WindowsFormsApplication1.Form1.Dispose(bool)':找不到合适的方法来覆盖
  • 不,你又把Text类的代码贴出来了。请发帖Form1.
  • 非常感谢。你是救生员。
  • 没关系。我不配。我们全能的造物主是唯一的救星。顺便说一句,祝你好运!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多