将“Label”控件添加到“RichTextBox”

时间:2012-08-26 18:16:55

标签: c# label richtextbox

我在向Label添加RichTextBox控件时遇到问题。显然,我必须在代码中遗漏一些东西。如果有人能指出我的遗漏,我将不胜感激。我知道这两个控件都已创建,但Label上没有显示RichTexBox ...而是在后面创建

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

1 个答案:

答案 0 :(得分: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);
}