c# Numericupdown textbox not showing value if i set it to minimum after resetting text

702 Views Asked by At

This code doesn't work if i set the value of the textbox to 1, with a minimum of 1.

numStartChannel.Minimum = 1;
numStartChannel.ResetText();
numStartChannel.Value = 1;

The control actually has the correct value internally, but still displays blank on the form. Note that the reset is actually ran in a click event, not directly before the value setting.

This code DOES work, but I don't know why. numStartChannel.Minimum = 1; numStartChannel.ResetText(); numStartChannel.Value = 2; numStartChannel.Value = 1;

And finally, this code doesn't work:

numStartChannel.Minimum = 1;
numStartChannel.ResetText();
numStartChannel.Value = 1;
numStartChannel.Value = 1;

Can anyone explain this behavior? C# Visual Studio 2022.

3

There are 3 best solutions below

0
John Lord On BEST ANSWER

The answer is that if you clear the text of the control, it doesn't clear the value. Therefore when i was trying to set the value back into it again to display, it wasn't triggering the control to know the value had been updated, because it hadn't. The unfortunate solution is to also set the .Text value to anything non-empty such as a space. Note: intellisense claims there is no such property but there is. Setting the value afterwards causes the control to redraw properly.

2
GetGeeky On

I have tried your code with the same result.

When I remove the numStartChannel.ResetText(); it works for me.

        numStartChannel.Minimum = 1;
        numStartChannel.Value = 2;

My Explination is that the resetText() sets the text to the default. Which is '0'. That is below your minium.

Thats works for me too:

        numStartChannel.Minimum = 0;
        numStartChannel.ResetText();
        numStartChannel.Value = 1;

If you use numStartChannel.Value++; after the reset the Value is displayed correctly.

I have currently no clue why the text is not updated after reseting the text. I think that it might be a bug in the control it self.

Why do you need to use resetText? Setting the value changes the Text.

Used .NET Framework: 4.7.2

2
Caius Jard On

I've never personally regarded the Text property on an NUD as useful from the code's perspective in any way; it's much more logical to just use Value

You want to reset an NUD to Minimum?

NUD.Value = NUD.Minimum;

Avoid using Text; if you want numeric strings, you can string the value yourself:

NUD.Value.ToString("F3"); //eg "123.457" if Value is 123.456789