代码之家  ›  专栏  ›  技术社区  ›  SAQ

如何在c#WinForms中接受用户的字体输入

  •  0
  • SAQ  · 技术社区  · 6 年前

    假设这是windows默认字体样式对话框,是否有任何库可供用户启用/调用以选择文本框格式的字体?

    enter image description here

    2 回复  |  直到 6 年前
        1
  •  3
  •   Rufus L    6 年前

    您可以使用 FontDialog class 向用户显示字体对话框。

    这个 FontDialog.ShowDialog method 返回a DialogResult 枚举,然后可以检查用户是否按了“OK”,如果按了,则可以设置 Font 您的财产 TextBox 字体 对话框的属性:

    下面的代码假定您有 文本框 已命名 textBox1 ,和 Button 已命名 button1 。单击按钮允许用户更改 字体 属于 文本框1 :

    private void button1_Click(object sender, EventArgs e)
    {
        var fontDialog = new FontDialog();
    
        // Show the dialog and check the result. 
        // If the user pressed 'Ok', then change the textbox font
        if (fontDialog.ShowDialog() == DialogResult.OK)
        {
            textBox1.Font = fontDialog.Font;
        }
    }
    
        2
  •  0
  •   Josh Whitmer Jack Whipnert    6 年前

    更新:假设您有一个FontDialog对象(从工具箱拖放)和一个文本框,您可以将以下代码添加到您选择的任何事件中。如果用户单击对话框上的“取消”按钮,检查ShowDailog的结果可以跳过分配。

     if(fontDialog1.ShowDialog() == DialogResult.OK)
     {
         textBox1.Font = fontDialog1.Font;
     };