我收到三个错误,阻止我的程序运行,我无法弄清楚它们的意思? 我一定是傻瓜,因为我甚至在我旁边都有我的C#书,但它仍然没有意义。 任何帮助都会很棒。 错误是:
Projects \ WindowsFormsApplication1 \ obj \ x86 \ Release \ WindowsFormsApplication1.exe'定义了多个入口点:'WindowsFormsApplication1.Program.Main()'。 使用/ main编译以指定包含入口点的类型。
'WindowsFormsApplication1.Form1.Dispose(bool)':找不到合适的方法来覆盖
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()); } }}
I am getting three errors that are preventing my program from running and I cant figure out what they mean? I must be stupid because I even have my C# book next to me and it still doesn't make sense. Any help would be great. The errors are:
Projects\WindowsFormsApplication1\obj\x86\Release\WindowsFormsApplication1.exe' has more than one entry point defined: 'WindowsFormsApplication1.Program.Main()'. Compile with /main to specify the type that contains the entry point.
'WindowsFormsApplication1.Form1.Dispose(bool)': no suitable method found to override
Projects\WindowsFormsApplication1\obj\x86\Release\WindowsFormsApplication1.exe' has more than one entry point defined: 'Text.Main()'. Compile with /main to specify the type that contains the entry point.
Code:
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()); } }The program.cs code is:
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()); } } }Updated Code of 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 code:
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()); } }}
最满意答案
首先,您不需要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); }First of all, you don't need another Main method in class Text, as it is already defined in the Program.cs file if you use the built-in template to create a WinForms app.
I hope removing the redundant Main method from class Text may be enough to run the app successfully.
EDIT
In the Program.cs file, instantiate the Text class instead of Form1 like below:
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()); } } }Also you need to enclose the class Text with the following namespace:
namespace WindowsFormsApplication1 { public class Text : Form { // ... } }EDIT 2:
Please override the Dispose() method inside class Form1 like below:
protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); }更多推荐
发布评论