简体   繁体   English

向“ RichTextBox”添加“标签”控件

[英]Adding a 'Label' control to a 'RichTextBox'

I'm having trouble adding a Label control to a RichTextBox . 我在将Label控件添加到RichTextBox遇到麻烦。 Clearly, there must be something that I'm missing in the code. 显然,代码中肯定缺少某些内容。 I would appreciate it if someone could point out my omission. 如果有人可以指出我的遗漏,我将不胜感激。 I know both of the controls are created, but the Label is not displayed on top of the RichTexBox ...instead, it's created behind it. 我知道这两个控件均已创建,但是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);
}

In general all you have to do is add a text value to your label! 通常,您要做的就是在标签上添加文本值 And you should pay attention to the xy coordinates for each of your controls. 并且您应该注意每个控件的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
}

And although it is possible to add a label to a RichTextBox control, I don't think it is very useful! 并且尽管可以 RichTextBox控件添加标签 ,但我认为它不是很有用! A RichTextBox can be used to display and format text : 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM