1
我做了一個簡單的文本編輯器。其中,可以使用用於更改字體系列,樣式和顏色的組合框。我成功地更改了所選文本的字體屬性。 我的代碼C#richtextbox字體屬性
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;
}
}
我的困境:
每當我試着豐富的文本框的當前字體屬性設置爲一個新的,它成功地改變了它,但不保留字體屬性以前的文字。我應該如何更改字體屬性,而不必更改富文本框中文本的以前的字體屬性? 請幫助,提前致謝!