回 帖 发 新 帖 刷新版面

主题:求助,程序中有几个text box,change的时候会相互修改应该怎么办

程序如下,想实现的功能很简单,一共八个窗口,想在任何一个窗口输入数值的时候其他窗口的数值会同时改变,但是这样会出现各个窗口相互修改的情况,最后出现"堆栈空间溢出"的问题,向大神们求救怎么修改这个程序,

PS,我完全是生手,只是想写个小程序方便平时使用,望轻喷



Private Sub Text1_Change()
Dim St As String
Dim E1!, E2!, w1!, w2!, F1!, F2!, T1!, T2!
St = Text1.Text
E1 = Val(St)

E2 = E1 * 1000
Text2.Text = E2

If E1 = 0 Then
  Text3.Text = "infinity"
  Text4.Text = "infinity"
Else
  w1 = 6.626176 * 300 / 1.602 / E1
  w2 = 6.626176 * 300 / 1.602 / E1 / 1000
  Text3.Text = w1
  Text4.Text = w2
End If
F1 = E1 / 4.135667516 * 1000 / 2.998 * 100
F2 = E1 / 4.135667516 * 1000
Text5.Text = F1
Text6.Text = F2
If F1 = 0 Then
  Text7.Text = "infinity"
  Text8.Text = "infinity"
Else
  T1 = 1000 / F2
  T2 = 1 / F2
  Text7.Text = T1
  Text8.Text = T2
End If

End Sub


Private Sub Text2_Change()


St = Text2.Text
E2 = Val(St)

E1 = E2 / 1000
Text1.Text = E1

If E1 = 0 Then
  Text3.Text = "infinity"
  Text4.Text = "infinity"
Else
  w1 = 6.626176 * 300 / 1.602 / E1
  w2 = 6.626176 * 300 / 1.602 / E1 / 1000
  Text3.Text = w1
  Text4.Text = w2
End If
F1 = E1 / 4.135667516 * 1000 / 2.998 * 100
F2 = E1 / 4.135667516 * 1000
Text5.Text = F1
Text6.Text = F2
If F1 = 0 Then
  Text7.Text = "infinity"
  Text8.Text = "infinity"
Else
  T1 = 1000 / F2
  T2 = 1 / F2
  Text7.Text = T1
  Text8.Text = T2
End If

End Sub

Private Sub Text3_Change()

Dim St As String

St = Text3.Text
w1 = Val(St)

w2 = w1 / 1000
Text4.Text = w2

If w1 = 0 Then
  Text1.Text = "infinity"
  Text2.Text = "infinity"
Else
  E1 = 6.626176 * 300 / 1.602 / w1
  E2 = 6.626176 * 300 / 1.602 / w1 * 1000
  Text1.Text = E1
  Text2.Text = E2
End If

If w1 = 0 Then
  Text5.Text = "infinity"
  Text6.Text = "infinity"
Else
  F1 = E1 / 4.135667516 * 1000 / 2.998 * 100
  F2 = E1 / 4.135667516 * 1000
  Text5.Text = F1
  Text6.Text = F2
End If


  T1 = 1000 / F2
  T2 = 1 / F2
  Text7.Text = T1
  Text8.Text = T2

End Sub

Private Sub Text4_Change()


St = Text4.Text
w2 = Val(St)

w1 = w2 * 1000
Text3.Text = w1

If w1 = 0 Then
  Text1.Text = "infinity"
  Text1.Text = "infinity"
Else
  E1 = 6.626176 * 300 / 1.602 / w1
  E2 = 6.626176 * 300 / 1.602 / w1 * 1000
  Text1.Text = E1
  Text2.Text = E2
End If

If w1 = 0 Then
  Text5.Text = "infinity"
  Text6.Text = "infinity"
Else
  F1 = E1 / 4.135667516 * 1000 / 2.998 * 100
  F2 = E1 / 4.135667516 * 1000
  Text5.Text = F1
  Text6.Text = F2
End If


  T1 = 1000 / F2
  T2 = 1 / F2
  Text7.Text = T1
  Text8.Text = T2
  
End Sub


Private Sub Text5_Change()


St = Text5.Text
F1 = Val(St)
F2 = F1 * 2.998 / 100
Text6.Text = F2


E1 = F1 * 4.135667516 / 1000 * 2.998 / 100
E2 = E1 * 1000
Text1.Text = E1
Text2.Text = E2

If E1 = 0 Then
  Text3.Text = "infinity"
  Text4.Text = "infinity"
Else
  w1 = 6.626176 * 300 / 1.602 / E1
  w2 = 6.626176 * 300 / 1.602 / E1 / 1000
  Text3.Text = w1
  Text4.Text = w2
End If


If F1 = 0 Then
  Text7.Text = "infinity"
  Text8.Text = "infinity"
Else
  T1 = 1000 / F2
  T2 = 1 / F2
  Text7.Text = T1
  Text8.Text = T2
End If

End Sub

Private Sub Text6_Change()


St = Text6.Text
F2 = Val(St)
F1 = F2 / 2.998 * 100
Text5.Text = F1


E1 = F1 * 4.135667516 / 1000 * 2.998 / 100
E2 = E1 * 1000
Text1.Text = E1
Text2.Text = E2

If E1 = 0 Then
  Text3.Text = "infinity"
  Text4.Text = "infinity"
Else
  w1 = 6.626176 * 300 / 1.602 / E1
  w2 = 6.626176 * 300 / 1.602 / E1 / 1000
  Text3.Text = w1
  Text4.Text = w2
End If


If F1 = 0 Then
  Text7.Text = "infinity"
  Text8.Text = "infinity"
Else
  T1 = 1000 / F2
  T2 = 1 / F2
  Text7.Text = T1
  Text8.Text = T2
End If

End Sub

Private Sub Text7_Change()


St = Text7.Text
T1 = Val(St)
T2 = T1 / 1000
Text8.Text = T2

If T1 = 0 Then
  Text5.Text = "infinity"
  Text6.Text = "infinity"
Else
  F2 = 1000 / T1
  F1 = F2 / 2.998 * 100
  Text5.Text = F1
  Text6.Text = F2
End If

If T1 = 0 Then
  Text1.Text = "infinity"
  Text2.Text = "infinity"
Else
  E1 = F1 * 4.135667516 / 1000 * 2.998 / 100
  E2 = E1 * 1000
  Text1.Text = E1
  Text2.Text = E2
End If


  w1 = 6.626176 * 300 / 1.602 / E1
  w2 = 6.626176 * 300 / 1.602 / E1 / 1000
  Text3.Text = w1
  Text4.Text = w2


End Sub

Private Sub Text8_Change()


St = Text8.Text
T2 = Val(St)
T1 = T2 * 1000
Text7.Text = T1

If T1 = 0 Then
  Text5.Text = "infinity"
  Text6.Text = "infinity"
Else
  F2 = 1000 / T1
  F1 = F2 / 2.998 * 100
  Text5.Text = F1
  Text6.Text = F2
End If

If T1 = 0 Then
  Text1.Text = "infinity"
  Text2.Text = "infinity"
Else
  E1 = F1 * 4.135667516 / 1000 * 2.998 / 100
  E2 = E1 * 1000
  Text1.Text = E1
  Text2.Text = E2
End If


  w1 = 6.626176 * 300 / 1.602 / E1
  w2 = 6.626176 * 300 / 1.602 / E1 / 1000
  Text3.Text = w1
  Text4.Text = w2
End Sub



回复列表 (共2个回复)

沙发

可以在 Option Explicit 下声明一个布尔型数组作为是否调用子过程的标记,数组下标值与文本框对应。

例如:

Option Explicit

dim bj(1 to 8) as boolean

Private Sub Text1_Change()
if not bj(1) then exit sub'如果没有获得焦点,不执行以下代码
'以下是你的原代码
......

bj(1)=false'最后必须加上这一句
end sub

Private Sub Text1_GotFocus()'当文本框获得焦点时才执行代码,其它几个文本框照此办理
bj(1)=true
end sub

Private Sub Text1_LostFocus()'当文本框丧失焦点时不执行代码,其它几个文本框照此办理
bj(1)=false
end sub

我没经过验证,你试试。

板凳

我做个一样的,不过用的是.NET。但是我觉得思路是一样的。通过一个类,这个类记录下所以绑定的文本框。当任意一个文本框发生文字输入事件的时候,向其它的文本框同步内容。这个需要你有面向对象的思维。还有就是有用代码绑定控件的思路。而不是简单的函数式编程。

我来回复

您尚未登录,请登录后再回复。点此登录或注册