Search code examples
c#winformsnumericupdown

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


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.


Solution

  • 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.