看板 Programming 關於我們 聯絡資訊
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 ──┘   唱片公司授權,音樂盡情下載 --
wahaha99:好接近自然語言的寫法!!XD 第一次看到VB 218.166.59.135 11/26 20:46
wahaha99:可以這樣寫@@ 218.166.59.135 11/26 20:46