| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 631 人关注过本帖
标题:[讨论]请教如何能使下列程序代码变的更简洁一些?
只看楼主 加入收藏
随波逐流
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2006-5-9
收藏
 问题点数:0 回复次数:6 
[讨论]请教如何能使下列程序代码变的更简洁一些?

Option Explicit

Private Sub Combo1_Change()
List1.Clear
List2.Clear
Command1.SetFocus
End Sub

Private Sub Combo1_Click()
List1.Clear
List2.Clear
Command1.SetFocus
End Sub

Private Sub Combo2_Change()
List1.Clear
List2.Clear
End Sub

Private Sub Combo2_Click()
List1.Clear
List2.Clear
End Sub

Private Sub Command1_Click()
Dim a As Integer
Dim b, c, d, e, f, g, h, y, z(4) As Variant
a = Val(Combo1.Text)
If Combo2.Text = "" Then
MsgBox "系数不能为空", vbInformation + vbOKOnly, "提示"
Exit Sub
End If
For b = 1 To 5
For c = 0 To 10
For d = 0 To 10
If Combo2.Text = "2" Then
If (c + d) <= 10 And (c + d) > 0 Then
If (a \ b) = (c + d) Then
z(1) = b & "×" & "(" & (c & "+" & d) & ")"
List1.AddItem z(1)
y = a Mod b
If (a Mod b) = 0 Then
List2.AddItem ""
Else
List2.AddItem y
End If
End If
End If
End If
For e = 0 To 10
If Combo2.Text = "3" Then
If (c + d + e) <= 10 And (c + d + e) > 0 Then
If (a \ b) = (c + d + e) Then
z(2) = b & "×" & "(" & (c & "+" & d & "+" & e) & ")"
List1.AddItem z(2)
y = a Mod b
If (a Mod b) = 0 Then
List2.AddItem ""
Else
List2.AddItem y
End If
End If
End If
End If
For f = 0 To 10
If Combo2.Text = "4" Then
If (c + d + e + f) <= 10 And (c + d + e + f) > 0 Then
If (a \ b) = (c + d + e + f) Then
z(3) = b & "×" & "(" & (c & "+" & d & "+" & e & "+" & f) & ")"
List1.AddItem z(3)
y = a Mod b
If (a Mod b) = 0 Then
List2.AddItem ""
Else
List2.AddItem y
End If
End If
End If
End If
For g = 0 To 10
If Combo2.Text = "5" Then
If (c + d + e + f + g) <= 10 And (c + d + e + f + g) > 0 Then
If (a \ b) = (c + d + e + f + g) Then
z(4) = b & "×" & "(" & (c & "+" & d & "+" & e & "+" & f & "+" & g) & ")"
List1.AddItem z(4)
y = a Mod b
If (a Mod b) = 0 Then
List2.AddItem ""
Else
List2.AddItem y
End If
End If
End If
End If

Next
Next
Next
Next
Next
Next

End Sub

Private Sub Command2_Click()
Unload Me
End Sub

Private Sub Command3_Click()
Unload Me
frmLogin.Show
End Sub

Private Sub Form_Load()
Dim i, x As Integer
For i = 10 To 50
Combo1.AddItem i
Next i

For x = 2 To 5
Combo2.AddItem x
Next x


End Sub

搜索更多相关主题的帖子: Sub Private End Click 
2006-05-25 10:05
dragonfly
Rank: 5Rank: 5
等 级:贵宾
威 望:17
帖 子:1024
专家分:0
注 册:2006-3-20
收藏
得分:0 
不知你的简洁是啥意思?是代码优化?代码规范整齐?
我指出一点:
书写不规范,阅读起来很费劲,另外
For b = 1 To 5
For c = 0 To 10
For d = 0 To 10
If Combo2.Text = "2" Then
If (c + d) <= 10 And (c + d) > 0 Then
If (a \ b) = (c + d) Then
z(1) = b & "×" & "(" & (c & "+" & d) & ")"
List1.AddItem z(1)
y = a Mod b
If (a Mod b) = 0 Then
List2.AddItem ""
Else
List2.AddItem y
End If
End If
End If
End If

If 和 End If 必须对齐,其内的内容缩进一个Tab,建议你多看看代码书写规范!

2006-05-25 12:39
随波逐流
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2006-5-9
收藏
得分:0 

是代码优化
我刚学VB,请各位多指教

2006-05-25 13:00
xinfresh
Rank: 4
等 级:贵宾
威 望:13
帖 子:594
专家分:0
注 册:2006-1-13
收藏
得分:0 
强烈同意二楼的观点!!代码书写要有好习惯

E-mail:xinfresh@QQ:383094053校内:http:///getuser.do?id=234719042
2006-05-25 19:13
maui2000
Rank: 1
等 级:新手上路
威 望:1
帖 子:113
专家分:0
注 册:2006-5-23
收藏
得分:0 
没仔细看楼主程序的目的
不过看到很多IF
而且多是对一个对象的判断
那么可以换成
select case 来做
2006-05-26 09:08
dragonfly
Rank: 5Rank: 5
等 级:贵宾
威 望:17
帖 子:1024
专家分:0
注 册:2006-3-20
收藏
得分:0 

得到xinfresh的赞同很荣幸,荣幸之余顺手贴上我手头正在写的一段代码,虽然不一定严格按照VB规范书写,但是起码要整齐,要有自己的风格(或者说习惯),这样就方便了以后的代码修改、代码移植或者代码共享,希望能对楼主有用

Private Function IsInputOk(ByVal Index As Integer) As Boolean

Const LowLimit = 300
Const UpLimit = 1100

Dim StartW(2) As Double
Dim EndW(2) As Double
Dim Interval As Double
Dim Speed As Integer
Dim MeasureTimes As Integer
Dim DelayTime As Double
Dim RepeatTimes As Double
Dim IntervalTime As Double
Dim WlPoint As Double

On Error GoTo Err_Hand

If ChkGrating1(Index).Value <> 1 And ChkGrating2(Index).Value <> 1 And ChkGrating3(Index).Value <> 1 Then
Call MsgBox("请选择使用的光栅号!", vbInformation, "警告!")
ChkGrating1(Index).SetFocus
GoTo Err_Hand
End If

If ChkGrating1(Index).Value = vbChecked Then
If Not IsNumeric(TxtStart1(Index).Text) Then
Call MsgBox("请输入数字!", vbInformation, "警告!")
TxtStart1(Index).SetFocus
GoTo Err_Hand
End If
If Not IsNumeric(TxtEnd1(Index).Text) Then
Call MsgBox("请输入数字!", vbInformation, "警告!")
TxtEnd1(Index).SetFocus
GoTo Err_Hand
End If
StartW(0) = Val(TxtStart1(Index).Text)
EndW(0) = Val(TxtEnd1(Index).Text)
If StartW(0) >= EndW(0) Then
Call MsgBox("起始波长不能大于或者等于终止波长!", vbInformation, "警告!")
TxtStart1(Index).SetFocus
GoTo Err_Hand
End If
If StartW(0) < LowLimit Then
Call MsgBox("起始波长不能小于" & LowLimit, vbInformation, "警告!")
TxtStart1(Index).SetFocus
GoTo Err_Hand
End If
If EndW(0) > UpLimit Then
Call MsgBox("终止波长不能大于" & UpLimit, vbInformation, "警告!")
TxtEnd1(Index).SetFocus
GoTo Err_Hand
End If
End If

If ChkGrating2(Index).Value = vbChecked Then
If Not IsNumeric(TxtStart2(Index).Text) Then
Call MsgBox("请输入数字!", vbInformation, "警告!")
TxtStart2(Index).SetFocus
GoTo Err_Hand
End If
If Not IsNumeric(TxtEnd2(Index).Text) Then
Call MsgBox("请输入数字!", vbInformation, "警告!")
TxtEnd2(Index).SetFocus
GoTo Err_Hand
End If
StartW(1) = Val(TxtStart2(Index).Text)
EndW(1) = Val(TxtEnd2(Index).Text)
If StartW(1) >= EndW(1) Then
Call MsgBox("起始波长不能大于或者等于终止波长!", vbInformation, "警告!")
TxtStart2(Index).SetFocus
GoTo Err_Hand
End If
If StartW(1) < LowLimit Then
Call MsgBox("起始波长不能小于" & LowLimit, vbInformation, "警告!")
TxtStart2(Index).SetFocus
GoTo Err_Hand
End If
If EndW(1) > UpLimit Then
Call MsgBox("终止波长不能大于" & UpLimit, vbInformation, "警告!")
TxtEnd2(Index).SetFocus
GoTo Err_Hand
End If
End If

If ChkGrating3(Index).Value = vbChecked Then
If Not IsNumeric(TxtStart3(Index).Text) Then
Call MsgBox("请输入数字!", vbInformation, "警告!")
TxtStart3(Index).SetFocus
GoTo Err_Hand
End If
If Not IsNumeric(TxtEnd3(Index).Text) Then
Call MsgBox("请输入数字!", vbInformation, "警告!")
TxtEnd3(Index).SetFocus
GoTo Err_Hand
End If
StartW(2) = Val(TxtStart3(Index).Text)
EndW(2) = Val(TxtEnd3(Index).Text)
If StartW(2) >= EndW(2) Then
Call MsgBox("起始波长不能大于或者等于终止波长!", vbInformation, "警告!")
TxtStart3(Index).SetFocus
GoTo Err_Hand
End If
If StartW(2) < LowLimit Then
Call MsgBox("起始波长不能小于" & LowLimit, vbInformation, "警告!")
TxtStart3(Index).SetFocus
GoTo Err_Hand
End If
If EndW(2) > UpLimit Then
Call MsgBox("终止波长不能大于" & UpLimit, vbInformation, "警告!")
TxtEnd3(Index).SetFocus
GoTo Err_Hand
End If
End If

If IsNumeric(CboInterval(Index).Text) Then '扫描间隔0.01到800nm
Interval = Val(CboInterval(Index).Text)
If Interval < 0.01 Or Interval > 800 Then
Call MsgBox("扫描间隔不能小于0.01nm或者大于800nm!", vbInformation, "警告!")
CboInterval(Index).SetFocus
GoTo Err_Hand
End If
Else
Call MsgBox("请输入数字!", vbInformation, "警告!")
CboInterval(Index).SetFocus
GoTo Err_Hand
End If

If IsNumeric(CboSpeed(Index).Text) Then '扫描速度0到255
Speed = Val(CboSpeed(Index).Text)
If Interval < 0 Or Interval > 255 Then
Call MsgBox("扫描速度不能小于0或者大于255!", vbInformation, "警告!")
CboSpeed(Index).SetFocus
GoTo Err_Hand
End If
Else
Call MsgBox("请输入数字!", vbInformation, "警告!")
CboSpeed(Index).SetFocus
GoTo Err_Hand
End If

If IsNumeric(CboMeasureTimes(Index).Text) Then '测量次数1到100次
MeasureTimes = Val(CboMeasureTimes(Index).Text)
If MeasureTimes < 1 Or MeasureTimes > 100 Then
Call MsgBox("测量次数不能小于1或者大于100!", vbInformation, "警告!")
CboMeasureTimes(Index).SetFocus
GoTo Err_Hand
End If
Else
Call MsgBox("请输入数字!", vbInformation, "警告!")
CboMeasureTimes(Index).SetFocus
GoTo Err_Hand
End If

If IsNumeric(CboDelayTime(Index).Text) Then '延迟时间0.001到100秒
DelayTime = Val(CboDelayTime(Index).Text)
If DelayTime < 0.001 Or DelayTime > 100 Then
Call MsgBox("延迟时间不能小于0.001或者大于100!", vbInformation, "警告!")
CboDelayTime(Index).SetFocus
GoTo Err_Hand
End If
Else
Call MsgBox("请输入数字!", vbInformation, "警告!")
CboDelayTime(Index).SetFocus
GoTo Err_Hand
End If

If IsNumeric(CboRepeatTime(Index).Text) Then '重复次数1到100次
RepeatTimes = Val(CboRepeatTime(Index).Text)
If RepeatTimes < 1 Or RepeatTimes > 100 Then
Call MsgBox("重复次数不能小于1或者大于100!", vbInformation, "警告!")
CboRepeatTime(Index).SetFocus
GoTo Err_Hand
End If
Else
Call MsgBox("请输入数字!", vbInformation, "警告!")
CboRepeatTime(Index).SetFocus
GoTo Err_Hand
End If

If IsNumeric(CboIntervalTime(Index).Text) Then '间隔时间0.001到100秒
IntervalTime = Val(CboIntervalTime(Index).Text)
If IntervalTime < 0.001 Or IntervalTime > 100 Then
Call MsgBox("间隔时间不能小于0.001或者大于100!", vbInformation, "警告!")
CboIntervalTime(Index).SetFocus
GoTo Err_Hand
End If
Else
Call MsgBox("请输入数字!", vbInformation, "警告!")
CboIntervalTime(Index).SetFocus
GoTo Err_Hand
End If

If Index < 2 Then
If IsNumeric(TxtWlPoint(Index).Text) Then
WlPoint = Val(TxtWlPoint(Index).Text)
If WlPoint < LowLimit Or WlPoint > UpLimit Then
If Index = 0 Then
Call MsgBox("激发波长不能小于" & LowLimit & "或者大于" & UpLimit, vbInformation, "警告!")
Else
Call MsgBox("发射波长不能小于" & LowLimit & "或者大于" & UpLimit, vbInformation, "警告!")
End If
TxtWlPoint(Index).SetFocus
GoTo Err_Hand
End If
Else
Call MsgBox("请输入数字!", vbInformation, "警告!")
TxtWlPoint(Index).SetFocus
GoTo Err_Hand
End If
End If

IsInputOk = True
Exit Function

Err_Hand:
IsInputOk = False

End Function


2006-05-26 12:58
lly421
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2006-5-27
收藏
得分:0 

谁有关于VB控件的资料啊??麻烦共享一下!!

2006-05-27 01:15
快速回复:[讨论]请教如何能使下列程序代码变的更简洁一些?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017406 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved