在c#中显示中心的表单

时间:2011-05-18 04:43:15

标签: c#

我是新来的,实际上我正在研究c#中的项目,在那个项目中我想在​​屏幕中央显示我的表单...所以为此我写了以下代码.. < / p>

public class CenterForm : System.Windows.Forms.Form
{
private System.ComponentModel.Container components;

public CenterForm()
{
    InitializeComponent();
    CenterToScreen();
}

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

private void InitializeComponent()
{
    this.button1 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(107, 177);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(75, 23);
    this.button1.TabIndex = 0;
    this.button1.Text = "Ok";
    this.button1.UseVisualStyleBackColor = true;
    // 
    // CenterForm
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Controls.Add(this.button1);
    this.Name = "CenterForm";
    this.Text = "DataGridExample";
    this.Paint += new System.Windows.Forms.PaintEventHandler(this.CenterForm_Paint);
    this.Resize += new System.EventHandler(this.CenterForm_Resize);
    this.ResumeLayout(false);

}
[STAThread]
static void Main()
{
    Application.Run(new CenterForm());
}
private void CenterForm_Resize(object sender, System.EventArgs e)
{
    Invalidate();
    Console.WriteLine("Resize");
}
private void CenterForm_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
    Graphics g = e.Graphics;
    g.DrawString("Text",
      new Font("Times New Roman", 20),
      new SolidBrush(Color.Black),
      this.DisplayRectangle);
}

private Button button1;
}  

但是它会给出一些错误 1)错误1'center.Form1.Dispose(bool)':找不到合适的方法来覆盖C:\ Users \ logicwaves \ Documents \ Visual Studio 2005 \ Projects \ center \ center \ Form1.Designer.cs 14 33 center

2)错误2程序'C:\ Users \ logicwaves \ Documents \ Visual Studio 2005 \ Projects \ center \ center \ obj \ Debug \ center.exe'定义了多个入口点:'center.Program.Main ()'C:\ Users \ logicwaves \ Documents \ Visual Studio 2005 \ Projects \ center \ center \ Program.cs 13 21 center

3)错误3程序'C:\ Users \ logicwaves \ Documents \ Visual Studio 2005 \ Projects \ center \ center \ obj \ Debug \ center.exe'定义了多个入口点:'CenterForm.Main() 'C:\ Users \ logicwaves \ Documents \ Visual Studio 2005 \ Projects \ center \ center \ Form1.cs 58 17 center

我该怎么做才能克服这些错误?

4 个答案:

答案 0 :(得分:7)

不需要CenterForm.StartPosition = FormStartPosition.CenterScreen;

答案 1 :(得分:2)

错误是因为您在Form.cs类中定义了Main()方法。通常,如果您使用的是Visual Studio,它将位于Program.cs类中。 Form.Designer.cs中也可以使用Dispose()方法。您可以通过将StartPosition属性设置为FormStartPosition.CenterScreen来制作屏幕的表单中心;

答案 2 :(得分:0)

试试这个:

当前表单

this

this.CenterToScreen();

答案 3 :(得分:-3)

1.在记事本中打开.cs文件

2.Now(ctr + A)选择全部并删除(你看不到任何文字)

3.键入以下代码

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;

namespace **[Namespace of your form]**
{
    public partial class [class name of your form]: Form
    {
        public [class name of your form]()
        {
            InitializeComponent();
        }       
    }
}

它的真正工作......