簡體   English   中英

C#:我無法弄清的三個構建錯誤

[英]C#: Three build errors that I can't figure out

我遇到三個錯誤,這些錯誤使程序無法運行,我無法弄清它們的含義? 我一定很愚蠢,因為我什至還有我的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());
    }
}

}

首先,如果使用內置模板創建WinForms應用程序,則不需要在Text類中使用另一個Main方法,因為該方法已經在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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM