设置 SelectionFont c#

setting SelectionFont c#

我正在尝试用 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 系统添加到框中 class:

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"];
    }
}

Additional Info:

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

有人能告诉我哪里错了吗? 谢谢

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

感谢大家的帮助!