开发者

Displaying the form in center in c#

开发者 https://www.devze.com 2023-03-06 11:59 出处:网络
i am new here, actually i am working on project which is in c#, and in that project i want to display my form in th开发者_开发技巧e center of the screen... So for that i have written the following cod

i am new here, actually i am working on project which is in c#, and in that project i want to display my form in th开发者_开发技巧e center of the screen... So for that i have written the following code..

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;
}  

but it is giving some errors,that are 1)Error 1 'center.Form1.Dispose(bool)': no suitable method found to override C:\Users\logicwaves\Documents\Visual Studio 2005\Projects\center\center\Form1.Designer.cs 14 33 center

2)Error 2 Program 'C:\Users\logicwaves\Documents\Visual Studio 2005\Projects\center\center\obj\Debug\center.exe' has more than one entry point defined: 'center.Program.Main()' C:\Users\logicwaves\Documents\Visual Studio 2005\Projects\center\center\Program.cs 13 21 center

3)Error 3 Program 'C:\Users\logicwaves\Documents\Visual Studio 2005\Projects\center\center\obj\Debug\center.exe' has more than one entry point defined: 'CenterForm.Main()' C:\Users\logicwaves\Documents\Visual Studio 2005\Projects\center\center\Form1.cs 58 17 center

what should i do, to overcome from these errors?


no need for that CenterForm.StartPosition = FormStartPosition.CenterScreen;


The errors are because you defined Main() method in the Form.cs class. Normally it will be in the Program.cs class, if you are using Visual Studio. Dispose() method may available in the Form.Designer.cs too. You can make the form center of the screen by setting the StartPosition property to FormStartPosition.CenterScreen;


Try this :

this meant by current Form

this.CenterToScreen();


1.Open .cs file in Notepad

2.Now (ctr+A) select all and delete(you can't see any text)

3.type the following 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;

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

Its realy working ...

0

精彩评论

暂无评论...
验证码 换一张
取 消