简体   繁体   English

C#Windows窗体类型转换错误

[英]C# Windows Forms Type Conversion Error

Right now, I am learning C# from a textbook. 现在,我正在从一本教科书中学习C#。 In one of the examples in the chapter I'm currently stuck on, Windows Forms, I am instructed to enter the following code to create a simple window: 在我当前停留的本章“ Windows窗体”中的一个示例中,指示我输入以下代码来创建一个简单的窗口:

namespace WinForms {

using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;

public class HelloWindowsForms : System.Windows.Forms.Form
{
    private System.Windows.Forms.Label label1;

    public HelloWindowsForms()
    {
        this.label1 = new System.Windows.Forms.Label();

        label1.Location = new System.Drawing.Point(8,8);
        label1.Text = "Hello Windows Forms!";
        label1.Size = new System.Drawing.Size(408,48);
        label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 24f);
        label1.TabIndex = 0;
        label1.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; //this is the problem

        this.Text = "Hello World";
        this.MaximizeBox = false;
        this.AutoScaleBaseSize = new System.Drawing.Size(5,13);
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
        this.MinimizeBox = false;
        this.ClientSize = new System.Drawing.Size(426,55);
        this.Controls.Add(label1);
    }

    public static void Main(string[] args)
    {
        Application.Run(new HelloWindowsForms());
    }
    }
}

On the problem line(specified in single line comment) I get this error when running this program: 在问题行(在单行注释中指定)上,运行此程序时出现此错误:

Cannot implicitly convert type 'System.Windows.Forms.HorizontalAlignment' to 'System.Drawing.ContentAlignment'. An explicit conversion exists (are you missing a cast?) (CS0266)

Other sources online say I need to cast, but I can't figure out what to cast or what to cast it as. 在线上的其他消息来源说,我需要投射,但是我无法弄清楚该投射或将其投射为什么。 I tried skipping ahead and completing the chapter, but I get similar Cannot implicitly convert type errors when trying to do all of the examples. 我尝试跳过并完成本章,但是在尝试执行所有示例时,我得到类似的Cannot implicitly convert type错误的信息。 Can someone help me figure out how to fix this? 有人可以帮我找出解决方法吗?

You are using the wrong enum. 您使用了错误的枚举。 You are using a System.Windows.Forms.HorizontalAlignment enum when the TextAlign property is calling for a System.Drawing.ContentAlignment enum. TextAlign属性调用System.Drawing.ContentAlignment枚举时,您正在使用System.Windows.Forms.HorizontalAlignment枚举。 Try this: 尝试这个:

label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;

I bumped in the same problem and figured this out, 我碰到了同样的问题,并弄清楚了,

If you are using 如果您正在使用

Label 标签

then use this: 然后使用这个:

this.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;

I think you are using 我想你在用

TextBox 文本框

Therefore, use: 因此,请使用:

this.textbox1.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM