Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# NumericUpDown值更改为默认值?_C# - Fatal编程技术网

C# NumericUpDown值更改为默认值?

C# NumericUpDown值更改为默认值?,c#,C#,我想创建一个表单,允许用户在五个不同的字段(NumericUpDown)中设置一定数量的点。当点数达到0时,用户无法再添加任何点数。(不过,我仍然希望用户能够删除点。) 以下是我目前的代码: private void calculateValue() { decimal tempValue = CMB_num_Aim.Value + CMB_num_Reflexes.Value + CMB_num_Positioning.Value + CMB_num_Movement.Value +

我想创建一个表单,允许用户在五个不同的字段(NumericUpDown)中设置一定数量的点。当点数达到0时,用户无法再添加任何点数。(不过,我仍然希望用户能够删除点。)

以下是我目前的代码:

private void calculateValue() {
  decimal tempValue = CMB_num_Aim.Value + CMB_num_Reflexes.Value + 
  CMB_num_Positioning.Value + CMB_num_Movement.Value + CMB_num_Teamwork.Value;
  controlValue = currentValue - tempValue;
  MyBox.CMB_tb_cv.Text = controlValue.ToString();
}
此方法(calculateValue)用于计算用户剩余的点数(controlValue)

private void CMB\u num\u Aim\u ValueChanged(对象发送方,事件参数e){
calculateValue();
如果(控制值<0){
//在此处防止默认值
MessageBox.Show(“您的积分不足!”);
}
}
此方法(CMB_num_Aim_ValueChanged)在NumericUpDown控件的值更改时激发。每个领域我都有一个,每个领域都做同样的事情

该方法按预期触发,但我无法阻止它发生-用户可以应用比他们更多的点。如何防止用户应用更多点

(我想做一个mouseUp方法,但我不知道用户是否会使用鼠标,或者是否会使用键盘输入值。)

替换

if (controlValue < 0) {
if(控制值<0){


如果(controlValue看起来你想在一些技能之间创建一个积分分配系统-目标、移动、团队合作等。你可以通过在输入时设置
NumericUpDown
控件的
最大值来轻松实现。将所有技能updown控件订阅到同一事件处理程序:

private void SkillNumericUpDown_Enter(object sender, EventArgs e)
{
   var skill = (NumericUpDown)sender;
   var availablePoints = 42;
   var maxSkillPoints = 20; // usually you cannot assign all points to one skill
   var unassignedPoints = availablePoints - SkillPointsAssigned;
   skill.Maximum = Math.Min(maxSkillPoints, unassignedPoints  + skill.Value);

   if (unassignedPoints == 0)
   {
       MessageBox.Show("You are out of points!");
       return;
   }

   if (skill.Value == maxSkillPoints)
   {
       MessageBox.Show("Skill maximized!");
       return;
   }
}

private decimal SkillPointsAssigned =>
    CMB_num_Aim.Value + 
    CMB_num_Reflexes.Value + 
    CMB_num_Positioning.Value + 
    CMB_num_Movement.Value + 
    CMB_num_Teamwork.Value;

好处-您将无法通过箭头或手动输入非法值。

使用属性将最小金额设置为零您可以通过设置前一个值、存储前一个值并将其设置回valuechanged eventKrishna来防止-我曾想过这样做,但我还没有找到一种可靠的方法来做到这一点意外地覆盖了我的“旧值”使用刚刚启动的控件。在标记属性中保留旧值,这样就不需要维护变量。只要验证正确,就更新标记属性。这听起来像是关于ComboBox控件的类似问题。有时提供的控件有点缺乏功能,扩展它们可能是一个很好的方法。
if (controlValue <= 0) {
private void SkillNumericUpDown_Enter(object sender, EventArgs e)
{
   var skill = (NumericUpDown)sender;
   var availablePoints = 42;
   var maxSkillPoints = 20; // usually you cannot assign all points to one skill
   var unassignedPoints = availablePoints - SkillPointsAssigned;
   skill.Maximum = Math.Min(maxSkillPoints, unassignedPoints  + skill.Value);

   if (unassignedPoints == 0)
   {
       MessageBox.Show("You are out of points!");
       return;
   }

   if (skill.Value == maxSkillPoints)
   {
       MessageBox.Show("Skill maximized!");
       return;
   }
}

private decimal SkillPointsAssigned =>
    CMB_num_Aim.Value + 
    CMB_num_Reflexes.Value + 
    CMB_num_Positioning.Value + 
    CMB_num_Movement.Value + 
    CMB_num_Teamwork.Value;