【问题标题】:How do I take font input from the user in c# WinForms如何在 c# WinForms 中从用户那里获取字体输入
【发布时间】:2018-05-08 15:32:58
【问题描述】:

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

【问题讨论】:

  • “链接”到文本框是什么意思?
  • 如果您使用的是 Winforms,请将字体对话框控件添加到您的表单中。
  • 就您的问题而言,当然有办法做到这一点。您在日常生活中的其他应用程序中没有看到它吗?您在下面已经有了非常正确的精彩答案。

标签: c# winforms user-interface


【解决方案1】:

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

FontDialog.ShowDialog method 返回一个DialogResult 枚举,然后您可以检查用户是否按下“确定”,如果他们按下了“确定”,那么您可以将TextBoxFont 属性设置为@ 987654329@对话框的属性:

下面的代码假设您有一个名为textBox1TextBox 和一个名为button1Button。点击按钮允许用户更改FonttextBox1

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】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-19
      相关资源
      最近更新 更多