There is a NumericUpDown, which is set to the maximum value:

this.DimCountUpDown.Maximum = new decimal(new int[] { 10000, 0, 0, 0}); 

I need to track when the user enters more than 10,000,000 from the keyboard and give him the corresponding notification. I try to do this:

  private void DimCountUpDown_KeyUp(object sender, KeyEventArgs e) { if (DimCountUpDown.Value > 10000) { MessageBox.Show("Π—Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ Π½Π΅ Π΄ΠΎΠ»ΠΆΠ½ΠΎ ΠΏΡ€Π΅Π²Ρ‹ΡˆΠ°Ρ‚ΡŒ 10000", "Ошибка Π²Π²ΠΎΠ΄Π°"); DimCountUpDown.Value = 10000; } } 

However, before getting here the entered value (for example, 10001) is automatically set to a maximum (10,000) and, accordingly, the condition does not work ...

  • Well, do not set the maximum value - VladD
  • @VladD, what is it like? Maximum must contain some value. - UnityMan
  • And if you remove the line this.DimCountUpDown.Maximum = ... in general? - VladD
  • @VladD, if you remove it, the default value for this control is set to 100. - UnityMan
  • one
    Okay, try this.DimCountUpDown.Maximum = decimal.MaxValue; Well, or for example, the value of 10001 should be enough. - VladD

2 answers 2

The idea is that your check happens in DimCountUpDown_KeyUp , so you need to disable the action of the maximum. To do this, it is enough to put in Maximum value greater than 10,000, for example, 10001. Or not to think, you can simply put the maximum possible:

 this.DimCountUpDown.Maximum = decimal.MaxValue; 
  • By the way, in Form1.Designer.cs the string <! - language: c # -> this.DimCountUpDown.Maximum = decimal.MaxValue; converted to <! - language: c # -> this.DimCountUpDown.Maximum = new decimal (new int [] {-1, -1, -1, 0}); Damn, in the comments block code does not issue? - UnityMan
  • one
    @UnityMan: Apparently, this is the same thing. The documentation describes the exact meaning of these bytes. - VladD
  • one
    @UnityMan use ``: ΠΊΠΎΠ΄ - andreycha

DimCountUpDown_KeyUp

Connect the handler to the KeyDown event.

UPDATE:

NumericUpDown is a container for controls: UpDownButtons and UpDownEdit.
Despite the fact that they are not public, but the base class is Control.
If you need to display a message when pressing updown, you can connect the handler to the Click event.

 var max = 10000; // ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊ наТатия ΠΊΠ½ΠΎΠΏΠΊΠΈ updown void UpDownButton_Click(object sender, EventArgs e) { var c = (sender as Control).Parent as NumericUpDown; // Π²Ρ‹Π²ΠΎΠ΄ΠΈΠΌ сообщСниС if (c.Value == max) { MessageBox.Show("Π—Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ Π½Π΅ Π΄ΠΎΠ»ΠΆΠ½ΠΎ ΠΏΡ€Π΅Π²Ρ‹ΡˆΠ°Ρ‚ΡŒ " + max, "Ошибка Π²Π²ΠΎΠ΄Π°"); } } var f = new Form(); var c = new NumericUpDown() { Parent = f, Maximum = max, Value = 9999 }; // ΠΏΠΎΠ΄ΠΊΠ»ΡŽΡ‡Π°Π΅ΠΌ ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊ ΠΊ updown foreach (var b in c.Controls.OfType<Control>()) b.Click += UpDownButton_Click; f.ShowDialog(); 
  • This is not an option, because there in DimCountUpDown.Value will be the previous value. - UnityMan
  • understandably. I updated the answer. - Stack