以编程方式使用richtextbox控件我将文本附加到richtextbox。
richTextBox1.AppendText("hello");不知何故,文本出现在richTextBox1.Text但未在表单中显示。 什么可能是什么问题? (我检查了forecolor似乎没问题)。 提前致谢
编辑:找到根本原因(错误地将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"); }`但仍然好奇这为什么会导致这种奇怪的行为?
using richtextbox control programatically i'm appending text to the richtextbox .
richTextBox1.AppendText("hello");somehow the text appears in the richTextBox1.Text but is not shown in the form. any idea of what might be the problem? (I checked the forecolor seems ok). Thanks in advance
Edit: found the root cause (had by mistake the initializeComponent() twice. )
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?
最满意答案
当你执行richTextBox1.Text = "hello";时会发生同样的情况richTextBox1.Text = "hello"; ?
编辑:试图解释这个问题
没有看到整个代码,我很难确切知道。
但我的猜测是,有些东西导致你的OnLoad事件处理程序在第一次调用InitializeComponent被调用,然后在第二次调用中, RichTextBox被一个新实例替换,你的文本被添加到旧实例中。
如果你发布仍然有行为的最小代码(包括InitializeComponent的内容),我可以尝试帮助找出原因。
编辑2
好吧,当您两次调用InitializeComponent ,实际上是在Form上创建了所有控件的两个实例。 所以发生了什么,第一次调用创建了一个RichTextBox 。 然后第二次调用在完全相同的位置创建了另一个RichTextBox ,大小相同。 然后将文本设置为第二个RichTextBox 。
您无法看到文本的原因是因为第一个RichTextBox 隐藏了第二个文本! 要测试它,你可以添加一些代码来在设置文本后更改richTextBox1的位置,然后你会看到有两个代码......
Does the same happen when you do 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.
If you post the minimal code that still has behavior (including the content of InitializeComponent), I can try help figure out the reason.
EDIT 2
Well, when you call InitializeComponent twice, you actually create two instances of all the controls on your Form. So what happened was, the first call created one RichTextBox. Then the second call created another RichTextBox in exactly the same location, with the same size. Then you set the text to the second RichTextBox.
The reason you can't see the text is because the first RichTextBox is hiding the second one! 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...
没有显示RichTextBox文本C#(RichTextBox text is not shown C#)以编程方式使用richtextbox控件我将文本附加到richtextbox。
richTextBox1.AppendText("hello");不知何故,文本出现在richTextBox1.Text但未在表单中显示。 什么可能是什么问题? (我检查了forecolor似乎没问题)。 提前致谢
编辑:找到根本原因(错误地将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"); }`但仍然好奇这为什么会导致这种奇怪的行为?
using richtextbox control programatically i'm appending text to the richtextbox .
richTextBox1.AppendText("hello");somehow the text appears in the richTextBox1.Text but is not shown in the form. any idea of what might be the problem? (I checked the forecolor seems ok). Thanks in advance
Edit: found the root cause (had by mistake the initializeComponent() twice. )
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?
最满意答案
当你执行richTextBox1.Text = "hello";时会发生同样的情况richTextBox1.Text = "hello"; ?
编辑:试图解释这个问题
没有看到整个代码,我很难确切知道。
但我的猜测是,有些东西导致你的OnLoad事件处理程序在第一次调用InitializeComponent被调用,然后在第二次调用中, RichTextBox被一个新实例替换,你的文本被添加到旧实例中。
如果你发布仍然有行为的最小代码(包括InitializeComponent的内容),我可以尝试帮助找出原因。
编辑2
好吧,当您两次调用InitializeComponent ,实际上是在Form上创建了所有控件的两个实例。 所以发生了什么,第一次调用创建了一个RichTextBox 。 然后第二次调用在完全相同的位置创建了另一个RichTextBox ,大小相同。 然后将文本设置为第二个RichTextBox 。
您无法看到文本的原因是因为第一个RichTextBox 隐藏了第二个文本! 要测试它,你可以添加一些代码来在设置文本后更改richTextBox1的位置,然后你会看到有两个代码......
Does the same happen when you do 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.
If you post the minimal code that still has behavior (including the content of InitializeComponent), I can try help figure out the reason.
EDIT 2
Well, when you call InitializeComponent twice, you actually create two instances of all the controls on your Form. So what happened was, the first call created one RichTextBox. Then the second call created another RichTextBox in exactly the same location, with the same size. Then you set the text to the second RichTextBox.
The reason you can't see the text is because the first RichTextBox is hiding the second one! 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...
发布评论