3

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.

8
  • Framework or Core project? WimForms controls and designer are still a bit wonky in Core; likely bug. I'd test it in Framework 4.8 to be sure. Commented Feb 23, 2022 at 9:21
  • 1
    Why are you calling ResetText()? numStartChannel.Minimum = 1; numStartChannel.Value = 1; is enough. The Text is irrelevant, what matters in a NUD is the Value. Describe a little better the context of this operation and post some code that can reproduce the issue. E.g., is databindings involved? Did you bind the Text property? Commented Feb 23, 2022 at 9:22
  • Getting NUD to misbehave is not that difficult, it is one of the controls done by the B-team. It should have overridden ResetText() to ensure the NUD always displays a value, but that didn't happen. The fix is simple, just don't call it since assigning Value already resets the text. Commented Feb 23, 2022 at 10:24
  • NumericUpDown.Text property is a useless property and that's why the property have been hidden in code editor and in property grid. Also ResetText method; it belongs to the Control base class as is really not intended to do any special thing but resetting the text at design-time or run-time to the default value which is empty. Commented Feb 23, 2022 at 15:21
  • You should always set the Minimum, Maximum and Value and they will take care of the Text as well. You don't need to call ResetText or set the Text of the control. Commented Feb 23, 2022 at 15:22

3 Answers 3

4

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

Sign up to request clarification or add additional context in comments.

2 Comments

is there a way to clear the text in the control? I was using resettext to make it blank.
Got the solution. ResetText is just clearing the text but leaves the value in the control. Trying to set the exact same value to it again simply didn't trigger the control because the value didn't change.
2

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.

Comments

1

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

2 Comments

i wasn't trying to set it to minimum. The value i was setting it to was coincidentally minimum. The first device I save settings for is going to start at channel 1.
Bind the NUD's Value property to the application settings

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.