setting SelectionFont c#

本文关键字:SelectionFont setting | 更新日期: 2023-09-27 17:52:43

我正在尝试在c#中制作高级文本编辑器。我目前有一个toolStripComboBox填充字体名称。当用户单击名称时,应该将SelectionFont设置为该字体。然而,它似乎没有效果。(我也有一个字体大小,它工作得很好)

下面是应用字体的代码:
private void toolStripComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    Font nf = new Font(toolStripComboBox1.SelectedIndex.ToString(), getCntDocument.SelectionFont.Size, getCurrentDocument.SelectionFont.Style);
    getCurrentDocument.SelectionFont = nf;
}

字体直接从InstalledFontFamilies系统类添加到方框中:

private void getFontCollection()
{
    InstalledFontCollection ifonts = new InstalledFontCollection();
    foreach (FontFamily ff in ifonts.Families)
    {
        toolStripComboBox1.Items.Add(ff.Name);
    }
    toolStripComboBox1.SelectedIndex = 0;
}

同样,getCurrentDocument如下:

private RichTextBox getCurrentDocument
{
    get
    {
        return (RichTextBox)tabControl1.SelectedTab.Controls["Body"];
    }
}

额外的信息:

private void formMain_Load(object sender, EventArgs e) 
{ 
   string[] args = System.Environment.GetCommandLineArgs(); 
   string filePath = args[1]; 
   filePath.Replace("''''", "''"); 
   addTab(); 
   getFontCollection(); 
   setFontSizes(); 
   getCurrentDocument.Text = (File.ReadAllText(filePath)); 
} 
我得到unsupportedFormatException

谁能告诉我我哪里错了?由于

setting SelectionFont c#

所以我找到了我的答案,原来我没有得到文本正确的方式…而不是使用toolStripComboBox1.SelectedIndex.ToString(),我必须首先使用ToolStripComboBox.ComboBox变量将ts组合框转换为常规组合框。这样我就可以使用toolStripComboBox1.ComboBox.GetItemText(toolStripComboBox1.ComboBox.SelectedItem)

谢谢大家的帮助!