如何从一个数字中减去可变数量的其他数字?
How to subtract from a number with a variable amount of other numbers?
抱歉进行了编辑,我很累而且我是个白痴,所以这是我的问题=
假设这是我的代码:
Dim ogquantity,subtractedquantity as double
Private Sub CalculateMe
ogquantity = numericupdown1.value
subtractedquantity = ogquantity - numericupdown2.value
subtractedquantity = ogquantity - numericupdown3.value
subtractedquantity = ogquantity - numericupdown4.value
subtractedquantity = ogquantity - numericupdown5.value
label1.text = subtractedquantity
End Sub
然后我将“CalculateMe”设置为 1-5 的代码numericupdowns_valuechanged
这不会输出更改的值..
我之所以这样设置它,是因为在实际代码中,numericupdown 可能会更改 subtractedquantity1 或 subtractedquantity28,它由每个 numericupdown 的下拉菜单决定。
像这样:
Dim item1stock, item1originalstock As Double
If itemselector1name.Text = itemdefiner1name.text Then
item1stock = item1originalstock - itemselector1quantity.value
itemselector1label.text = item1stock
End If
If itemselector2name.Text = itemdefiner1name.text Then
item1stock = item1originalstock - itemselector2quantity.value
itemselector2label.text = item1stock
End If
我会重复检查每个这样的项目,但如果两个不同的 'itemselectorquantity' 应该从我不知道如何设置的相同原始库存数量中减去。我在这里尽力而为,但我是编码新手,不擅长解释。谢谢!
你的问题相当混乱,但是,作为一个例子,如果你想使用零,一组控件中的一个或多个基于另一组控件,你可能会做这样的事情,考虑这个例子CheckBoxes
指定要从初始数量中减去哪个 NumericUpDowns
:
Private Function SubtractCheckedValues(value As Decimal) As Decimal
Dim checkBoxes = {CheckBox1, CheckBox2, CheckBox3, CheckBox4}
Dim numericUpDowns = {NumericUpDown1, NumericUpDown2, NumericUpDown3, NumericUpDown4}
For i = 0 To checkBoxes.GetUpperBound(0)
If checkBoxes(i).Checked Then
value -= numericUpDowns(i).Value
End If
Next
Return value
End Function
抱歉进行了编辑,我很累而且我是个白痴,所以这是我的问题= 假设这是我的代码:
Dim ogquantity,subtractedquantity as double
Private Sub CalculateMe
ogquantity = numericupdown1.value
subtractedquantity = ogquantity - numericupdown2.value
subtractedquantity = ogquantity - numericupdown3.value
subtractedquantity = ogquantity - numericupdown4.value
subtractedquantity = ogquantity - numericupdown5.value
label1.text = subtractedquantity
End Sub
然后我将“CalculateMe”设置为 1-5 的代码numericupdowns_valuechanged 这不会输出更改的值.. 我之所以这样设置它,是因为在实际代码中,numericupdown 可能会更改 subtractedquantity1 或 subtractedquantity28,它由每个 numericupdown 的下拉菜单决定。 像这样:
Dim item1stock, item1originalstock As Double
If itemselector1name.Text = itemdefiner1name.text Then
item1stock = item1originalstock - itemselector1quantity.value
itemselector1label.text = item1stock
End If
If itemselector2name.Text = itemdefiner1name.text Then
item1stock = item1originalstock - itemselector2quantity.value
itemselector2label.text = item1stock
End If
我会重复检查每个这样的项目,但如果两个不同的 'itemselectorquantity' 应该从我不知道如何设置的相同原始库存数量中减去。我在这里尽力而为,但我是编码新手,不擅长解释。谢谢!
你的问题相当混乱,但是,作为一个例子,如果你想使用零,一组控件中的一个或多个基于另一组控件,你可能会做这样的事情,考虑这个例子CheckBoxes
指定要从初始数量中减去哪个 NumericUpDowns
:
Private Function SubtractCheckedValues(value As Decimal) As Decimal
Dim checkBoxes = {CheckBox1, CheckBox2, CheckBox3, CheckBox4}
Dim numericUpDowns = {NumericUpDown1, NumericUpDown2, NumericUpDown3, NumericUpDown4}
For i = 0 To checkBoxes.GetUpperBound(0)
If checkBoxes(i).Checked Then
value -= numericUpDowns(i).Value
End If
Next
Return value
End Function