我想要创建一个表单,允许用户在五个不同的字段(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_num_Aim_ValueChanged(object sender, EventArgs e) {
calculateValue();
if (controlValue < 0) {
//Prevent Default here
MessageBox.Show("You are out of points!");
}
}此方法(CMB_num_Aim_ValueChanged)在NumericUpDown控件的值发生更改时触发。每个领域我都有一个,每个都在做同样的事情。
该方法如预期的那样触发,但我无法阻止它的发生-用户可以应用比他们拥有的更多的点。如何防止用户应用更多的点数?
(我想过要创建一个mouseUp方法,但我不知道用户是否会使用鼠标,或者他是否会使用键盘输入值。)
发布于 2017-07-05 10:53:18
似乎你想要创建一个点分配系统之间的一些技能-目标,移动,团队合作等。你可以很容易地通过设置Maximum值的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;效益-您将无法输入非法价值,既不能通过箭头或手动。
发布于 2017-07-05 09:59:04
替换
if (controlValue < 0) {通过
if (controlValue <= 0) {https://stackoverflow.com/questions/44922835
复制相似问题