我想在键盘文本框中做同样的事情。 防爆。我有几个要键入的文本框。我想自动计算文本框最后一个的平均值。所以我需要使用事件键。但我有很多文本框要做,我应该多次点击这样做。有什么建议吗?喜欢上课方法?写一个循环去做吗?我的代码:
Private Sub gsm1_ave_KeyUp(KeyCode As Integer, Shift As Integer)
Dim gsm1, gsm2, gsm3, gsm4, gsm5 As Variant
gsm1 = Me.gsm1_1
gsm2 = Me.gsm1_2
gsm3 = Me.gsm1_3
gsm4 = Me.gsm1_4
gsm5 = Me.gsm1_5
Me.gsm1_ave = (gsm1 + gsm2 + gsm3 + gsm4 + gsm5) / 5
End Sub
Private Sub gsm2_ave_KeyUp(KeyCode As Integer, Shift As Integer)
Dim gsm1, gsm2, gsm3, gsm4, gsm5 As Variant
gsm1 = Me.gsm2_1
gsm2 = Me.gsm2_2
gsm3 = Me.gsm2_3
gsm4 = Me.gsm2_4
gsm5 = Me.gsm2_5
Me.gsm2_ave = (gsm1 + gsm2 + gsm3 + gsm4 + gsm5) / 5
End Sub
Private Sub gsm3_ave_KeyUp(KeyCode As Integer, Shift As Integer)
Dim gsm1, gsm2, gsm3, gsm4, gsm5 As Variant
gsm1 = Me.gsm3_1
gsm2 = Me.gsm3_2
gsm3 = Me.gsm3_3
gsm4 = Me.gsm3_4
gsm5 = Me.gsm3_5
Me.gsm3_ave = (gsm1 + gsm2 + gsm3 + gsm4 + gsm5) / 5
End Sub
答案 0 :(得分:0)
一些建议:
您可能想尝试在_Change事件中而不是KeyUp中执行此操作,因为您真正想要做的是我在文本框中的值更改时重新计算平均值,而不是在按下某个键时。
如果您将代码重写为
Me.gsm1_ave = (Me.gsm1_1 + Me.gsm1_2 + Me.gsm1_3 + Me.gsm1_4 + Me.gsm1_5) / 5
对我来说,这看起来只有一行。没有太多开销。但是,是的,如果你想在任何这些文本框发生变化时重新计算平均值,你需要在所有5个框中处理_Change事件,在每个框中执行相同的行。
答案 1 :(得分:0)
easy 这里要做的就是将该功能提取到子程序中。
Private Sub HandleKeyPressEvent(cntrl)
Dim gsm1, gsm2, gsm3, gsm4, gsm5 As Variant
gsm1 = Me.gsm3_1
gsm2 = Me.gsm3_2
gsm3 = Me.gsm3_3
gsm4 = Me.gsm3_4
gsm5 = Me.gsm3_5
cntrl = (gsm1 + gsm2 + gsm3 + gsm4 + gsm5) / 5
End Sub
然后从事件中调用它。
Private Sub gsm1_ave_KeyUp(KeyCode As Integer, Shift As Integer)
HandleKeyPressEvent Me.gsm1_ave
End Sub
Private Sub gsm2_ave_KeyUp(KeyCode As Integer, Shift As Integer)
HandleKeyPressEvent Me.gsm2_ave
End Sub
Private Sub gsm3_ave_KeyUp(KeyCode As Integer, Shift As Integer)
HandleKeyPressEvent Me.gsm2_ave
End Sub