回文章列表
作者aling.bbs@bbs.ntit.edu.tw (..............), 信區: sources
標題Re: =?big5?Q?=C0=B0=A7=DA=AC=DD=A7=DA=BF=F9=A6b=AD=FE?=
時間NTITBBS (Fri Sep 24 01:32:42 2004)
轉信站: GIBBS!news2.ncku!ctu-gate!ctu-reader!news.nctu!NTITBBS
Origin: bbs.ntit.edu.tw

※ 引述《a9547510.bbs@bbs.kimo.com.tw (007)》之銘言:
> 以下是我的算利息的程式,請解釋我錯在哪裡,好嗎?
> Private Sub btnend_Click()
> End
> End Sub
> Private Sub txtcapital_KeyPress(KeyAscii As Integer)      '只能輸入數字
>     If KeyAscii <> 8 And KeyAscii <> 13 And KeyAscii <> 9 And KeyAscii <> 27
> Then
>         Select Case Chr(KeyAscii)
>             Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
>             Case Else
>                 KeyAscii = 0
>         End Select
>     End If
> End Sub
> Private Sub txtinterest_Change()
> txtinterest.Text = ""
> MsgBox "別玩了,這裡不能輸入", 16
> End Sub
> Private Sub txtrate_KeyPress(KeyAscii As Integer)
>     If KeyAscii <> 8 And KeyAscii <> 13 And KeyAscii <> 9 And KeyAscii <> 27
> Then
>         Select Case Chr(KeyAscii)
>             Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
>             Case Else
>                 KeyAscii = 0
>         End Select
>     End If
>   If txtrate.Text < 0 Or txtrate.Text > 100 Then
>   MsgBox "年利率輸入錯誤", 16
>   txtrate.Text = ""
>   End If
> End Sub
> Private Sub txtyear_KeyPress(KeyAscii As Integer)
>     If KeyAscii <> 8 And KeyAscii <> 13 And KeyAscii <> 9 And KeyAscii <> 27
> Then
>         Select Case Chr(KeyAscii)
>             Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
>             Case Else
>                 KeyAscii = 0
>         End Select
>     End If
> End Sub
> Private Sub btnrun_Click()
> If txtcapital.Text = "" Then '沒褕入
> MsgBox "請輸入本金", 0 + 16, "Error"
> End If
> If txtrate.Text = "" Then
> MsgBox "請輸入年利率", 0 + 16, "Error"
> End If
> If txtyear.Text = "" Then
> MsgBox "請輸入年數", 0 + 16, "Error"
> End If
> '本金是txtcapital
> '利率是txtrate
> '年數是txtyear
> If txtcapital.Text <> "" & txtrate.Text <> "" & txtyear.Text <> "" Then
> txtinterest.Text = txtcapital.Text * ((1 + (txtrate.Text / 100)) ^ txtyear.Text
> )
> End If
> End Sub
> Private Sub Command1_Click()  '重新計算
> txtcapital.Text = ""
> txtrate.Text = ""
> txtyear.Text = ""
> txtinterest = ""
> End Sub



若是你們老師要求你們用KeyPress來做的話,那沒話講

不然我是覺得你把它複雜化了

你應該要把函數和事件讀熟一點

像是chang事件

chang事件只要是程式或使用者去改到內容便會執行一次

還有檢查是否為數字,事實上你可以用IsNumeric來檢查是否為數字

我個人推薦啦你可以用LostFocus或是Validate事件來寫會方便許多

如果你用LostFocus 那檢查可以這樣寫

Private Sub Text1_LostFocus()
If Not IsNumeric(Text1.Text) Or Text1.Text = "" Then
MsgBox "請輸入數字,否則請按取消離開", vbOKCancel, "MSGBOX"
If vbOK = 1 Then
Text1.SetFocus
ElseIf vbCancel = 1 Then
End
End If
End If
End Sub

而Validate事件你可以這樣寫

Private Sub Command1_Click()
'本金是text1
'利率是text2
'年數是text3
Label4.Caption = Val(Text1.Text) * ((1 + (Val(Text2.Text) / 100)) ^ Val(Text3.Text))
End Sub

Private Sub Command2_Click()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Label4.Caption = 0
End Sub

Private Sub Text1_Validate(Cancel As Boolean)
If Not IsNumeric(Text1.Text) Or Text1.Text = "" Or Val(Text1.Text) <= 0 Then
MsgBox "請輸入數字並大於零!!"
    Cancel = True
End If
End Sub

Private Sub Text2_Validate(Cancel As Boolean)
If Not IsNumeric(Text2.Text) Or Text2.Text = "" Or Val(Text2.Text) <= 0 Then
MsgBox "請輸入年利率並大於零"
    Cancel = True
End If
End Sub


Private Sub Text3_Validate(Cancel As Boolean)
If Not IsNumeric(Text3.Text) Or Text3.Text = "" Or Val(Text3.Text) <= 0 Then
    MsgBox "請輸入年數並大於零"
    Cancel = True
End If
--
※ Origin: 碧海藍天 <bbs.ntit.edu.tw> 
◆ From: ppp-218-32-112-77.tc.sparqnet.net
文章選讀←離開[主題上]主題下(k)上篇(j)下篇S/a搜尋 G串列 TAB精華 ↑↓捲 Pg/Space翻