2012-08-26 73 views
0

我在添加Label控件到RichTextBox時遇到問題。很明顯,我必須在代碼中缺少某些東西。如果有人能指出我的疏忽,我將不勝感激。我知道這兩個控件都已創建,但Label未顯示在RichTexBox的頂部......相反,它在之後創建了將「標籤」控件添加到「RichTextBox」

RichTextBox richBox8; 
Label label8; 

private void create() 
{ 
    richBox8 = new RichTextBox(); 
    richBox8.Location = new System.Drawing.Point(957, 95); 
    richBox8.Size = new System.Drawing.Size(159, 50); 
    richBox8.Name = "richTextBox8"; 
    Controls.Add(richBox8); 

    label8 = new Label(); 
    label8.Location = new System.Drawing.Point(984, 106); 
    label8.Name = "label8"; 
    label8.Size = new System.Drawing.Size(110, 25); 
    label8.Text = "" 
    Controls.Add(label8); 
    richBox8.Controls.Add(label8); 
} 

回答

0

一般而言,所有你需要做的就是文本值添加到您的標籤!並且您應該注意每個控件的x-y座標。

RichTextBox richBox8 = null; 
Label label8 = null; 
private void button1_Click(object sender, EventArgs e) 
{ 
    richBox8 = new RichTextBox(); 
    richBox8.Location = new System.Drawing.Point(1, 1); 
    richBox8.Size = new System.Drawing.Size(300, 200); 
    richBox8.Name = "richTextBox8"; 
    Controls.Add(richBox8); 

    label8 = new Label(); 
    label8.Location = new System.Drawing.Point(5, 5); 
    label8.Name = "label8"; 
    label8.Size = new System.Drawing.Size(110, 25); 
    label8.Text = "hello world"; // crucial, if there is no text, you won't see any label! 
    richBox8.Controls.Add(label8); 
    // adding the label once again to the form.Controls collection is unnecessary 
} 

雖然它是可能的標籤添加到RichTextBox控制,我不認爲它是非常有用的!一個RichTextBox可以用來顯示格式文本

public Form1() 
{ 
    InitializeComponent(); 
    richTextBox1.Text = "demo text demo text demo text demo text demo text demo text demo text demo text demo text demo text demo text demo text "; 
} 

private void button1_Click(object sender, EventArgs e) 
{ 
    richTextBox1.Select(10, 20); 
    richTextBox1.SelectionColor = Color.Blue; 

    richTextBox1.Select(25, 30); 
    richTextBox1.SelectionFont = new Font("Verdana", 12); 
} 
+0

各種項目要求的各項任務。我正在嘗試使用richTextBoxes和標籤,因此我需要知道它們是如何一起工作的。感謝您的回答,非常有幫助。 –