[英]How do I change the text in a RichTextBox size and removing the highlight on the text?
这是我的代码。 当我打开一个文本文件时,它会更改文本的字体大小,但是会为所有文本着色,使其像我选择所有文本一样突出显示。
private void openToolStripMenuItem1_Click(object sender, EventArgs e)
{
OpenFileDialog theDialog = new OpenFileDialog();
theDialog.Title = "Open Text File";
theDialog.Filter = "TXT files|*.txt";
theDialog.InitialDirectory = @"C:\";
if (theDialog.ShowDialog() == DialogResult.OK)
{
string filename = theDialog.FileName;
richTextBox1.Text = File.ReadAllText(filename);
this.richTextBox1.SelectionStart = 0;
this.richTextBox1.SelectionLength = this.richTextBox1.Text.Length;
this.richTextBox1.SelectionFont = new System.Drawing.Font("Maiandra GD", 30);
string s = richTextBox1.Text;
richTextBox1.Clear();
richTextBox1.Text = s;
}
}
我试图添加以下内容:
string s = richTextBox1.Text;
richTextBox1.Clear();
richTextBox1.Text = s;
它确实成功了,但问题是文本现在又回到了原来的小尺寸。 我还尝试过添加以下内容:
this.richTextBox.SelectionStart = 0;
this.richTextBox.SelectionLength = richTextBox.Text.Length;
this.richTextBox.SelectionBackColor = Color.White;
但这没有做到。
您正在直接更改Text属性。: richTextBox1.Text = s;
永远不要这样做或richTextBox1.Text = File.ReadAllText(filename);
如果您不想弄乱任何格式。 看到这里的规则!
改变这个
if (theDialog.ShowDialog() == DialogResult.OK)
{
string filename = theDialog.FileName;
richTextBox1.Text = File.ReadAllText(filename);
this.richTextBox1.SelectionStart = 0;
this.richTextBox1.SelectionLength = this.richTextBox1.Text.Length;
this.richTextBox1.SelectionFont = new System.Drawing.Font("Maiandra GD", 30);
string s = richTextBox1.Text;
richTextBox1.Clear();
richTextBox1.Text = s;
}
对此:
if (theDialog.ShowDialog() == DialogResult.OK)
{
string filename = theDialog.FileName;
string s = File.ReadAllText(filename);
this.richTextBox1.SelectionStart = 0; // or wherever you want to insert..
this.richTextBox1.SelectionLength = 0;
this.richTextBox1.SelectionFont = new System.Drawing.Font("Maiandra GD", 30);
this.richTextBox1.SelectdText = s;
}
我不知道您提到的“突出显示”,但是当您在选择内容之外插入文本时,可能会将文本还原为之前设置的默认字体和属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.