我遇到三个阻止我的程序运行的错误,我无法弄清楚它们的含义是什么?我一定是傻瓜,因为我甚至把我的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());
}
}
}
答案 0 :(得分:0)
首先,您不需要在类Main
中使用其他Text
方法,因为如果您使用内置模板创建,则已在Program.cs
文件中定义该方法一个WinForms应用程序。
我希望从类Main
中删除冗余的Text
方法可能足以成功运行该应用。
在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
{
// ...
}
}
请覆盖类Dispose()
内的Form1
方法,如下所示:
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}