任何人都可以告诉我如何使用字体对话框更改字体。我试图得到它,以便选择的文本更改,或者如果没有选择文本,只有标记被更改后的字体(不是整个文本框)。
这是我到目前为止所拥有的。感谢名单
private void menuFont_Click(object sender, EventArgs e)
{
if (fontDialog1.ShowDialog() == DialogResult.OK)
{
if (richtextbox.SelectedText != "")
{
richtextbox.Font = fontDialog1.Font;
}
}}
答案 0 :(得分:1)
private void menuFont_Click(object sender, EventArgs e)
{
if (fontDialog1.ShowDialog() == DialogResult.OK & !String.IsNullOrEmpty(richtextbox.Text))
{
richtextbox.SelectionFont = fontDialog1.Font;
}
else
{
// richtextbox.SelectionFont = ?
}
}
修改强>
如果&&
为fontDialog1.ShowDialog() == DialogResult.OK
,您可以使用false
并且根据user210118
推荐
答案 1 :(得分:0)
是否有SelFont,SelX将字体属性应用于所选文本?现在我考虑一下,也许它是SelectedX,但应用程序是一样的。
答案 2 :(得分:0)
使用RichTextBox的SelectionFont属性,该示例将根据您的需要运行:
private void menuFont_Click(object sender, EventArgs e)
{
if (fontDialog1.ShowDialog() == DialogResult.OK) {
richtextbox.SelectionFont = fontDialog1.Font;
}
}
答案 3 :(得分:0)
要将以下文本输入为不同的字体,而不仅仅是所选文本,您需要向RTB添加运行块,然后写入其中。我为RTB实现了一个工具栏,它可以做这样的事情:
public static void SetFontSize(RichTextBox target, double value)
{
// Make sure we have a richtextbox.
if (target == null)
return;
// Make sure we have a selection. Should have one even if there is no text selected.
if (target.Selection != null)
{
// Check whether there is text selected or just sitting at cursor
if (target.Selection.IsEmpty)
{
// Check to see if we are at the start of the textbox and nothing has been added yet
if (target.Selection.Start.Paragraph == null)
{
// Add a new paragraph object to the richtextbox with the fontsize
Paragraph p = new Paragraph();
p.FontSize = value;
target.Document.Blocks.Add(p);
}
else
{
// Get current position of cursor
TextPointer curCaret = target.CaretPosition;
// Get the current block object that the cursor is in
Block curBlock = target.Document.Blocks.Where
(x => x.ContentStart.CompareTo(curCaret) == -1 && x.ContentEnd.CompareTo(curCaret) == 1).FirstOrDefault();
if (curBlock != null)
{
Paragraph curParagraph = curBlock as Paragraph;
// Create a new run object with the fontsize, and add it to the current block
Run newRun = new Run();
newRun.FontSize = value;
curParagraph.Inlines.Add(newRun);
// Reset the cursor into the new block.
// If we don't do this, the font size will default again when you start typing.
target.CaretPosition = newRun.ElementStart;
}
}
}
else // There is selected text, so change the fontsize of the selection
{
TextRange selectionTextRange = new TextRange(target.Selection.Start, target.Selection.End);
selectionTextRange.ApplyPropertyValue(TextElement.FontSizeProperty, value);
}
}
// Reset the focus onto the richtextbox after selecting the font in a toolbar etc
target.Focus();
}