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

Time:02-24

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.

CodePudding user response:

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

CodePudding user response:

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

CodePudding user response:

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 call UpdateEditText() which forces the control to display the value stored internally.

  • Related