简体   繁体   English

没有显示RichTextBox文本C#

[英]RichTextBox text is not shown C#

using richtextbox control programatically i'm appending text to the richtextbox . 以编程方式使用richtextbox控件我将文本附加到richtextbox。

richTextBox1.AppendText("hello");

somehow the text appears in the richTextBox1.Text but is not shown in the form. 不知何故,文本出现在richTextBox1.Text但未在表单中显示。 any idea of what might be the problem? 什么可能是什么问题? (I checked the forecolor seems ok). (我检查了forecolor似乎没问题)。 Thanks in advance 提前致谢

Edit: found the root cause (had by mistake the initializeComponent() twice. ) 编辑:找到根本原因(错误地将initializeComponent()两次。)

private void InitializeComponent()
{
    this.richTextBox1 = new System.Windows.Forms.RichTextBox();
    this.SuspendLayout();
    // 
    // richTextBox1
    // 
    this.richTextBox1.Location = new System.Drawing.Point(114, 104);
    this.richTextBox1.Name = "richTextBox1";
    this.richTextBox1.Size = new System.Drawing.Size(100, 96);
    this.richTextBox1.TabIndex = 0;
    this.richTextBox1.Text = "";
    // 
    // Form1
    // 
    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(284, 262);
    this.Controls.Add(this.richTextBox1);
    this.Name = "Form1";
    this.Text = "Form1";
    this.Load += new System.EventHandler(this.Form1_Load);
    this.ResumeLayout(false);

}

   public Form1()
    {
            InitializeComponent();
            InitializeComponent();

    }

    private void Form1_Load(object sender, EventArgs e)
    {
        richTextBox1.AppendText("hello world");
    }`

but still curious about why did this cause this weird behavior? 但仍然好奇为什么这会导致这种奇怪的行为?

Does the same happen when you do richTextBox1.Text = "hello"; 当你执行richTextBox1.Text = "hello";时会发生同样的情况richTextBox1.Text = "hello"; ?

EDIT: trying to explain the problem 编辑:试图解释这个问题

Without seeing the entire code it's difficult for me to know for sure. 没有看到整个代码,我很难确切知道。

But my guess is, that something caused your OnLoad event handler to be called from within the first call to InitializeComponent , and then in the second call the RichTextBox was replaced with a new instance, and your text was added to the old instance. 但我的猜测是,有些东西导致你的OnLoad事件处理程序在第一次调用InitializeComponent被调用,然后在第二次调用中, RichTextBox被一个新实例替换,你的文本被添加到旧实例中。

If you post the minimal code that still has behavior (including the content of InitializeComponent ), I can try help figure out the reason. 如果你发布仍然有行为的最小代码(包括InitializeComponent的内容),我可以尝试帮助找出原因。

EDIT 2 编辑2

Well, when you call InitializeComponent twice, you actually create two instances of all the controls on your Form . 好吧,当您两次调用InitializeComponent ,实际上是在Form上创建了所有控件的两个实例。 So what happened was, the first call created one RichTextBox . 所以发生了什么,第一次调用创建了一个RichTextBox Then the second call created another RichTextBox in exactly the same location, with the same size. 然后第二次调用在完全相同的位置创建了另一个RichTextBox ,大小相同。 Then you set the text to the second RichTextBox . 然后将文本设置为第二个RichTextBox

The reason you can't see the text is because the first RichTextBox is hiding the second one! 你看不到文字的原因是因为第一个RichTextBox 隐藏了第二个! To test that, you can add some code to change the location of richTextBox1 after you set its text, and then you'll see that there are two of them... 要测试它,你可以添加一些代码来在设置文本后更改richTextBox1的位置,然后你会看到有两个代码......

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

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