📌  相关文章
📜  如何在C#中设置NumericUpDown的字体?

📅  最后修改于: 2021-05-29 16:04:31             🧑  作者: Mango

在Windows窗体中,NumericUpDown控件用于提供Windows旋转框或显示数字值的上下控件。换句话说,NumericUpDown控件提供了一个界面,该界面使用向上和向下箭头移动并保存一些预定义的数值。在NumericUpDown控件中,您可以使用Font属性设置上下控件中存在的内容的字体。您可以通过两种不同的方式设置此属性:

1.设计时:这是设置NumericUpDown字体的最简单方法,如以下步骤所示:

  • 第1步:创建一个Windows窗体,如下图所示:

    Visual Studio->文件->新建->项目-> WindowsFormApp

  • 步骤2:接下来,将NumericUpDown控件从窗体上的工具箱中拖放,如下图所示:

  • 步骤3:拖放之后,您将转到NumericUpDown的属性并设置NumericUpDown的字体,如下图所示;

    输出:

2.运行时:比上述方法有些棘手。在此方法中,可以借助给定的语法以编程方式设置NumericUpDown控件中存在的内容的字体:

public virtual System.Drawing.Font Font { get; set; }

此处,字体表示NumericUpDown控件中存在的内容的字体。以下步骤显示了如何动态设置NumericUpDown的字体:

  • 步骤1:使用NumericUpDown类提供的NumericUpDown()构造函数创建NumericUpDown。
    // Creating a NumericUpDown
    NumericUpDown n = new NumericUpDown();
    
  • 步骤2:创建NumericUpDown之后,设置NumericUpDown类提供的NumericUpDown的Font属性。
    // Setting the font
    n.Font = new Font("Bodoni MT", 12);
    
  • 步骤3:最后,使用以下语句将此NumericUpDown控件添加到表单中:
    // Adding NumericUpDown control on the form
    this.Controls.Add(n);
    

    例子:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
      
    namespace WindowsFormsApp42 {
      
    public partial class Form1 : Form {
      
        public Form1()
        {
            InitializeComponent();
        }
      
        private void Form1_Load(object sender, EventArgs e)
        {
      
            // Creating and setting the 
            // properties of the labels
            Label l1 = new Label();
            l1.Location = new Point(348, 61);
            l1.Size = new Size(215, 20);
            l1.Text = "Form";
            l1.Font = new Font("Bodoni MT", 12);
            this.Controls.Add(l1);
      
            Label l2 = new Label();
            l2.Location = new Point(242, 136);
            l2.Size = new Size(103, 20);
            l2.Text = "Enter Age";
            l2.Font = new Font("Bodoni MT", 12);
            this.Controls.Add(l2);
      
            // Creating and setting the 
            // properties of NumericUpDown
            NumericUpDown n = new NumericUpDown();
            n.Location = new Point(386, 130);
            n.Size = new Size(126, 26);
            n.Font = new Font("Bodoni MT", 12);
            n.Value = 18;
            n.Minimum = 18;
            n.Maximum = 30;
            n.BackColor = Color.LightGreen;
            n.ForeColor = Color.DarkGreen;
            n.Increment = 1;
            n.Name = "MySpinBox";
      
            // Adding this control 
            // to the form
            this.Controls.Add(n);
        }
    }
    }
    

    输出: