【发布时间】:2012-04-09 03:31:59
【问题描述】:
我做了一个简单的文本编辑器。其中有一个用于更改字体系列、样式和颜色的组合框。我成功地更改了所选文本的字体属性。 我的代码
private void fontFamilySelectedIndexChanged(object sender, EventArgs e)
{
if (notesCon[currentNotes].SelectedText != "")
{
notesCon[currentNotes].SelectionFont = new Font(fontFamily.SelectedItem.ToString(), notesCon[currentNotes].SelectionFont.Size, notesCon[currentNotes].SelectionFont.Style);
}
}
private void fontStyleSelectedIndexChanged(object sender, EventArgs e)
{
if (notesCon[currentNotes].SelectedText != "")
{
switch (fontStyle.SelectedItem.ToString())
{
case "Bold": notesCon[currentNotes].SelectionFont = new Font(notesCon[currentNotes].SelectionFont.FontFamily, notesCon[currentNotes].SelectionFont.Size, FontStyle.Bold);
break;
case "Italic": notesCon[currentNotes].SelectionFont = new Font(notesCon[currentNotes].SelectionFont.FontFamily, notesCon[currentNotes].SelectionFont.Size, FontStyle.Italic);
break;
case "Regular": notesCon[currentNotes].SelectionFont = new Font(notesCon[currentNotes].SelectionFont.FontFamily, notesCon[currentNotes].SelectionFont.Size, FontStyle.Regular);
break;
case "Strikeout": notesCon[currentNotes].SelectionFont = new Font(notesCon[currentNotes].SelectionFont.FontFamily, notesCon[currentNotes].SelectionFont.Size, FontStyle.Strikeout);
break;
case "Underline": notesCon[currentNotes].SelectionFont = new Font(notesCon[currentNotes].SelectionFont.FontFamily, notesCon[currentNotes].SelectionFont.Size, FontStyle.Underline);
break;
}
}
}
private void fontColorSelectedValueChanged(object sender, EventArgs e)
{
if (notesCon[currentNotes].SelectedText != "")
{
notesCon[currentNotes].SelectionFont = new Font(fontFamily.SelectedItem.ToString(), notesCon[currentNotes].SelectionFont.Size, notesCon[currentNotes].SelectionFont.Style);
notesCon[currentNotes].SelectionColor = fontColor.SelectedItem.Color;
}
}
我的困境:
每当我尝试将富文本框的当前字体属性设置为新的时,它都会成功更改它,但不会保留以前文本的字体属性。我应该如何更改字体属性,而不更改富文本框中文本的先前字体属性? 请帮助,在此先感谢!
【问题讨论】:
标签: c# fonts richtextbox