2014-01-07 54 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