[英]Can't write into a RichTextBox control
我無法在richTextBox或textBox中寫入,每當我開始寫一些它凍結我的程序。 知道它是什么? 我沒有改變textBox的代碼或屬性。
using System.Text;
using System.Windows.Forms;
using System.IO;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
File.WriteAllText("TextFile1.txt", richTextBox1.Text);
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
richTextBox1.LoadFile ("TextFile1.txt");
}
}
看起來您正在將文件加載到您正在編寫的文本框中:
所以擺脫這段代碼:
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
richTextBox1.LoadFile ("TextFile1.txt");
}
嘗試將其移動到表單的OnLoad方法中(假設您希望在表單打開時填充文本框):
protected override void OnLoad(EventArgs e) {
base.OnLoad(e);
richTextBox1.LoadFile ("TextFile1.txt");
}
RichTextBox還有一個SaveFile方法。 從您的代碼中不清楚“豐富”文本對應用程序是否重要。
bool _isLoading = false;
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
if(_isLoading) return;
_isLoading = true;
richTextBox1.LoadFile ("TextFile1.txt");
_isLoading = false;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.