星期六, 10月 23, 2021

[C#] NumericUpDown

根據 NumericUpDown 文章範例再加上常用功能的筆記
using System;
using System.Windows.Forms;

namespace NnumericUpDownSample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            // 輸入法切換回英數,且停用輸入法切換
            numericUpDown1.ImeMode = ImeMode.Disable;

            // 設定初始值、最大值和最小值
            numericUpDown1.Minimum = -9999999;
            numericUpDown1.Maximum = 9999999;
            numericUpDown1.Value = 9999;
        }

        #region 全選
        private void SelectAll()
        {
            numericUpDown1.Select(0, numericUpDown1.Text.Length);
        }

        private void numericUpDown1_Enter(object sender, EventArgs e)
        {
            SelectAll();
        }

        private void numericUpDown1_MouseDown(object sender, MouseEventArgs e)
        {
            SelectAll();
        }
        #endregion

        private void chkDecimalPlaces_CheckedChanged(object sender, EventArgs e)
        {
            if (numericUpDown1.DecimalPlaces > 0)
            {
                numericUpDown1.DecimalPlaces = 0;
                numericUpDown1.Value = Decimal.Round(numericUpDown1.Value, 0, MidpointRounding.AwayFromZero);
            }
            else
            {
                numericUpDown1.DecimalPlaces = 2;
                numericUpDown1.Increment = 0.25M;
            }
        }

        private void chkThousandsSeparator_CheckedChanged(object sender, EventArgs e)
        {
            numericUpDown1.ThousandsSeparator = !numericUpDown1.ThousandsSeparator;
        }

        private void chkHexadecimal_CheckedChanged(object sender, EventArgs e)
        {
            numericUpDown1.Hexadecimal = !numericUpDown1.Hexadecimal;
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            string info = string.Empty;
            info += $"Value:{numericUpDown1.Value}" + Environment.NewLine;
            info += $"Text:{numericUpDown1.Text}";
            lblInfo.Text = info;
        }
    }
}
[C#] NumericUpDown-2

NumericUpDown.Text Property

官方文件上就可以知道 Text Property 不會出現在屬性視窗內,但是 Code 內是可以直接叫用,透過 Text Property 來自訂全選功能 

  [C#] NumericUpDown-1

ImeMode.Disable

NumericUpDown 為輸入數字使用,因此把強制切換成英數並關閉鍵盤切換輸入法

沒有留言:

張貼留言