如果用户减少数字向上控制的值,我该如何减少文本框的值 [英] How do I decrement the value of a textbox if user decrement the value of a numeric up down control

查看:72
本文介绍了如果用户减少数字向上控制的值,我该如何减少文本框的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我一直在寻找解决方案但找不到解决方案。就像标题所说,如果用户减少向上数字向上控制的值,我想改变文本框的值。



例如,如果用户增加它将为文本框值加1的值,但如果他想减少数字向上控制值,则应减去文本值。



我是什么尝试过:



我试图使用值更改事件来做它,它可以用于递增但是每次我递减nup的值它会递增而不是减少。 :

  decimal  totalCost;  //  变量 
私人 void nupWhiteC_ValueChanged( object sender,EventArgs e)
{
if (nupWhiteC.Increment == 1
{
totalCost = totalCost + 0 0.50米;
textBox1.Text = totalCost.ToString();
}
else if (nupWhiteC.Increment == -1)
{
totalCost = totalCost - 0 .50m;
textBox1.Text = totalCost.ToString();
}
}





提前致谢。

解决方案

首先,我建议学习如何调试,因为你会更快地解决这个问题。 增量属性是单击向上或向下时值的更改量。因此,在您的情况下,Increment设置为1,因此第一个if语句始终在运行。再次,只需设置一个断点,你就会看到发生了什么。



你需要做的就是检查numWhite.Value,然后相应地设置你的文本框。 blockquote>

NumericUpDown.Increment属性(System.Windows.Forms) [ ^ ]指定当用户点击向上或向下按钮时,值会改变多少 - 而不是更改多少因此,无论用户是否单击向上或向下按钮,您从Increment属性获得的值都不会更改(除非您的代码专门更改) 。



我建议您使用控件的Tag属性来包含最后一个值:然后您可以判断使用是否增加通过从当前值中减去最后一个值(然后在退出处理程序之前将当前值存储到Tag中)来定义或减少它(以及减少多少)。

我相信你知道,但仅适用于受教育程度较低的人:Tag属性为对象 - 您需要将其强制转换为十进制为了进行减法。


Hello, I was looking for a solution but couldn't find one. Like the heading is saying, I want to change the value of a textbox if user decrements the value of a up numeric up down control.

For example, if a user increment the value it will add 1 to the textbox value but if he wants to decrements the number up down control value the text value should subtract.

What I have tried:

I was trying to do it using the value changed event, it worked for incrementing but every time I decrement the value of nup it increments rather than decrements. :

       decimal totalCost; //variable
private void nupWhiteC_ValueChanged(object sender, EventArgs e)
        {
            if (nupWhiteC.Increment == 1)
            {
                totalCost = totalCost + 0.50m;
                textBox1.Text = totalCost.ToString();
            }
            else if (nupWhiteC.Increment == -1)
            {
                totalCost = totalCost - 0.50m;
                textBox1.Text = totalCost.ToString();
            }
        } 



Thanks in advance.

解决方案

First I would suggest learning how to debug because you'll figure things out like this much faster. The Increment property is how much the value changes when clicking up or down. So, in your case, Increment is set to 1 and so the first if statement is always running. Again, just put a breakpoint and you'll see what is happening.

All you need to do is check numWhite.Value and then set your Textbox accordingly.


The NumericUpDown.Increment Property (System.Windows.Forms)[^] specifies by how much a value will change if the user clicks either of the up or down buttons - not how much it changed by.

So regardless of whether the user clicked the up or down button, the value you get from the Increment property will not change (unless your code specifically changes it).

What I would suggest is that you use the Tag property of the control to contain the last value: you can then tell if the use incremented or decremented it (and by how much) by subtracting the last value from the current one (and then storing the current value into the Tag before you exit the handler).
I'm sure you know, but just for those less educated: the Tag property is an object - to you will need to cast it to a decimal in order to do the subtraction.


这篇关于如果用户减少数字向上控制的值,我该如何减少文本框的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆