【问题标题】:Increment/Decrement value from NumericUpDown从 NumericUpDown 增加/减少值
【发布时间】:2021-11-21 03:11:25
【问题描述】:

我有以下问题。我用两个 NumericUpDown 创建了一个表单,我想将它们的值增加/减少 0.01。我尝试使用 NumericUpDownObject.Increment=0.01M,但之后没有变化,值没有变化。这是代码:

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;
            t.Value = 0;
            t.Increment = 0.01M;
            t.Top = i * 25;
            t.Left = 30;
            t2.Value = 0;
            t2.Top = i * 25;
            t2.Left = 90;
            i++;
            t.Size =new System.Drawing.Size(30, 30);
            t2.Size = new System.Drawing.Size(30, 30);
            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;


            
        }

【问题讨论】:

  • 可能是因为您在每次点击时都会创建一个新的NumericUpDown?

标签: c# decimal numeric numericupdown


【解决方案1】:

试试这个

这将在创建时设置整个表单,但仅在单击时增加。

看起来您的解决方案每次单击时都会重新创建所有内容。

P.s apols - 自从我做 Winforms/WPF 以来已经有一段时间了

namespace Proiect1
{
    public partial class Form1 : Form
    {
        NumericUpDown _t = new NumericUpDown();
        NumericUpDown _t2 = new NumericUpDown();
        Label _l = new Label();
        Label _l2 = new Label();


        public Form1()
        {
            InitializeComponent();

            int i = 1;
            int o = 1;

            
            _l.Top = i * 25;
            _l.Left = 3;
            _l2.Top = i * 25;
            _l2.Left = 63;
            _t.Value = 0;
            _t.Increment = 0.01;
            _t.Top = i * 25;
            _t.Left = 30;
            _t2.Value = 0;
            _t2.Top = i * 25;
            _t2.Left = 90;
            i++;
            _t.Size =new System.Drawing.Size(30, 30);
            _t2.Size = new System.Drawing.Size(30, 30);
            _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(l_2);
            panel1.AutoScroll = true;
        }

        
        private void button1_Click(object sender, EventArgs e)
        {
            _t.Increment = 0.01;
        }

【讨论】:

  • 增量只是一步。实际值为Value。所以试试t.Value += t.Increment;
  • 现在我在 public void button1_Click 中收到错误“名称 't' 在当前上下文中不存在”。
  • t.Value += t.Increment;也不行
  • 如果我在第一次发布时保留我的代码,我会在 t.Increment=0.01M;如果我将其更改为浮动或任何“无法将类型'浮动'隐式转换为'十进制。”
  • 你可以编辑你的帖子来这么说吗?了解所有细节会很有帮助
猜你喜欢
  • 2012-05-30
  • 2013-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-06
  • 2015-08-09
  • 1970-01-01
相关资源
最近更新 更多