I'm developing a pig farm management program in C#. When I select a pig, its information appears and needs to be modified. A pig consists of four elements: sex, condition, gait, and age.
The sex and condition are modified using radio buttons.
The age is modified using a NumericUpDown control.
The problem arises when I want to modify the weight, which is a double (real number). I realized that NumericUpDown only works with integer values by default. I don't know if there's a parameter that allows me to use decimal values or if I need a different component.
What would be the best approach to handle this?
The screenshot is in Spanish since it is the main language of the application. enter image description here
I'm developing a pig farm management program in C#. When I select a pig, its information appears and needs to be modified. A pig consists of four elements: sex, condition, gait, and age.
The sex and condition are modified using radio buttons.
The age is modified using a NumericUpDown control.
The problem arises when I want to modify the weight, which is a double (real number). I realized that NumericUpDown only works with integer values by default. I don't know if there's a parameter that allows me to use decimal values or if I need a different component.
What would be the best approach to handle this?
The screenshot is in Spanish since it is the main language of the application. enter image description here
Share Improve this question asked Mar 26 at 23:25 chamorrochamorro 155 bronze badges 4- 1 First, why is weight a double? Are you using pounds and ounces? Second, what unit would you want the NumericUpDown to use to incrementf? 0.1? 0.01? 0.001? Also, why do you need an image at all in this question? How is posting an image relevant to whether a NumericUpDown can be used for real numbers? – Ken White Commented Mar 26 at 23:39
- @KenWhite I'm using kilograms and I think it should move in 0.001 and the weight is double since a pig can weigh 1 and a half kg which would represent 1.50 kg – chamorro Commented Mar 26 at 23:43
- Next time you're dealing with built-in components or class, in VS just put your cursor in the word and press F1, it will open the full documentation on what options are available – Martheen Commented Mar 27 at 1:17
- I'd give myself a few more options; which implies a few more radio buttons; e.g. Metric vs imperial. Pounds "and" ounces (enter 1 or both); or Pounds/Kg only. In any case, 2 Textboxes and don't worry about decimals. If pounds and/or ounces (or kg and/or g) add both if entered. With say kg only, the first TB is kg; the 2nd the grams. Add together. Set the max length of the TB; e.g. 999 (.999 kg or 999 grams) for the 2nd TB. – Gerry Schmitz Commented Mar 27 at 1:53
1 Answer
Reset to default 3Have you tried setting the increment property to a decimal (e.g. 0.1M)?
There is an example on the increment property page that has an increment of decimal : https://learn.microsoft/en-us/dotnet/api/system.windows.forms.numericupdown.increment
i.e. if your control is numericUpDown1:
numericUpDown1.DecimalPlaces = 2;
numericUpDown1.Increment = 0.25M;
The properties documentation also states that the Value property is a decimal: https://learn.microsoft/en-us/dotnet/api/system.windows.forms.numericupdown.value?view=windowsdesktop-9.0
So, even though the default increment is 1 decimals should be supported. If this doesn't work please confirm a few more details for context, including a code sample and details of the version of framework that you are targeting. HTH, Nick