2012-08-26 66 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和标签,因此我需要知道它们是如何一起工作的。感谢您的回答,非常有帮助。 –