Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 方法创建和更改标签_C# - Fatal编程技术网

C# 方法创建和更改标签

C# 方法创建和更改标签,c#,C#,我想创建标签并使用方法设置文本,但它不起作用,以下是我的代码: public partial class Form1 : Form { public Form1() { InitializeComponent(); intro(); } private void fullScreen() { this.FormBorderStyle = System.Windows.F

我想创建标签并使用方法设置文本,但它不起作用,以下是我的代码:

public partial class Form1 : Form
{        
    public Form1()
    {            
        InitializeComponent();
        intro();
    }
    private void fullScreen()
    {
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        this.Bounds = Screen.PrimaryScreen.Bounds;
    }
    private void intro()
    {
        pictureBox1.BackColor = Color.White;
        pictureBox1.SendToBack();
        Label introInfo = new Label();
        introInfo.Font = new Font("century gothic", 24, FontStyle.Bold);
        introInfo.ForeColor = Color.Cyan;
        introInfo.Text = "succes bro!";
        introInfo.Visible = true;
        introInfo.Location = new Point(100, 100);            
    }        
}

我应该怎么做才能让它工作?

您需要将
标签添加到
表单中

this.Controls.Add(label);
看看


您需要将新创建的标签添加到控件集合

public partial class Form1 : Form
{        
    public Form1()
    {            
        InitializeComponent();
        intro();
    }
    private void fullScreen()
    {
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        this.Bounds = Screen.PrimaryScreen.Bounds;
    }


    private void intro()
    {
        pictureBox1.BackColor = Color.White;
        pictureBox1.SendToBack();
        Label introInfo = new Label();
        introInfo.Font = new Font("century gothic", 12, FontStyle.Bold);
        introInfo.ForeColor = Color.Cyan;
        introInfo.Text = "succes bro!";
        introInfo.Visible = true;
        introInfo.Location = new Point(75, 23);
        introInfo.Size= new Size(100,100);
        this.Controls.Add(introInfo);
    }

我应该在哪里添加此.Controls.add(label)?像这样?”私有void intro(){pictureBox1.BackColor=Color.White;pictureBox1.SendToBack();Label introInfo=new Label();introInfo.Font=new Font(“世纪哥特式”,24,FontStyle.Bold);introInfo.ForeColor=Color.Cyan;introInfo.Text=“成功兄弟!”;introInfo.Visible=true;introInfo.Location=new Point(100100);this.Controls.Add(introInfo);}'但是为什么在我的表单中,我看不到标签?您还必须指定标签尺寸,复制上面的代码,它就会工作。以及如何使用方法intro();当窗体加载时?当我使用按钮单击事件时,它正在工作。但如何在加载时在表单上实现这一点?@notAdmin:我尝试过这个,它对我有效。你能试着改变一下标签的位置吗?没问题,现在我想用方法intro();当form1启动时。我可以这样做吗?@notAdmin:是的,你可以很好地使用表单加载事件来实现这个目的。
public partial class Form1 : Form
{        
    public Form1()
    {            
        InitializeComponent();
        intro();
    }
    private void fullScreen()
    {
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        this.Bounds = Screen.PrimaryScreen.Bounds;
    }


    private void intro()
    {
        pictureBox1.BackColor = Color.White;
        pictureBox1.SendToBack();
        Label introInfo = new Label();
        introInfo.Font = new Font("century gothic", 12, FontStyle.Bold);
        introInfo.ForeColor = Color.Cyan;
        introInfo.Text = "succes bro!";
        introInfo.Visible = true;
        introInfo.Location = new Point(75, 23);
        introInfo.Size= new Size(100,100);
        this.Controls.Add(introInfo);
    }