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
DecimalPlacesproperty 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 fort, leavingt2with the default increment of 1. Now both controls will use the 0.01 step. - Widened control size: Increasing the
Sizeto 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
相关产品推荐
相关产品推荐

