請教..vb.net的一段語法
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim a, b, temp, i, j As Integer
For Each ctl As Control In Controls
If TypeOf ctl Is TextBox Then
If Not IsNumeric(ctl.Text) Then
MessageBox.Show("請輸入整數!")
ctl.Focus()
CType(ctl, TextBox).SelectAll()
Exit Sub
End If
End If
Next
' If IsNumeric(TextBox1.Text) = False Then
' MsgBox("請輸入整數")
' TextBox1.Focus()
' TextBox1.SelectAll()
' Exit Sub
' End If
'如果有很多個textbox 可否有快速的語法做每一個textbox檢查呢?
' If IsNumeric(TextBox2.Text) = False Then
' MsgBox("請輸入整數")
' TextBox2.Focus()
' TextBox2.SelectAll()
' Exit Sub
' End If
a = Val(TextBox1.Text)
b = Val(TextBox2.Text)
If a > b Then 'a=5 b=2
temp = a 'temp=5 a=5 b=2
a = b 'temp=5 a=2 b=2
b = temp 'temp=5 b=5 a=2
End If
If a <= 0 Or b <= 0 Then '左右兩邊的條件有一個成立就可以
MsgBox("請輸入大於0的整數")
Exit Sub
End If
For i = a To b
If i Mod 2 = 0 Then j = j + 1 '當i除以2的餘數為0時 j就加一
Next
MsgBox("偶數共有" & j & "個")
End Sub
以上的「For Each ctl As Control In Controls」 是用來取代下面的那兩個
textbox的偵測
不過 我不瞭解的地方也就在這裡
請教..以下幾個關鍵字分別代表什麼呢?
1.for each
2.as Control
3. in control
謝謝
--
┌─────◆KKCITY◆─────┐ ◢ ◤ 找歌最方便 KKBOX 歌詞搜尋!!
│ bbs.kkcity.com.tw │ \^_^ / ★http://www.kkbox.com.tw★
└──《From:218.169.123.248 》──┘ ◤ 唱片公司授權,音樂盡情下載
--
推
11/26 20:46, , 1F
11/26 20:46, 1F
→
11/26 20:46, , 2F
11/26 20:46, 2F
討論串 (同標題文章)
Programming 近期熱門文章
PTT數位生活區 即時熱門文章