我如何改变文本在RichTextBox的大小和删除文本上的高亮显示
本文关键字:文本 删除 显示 高亮 何改变 改变 RichTextBox | 更新日期: 2023-09-27 17:52:16
这是我的代码。当我打开一个文本文件时它会改变文本的字体大小然后给所有文本上色使其高亮,就像我选中了所有文本一样。
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;
}
我不知道你提到的"突出显示",但是当你在选择之外插入文本时,它可能会将文本恢复为默认字体&