0

我在将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);
}
4

1 回答 1

0

一般来说,您所要做的就是为您的标签添加一个文本值!您应该注意每个控件的 xy 坐标。

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,但我认为它不是很有用!ARichTextBox可用于显示格式化文本

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);
}
于 2012-08-26T18:33:50.267 回答