c# Numericupdown to pass a value to an integer

社会主义新天地 提交于 2020-01-02 06:31:23

问题


Good day everyone, I'm having a problem passing a numericupdown value into an interger.

    private void button5_Click(object sender, EventArgs e)
    {
     int count = numericUpDown1.Value;
     updateCount(count);
    }

So, what I want to do it so pass the value of the numericupdown value into integer named count and then pass the value into a method that updates a table in my database.

I just started c# lately and cant seem to understand some some documentation.

Thank you guys!


回答1:


NumericUpDown.Value returns decimal so you need to round and convert to integer

Use this,

int count = Convert.ToInt32(Math.Round(numericUpDown1.Value, 0));
updateCount(count);

You can convert directly to the integer type if you have set Increment to integer (no decimal points) number, otherwise it is safe to round first before conversion.

So without round

int count = Convert.ToInt32(numericUpDown1.Value);
updateCount(count);



回答2:


The issue is that the numeric value of the NumericUpDown control is decimal and you wanted to assign it to a integer type. You should do a TypeCast to assign it. For the same there is a Convert class you can use it like this

int count = Convert.ToInt32(numericUpDown1.Value);



回答3:


int count = (int)numericUpDown1.Value;



来源:https://stackoverflow.com/questions/32216708/c-sharp-numericupdown-to-pass-a-value-to-an-integer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!