2014-01-07 56 views
0

我想创建标签和使用方法设置文本,但它不会工作,这是我的代码:方法来创建和更改标签

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

我应该怎么做,使其工作?

回答

1

您需要将label添加到form

this.Controls.Add(label); 

看看this example

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
     this.Load += Form1_Load; 
    } 

    void Form1_Load(object sender, EventArgs e) 
    { 
     intro(); 
    } 

    private void fullScreen() 
    { 
     this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; 
     this.Bounds = Screen.PrimaryScreen.Bounds; 
    } 

    private void intro() 
    { 
     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); 
     introInfo.Height = 35; 
     introInfo.Width = 250; 

     this.Controls.Add(introInfo); 
    } 
} 
+0

哪里我应该添加this.Controls.Add(label); ? – notAdmin

+0

用代码示例更新了答案。 – Marko

+0

是这样的? 'private 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 =“succes bro!”; introInfo.Visible = true; introInfo.Location = new Point(100,100); this.Controls.Add(introInfo); }' – notAdmin

0

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

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); 
    } 
+0

标签不会显示 – notAdmin

+0

当我使用按钮单击事件时它正在工作。但如何在载入时在窗体上执行该操作? – notAdmin

+0

@notAdmin:我试过这个,它为我工作。你可以尝试改变标签的位置吗? – Ramashankar