You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WinForm中设置NumericUpDown控件Increment为0.01M后数值增减无效果的技术问题

Fixing NumericUpDown Increment Not Working with 0.01 Step in WinForms

Got it, let's break down why your controls aren't behaving as expected and fix it right away.

The core issue here is two-fold:

  • You didn't set the DecimalPlaces property for your NumericUpDown controls (it defaults to 0, meaning only integers are displayed)
  • You forgot to apply the 0.01 increment to your second control (t2)

Even though you set Increment = 0.01M for t, the control was only showing whole numbers—so when you clicked the up/down arrows, the value was changing behind the scenes, but you couldn't see it.

Here's your revised code with the necessary fixes:

namespace Proiect1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        int i = 1;
        int o = 1;
        private void button1_Click(object sender, EventArgs e)
        {
            Label l = new Label();
            Label l2 = new Label();
            NumericUpDown t = new NumericUpDown();
            NumericUpDown t2 = new NumericUpDown();
            l.Top = i * 25;
            l.Left = 3;
            l2.Top = i * 25;
            l2.Left = 63;
            
            // Configure first NumericUpDown
            t.Value = 0;
            t.Increment = 0.01M;
            t.DecimalPlaces = 2; // Critical: enables display of two decimal places
            t.Top = i * 25;
            t.Left = 30;
            t.Size = new System.Drawing.Size(60, 30); // Wider to fit decimal digits
            
            // Configure second NumericUpDown (added missing Increment and DecimalPlaces)
            t2.Value = 0;
            t2.Increment = 0.01M; // Apply the same 0.01 step here
            t2.DecimalPlaces = 2; // Match decimal display for consistency
            t2.Top = i * 25;
            t2.Left = 90;
            t2.Size = new System.Drawing.Size(60, 30); // Wider to fit decimal digits
            
            i++;
            l.Size = new System.Drawing.Size(25, 20);
            l2.Size = new System.Drawing.Size(29, 20);
            l.Text = "n" + o;
            l2.Text = "w" + o;
            o++;
            panel1.Controls.Add(t);
            panel1.Controls.Add(t2);
            panel1.Controls.Add(l);
            panel1.Controls.Add(l2);
            panel1.AutoScroll = true;
        }
    }
}

Key Explanations:

  • DecimalPlaces = 2: This tells the control to display two digits after the decimal point, so you'll actually see the 0.01 increments when using the up/down arrows.
  • Added t2.Increment = 0.01M: Your original code only set the increment for t, leaving t2 with the default increment of 1. Now both controls will use the 0.01 step.
  • Widened control size: Increasing the Size to 60px wide ensures the full decimal value is visible without being truncated.

Give this a run, and both NumericUpDown controls should now increment/decrement smoothly by 0.01 when you use the arrow buttons.

内容的提问来源于stack exchange,提问作者DariusMan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 16:12:50