none
關於眾多相同類型之物件重覆的程式碼輸入方式 RRS feed

  • 問題

  • 我自己在寫程式時遇到需要寫這樣一段:

      Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        TextBox1.ForeColor = Color.Black
      End Sub
      Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
        TextBox2.ForeColor = Color.Black
      End Sub
      Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged
        TextBox3.ForeColor = Color.Black
      End Sub
      Private Sub TextBox4_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox4.TextChanged
        TextBox4.ForeColor = Color.Black
      End Sub
      Private Sub TextBox5_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox5.TextChanged
        TextBox5.ForeColor = Color.Black
      End Sub
      Private Sub TextBox6_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox6.TextChanged
        TextBox6.ForeColor = Color.Black
      End Sub
      Private Sub TextBox7_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox7.TextChanged
        TextBox7.ForeColor = Color.Black
      End Sub
      Private Sub TextBox8_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox8.TextChanged
        TextBox8.ForeColor = Color.Black
      End Sub
      Private Sub TextBox9_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox9.TextChanged
        TextBox9.ForeColor = Color.Black
      End Sub
      Private Sub TextBox10_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox10.TextChanged
        TextBox10.ForeColor = Color.Black
      End Sub
      Private Sub TextBox11_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox11.TextChanged
        TextBox11.ForeColor = Color.Black
      End Sub
      Private Sub TextBox12_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox12.TextChanged
        TextBox12.ForeColor = Color.Black
      End Sub
      Private Sub TextBox13_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox13.TextChanged
        TextBox13.ForeColor = Color.Black
      End Sub
      Private Sub TextBox14_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox14.TextChanged
        TextBox14.ForeColor = Color.Black
      End Sub
      Private Sub TextBox15_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox15.TextChanged
        TextBox15.ForeColor = Color.Black
      End Sub
      Private Sub TextBox16_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox16.TextChanged
        TextBox16.ForeColor = Color.Black
      End Sub
      Private Sub TextBox17_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox17.TextChanged
        TextBox17.ForeColor = Color.Black
      End Sub
      Private Sub TextBox18_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox18.TextChanged
        TextBox18.ForeColor = Color.Black
      End Sub
      Private Sub TextBox19_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox19.TextChanged
        TextBox19.ForeColor = Color.Black
      End Sub
      Private Sub TextBox20_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox20.TextChanged
        TextBox20.ForeColor = Color.Black
      End Sub
      Private Sub TextBox21_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox21.TextChanged
        TextBox21.ForeColor = Color.Black
      End Sub
      Private Sub TextBox22_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox22.TextChanged
        TextBox22.ForeColor = Color.Black
      End Sub
      Private Sub TextBox23_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox23.TextChanged
        TextBox23.ForeColor = Color.Black
      End Sub
      Private Sub TextBox24_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox24.TextChanged
        TextBox24.ForeColor = Color.Black
      End Sub
      Private Sub TextBox25_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox25.TextChanged
        TextBox25.ForeColor = Color.Black
      End Sub
      Private Sub TextBox26_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox26.TextChanged
        TextBox26.ForeColor = Color.Black
      End Sub
      Private Sub TextBox27_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox27.TextChanged
        TextBox27.ForeColor = Color.Black
      End Sub
      Private Sub TextBox28_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox28.TextChanged
        TextBox28.ForeColor = Color.Black
      End Sub
      Private Sub TextBox29_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox29.TextChanged
        TextBox29.ForeColor = Color.Black
      End Sub
      Private Sub TextBox30_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox30.TextChanged
        TextBox30.ForeColor = Color.Black
      End Sub
      Private Sub TextBox31_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox31.TextChanged
        TextBox31.ForeColor = Color.Black
      End Sub
      Private Sub TextBox32_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox32.TextChanged
        TextBox32.ForeColor = Color.Black
      End Sub
      Private Sub TextBox33_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox33.TextChanged
        TextBox33.ForeColor = Color.Black
      End Sub
      Private Sub TextBox34_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox34.TextChanged
        TextBox34.ForeColor = Color.Black
      End Sub
      Private Sub TextBox35_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox35.TextChanged
        TextBox35.ForeColor = Color.Black
      End Sub
      Private Sub TextBox36_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox36.TextChanged
        TextBox36.ForeColor = Color.Black
      End Sub
      Private Sub TextBox37_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox37.TextChanged
        TextBox37.ForeColor = Color.Black
      End Sub
      Private Sub TextBox38_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox38.TextChanged
        TextBox38.ForeColor = Color.Black
      End Sub
      Private Sub TextBox39_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox39.TextChanged
        TextBox39.ForeColor = Color.Black
      End Sub
      Private Sub TextBox40_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox40.TextChanged
        TextBox40.ForeColor = Color.Black
      End Sub
      Private Sub TextBox41_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox41.TextChanged
        TextBox41.ForeColor = Color.Black
      End Sub
      Private Sub TextBox42_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox42.TextChanged
        TextBox42.ForeColor = Color.Black
      End Sub
      Private Sub TextBox43_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox43.TextChanged
        TextBox43.ForeColor = Color.Black
      End Sub
      Private Sub TextBox44_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox44.TextChanged
        TextBox44.ForeColor = Color.Black
      End Sub
      Private Sub TextBox45_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox45.TextChanged
        TextBox45.ForeColor = Color.Black
      End Sub
      Private Sub TextBox46_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox46.TextChanged
        TextBox46.ForeColor = Color.Black
      End Sub
      Private Sub TextBox47_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox47.TextChanged
        TextBox47.ForeColor = Color.Black
      End Sub
      Private Sub TextBox48_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox48.TextChanged
        TextBox48.ForeColor = Color.Black
      End Sub
      Private Sub TextBox49_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox49.TextChanged
        TextBox49.ForeColor = Color.Black
      End Sub
      Private Sub TextBox50_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox50.TextChanged
        TextBox50.ForeColor = Color.Black
      End Sub
    

     難道沒有其他輸入的方法嗎?(我最後是用excel協助)

    謝謝各位回答

    2011年3月29日 下午 07:47

解答

  • 讓所有相同處理方式的TextBox都指向同一個事件委派

    然後 Sender代表的就是發生事件的物件, 這有幾種不同的方式

    (1)

      Private Sub TextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged
            CType(sender, TextBox).ForeColor = Color.Black
        End Sub

     

    (2)

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            AddHandler TextBox1.TextChanged, AddressOf TextBox_TextChanged
            AddHandler TextBox2.TextChanged, AddressOf TextBox_TextChanged

        End Sub

    Private Sub TextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
            CType(sender, TextBox).ForeColor = Color.Black
        End Sub

    PS: 這個方式在粗體字的部份可以使用迴圈做:

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            For Each con In Me.Controls
                If con.GetType() = GetType(TextBox) Then
                    AddHandler CType(con, TextBox).TextChanged, AddressOf TextBox_TextChanged
                End If
            Next
        End Sub

     

    (3) 以上的重點資訊

    (a) WithEvents 和 Handles 子句

    (b) AddHandler 陳述式


    在現實生活中,你和誰在一起的確很重要,甚至能改變你的成長軌跡,決定你的人生成敗。 和什麼樣的人在一起,就會有什麼樣的人生。 和勤奮的人在一起,你不會懶惰; 和積極的人在一起,你不會消沈; 與智者同行,你會不同凡響; 與高人為伍,你能登上巔峰。

    2011年3月30日 上午 12:34
    版主