gpt4 book ai didi

c# - 在 C# 中居中显示窗体

转载 作者:太空宇宙 更新时间:2023-11-03 19:29:25 27 4
gpt4 key购买 nike

我是新来的,实际上我正在做一个用 C# 编写的项目,在那个项目中我想在​​屏幕中央显示我的表单...所以为此我编写了以下代码..

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 中心

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

最佳答案

不需要 CenterForm.StartPosition = FormStartPosition.CenterScreen;

关于c# - 在 C# 中居中显示窗体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6039960/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com