如何更改 RichTextBox 中的文本大小并删除文本上的突出显示?
How do I change the text in a RichTextBox size and removing the highlight on the text?
这是我的代码。当我打开一个文本文件时,它会更改文本的字体大小,然后为所有文本着色,使其像我一样突出显示 select 所有文本。
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;
但这并没有做到。
您正在直接更改文本 属性。: richTextBox1.Text = s;
如果您不想弄乱任何格式,请不要这样做或 richTextBox1.Text = File.ReadAllText(filename);
。规则见here!
改变这个
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;
}
我不知道你提到的 'highlighting' 但当你在选择之外插入文本时,它可能会将文本恢复为你之前设置的默认字体和属性..
这是我的代码。当我打开一个文本文件时,它会更改文本的字体大小,然后为所有文本着色,使其像我一样突出显示 select 所有文本。
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;
但这并没有做到。
您正在直接更改文本 属性。: richTextBox1.Text = s;
如果您不想弄乱任何格式,请不要这样做或 richTextBox1.Text = File.ReadAllText(filename);
。规则见here!
改变这个
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;
}
我不知道你提到的 'highlighting' 但当你在选择之外插入文本时,它可能会将文本恢复为你之前设置的默认字体和属性..