none
控件数组求教各位,谢谢!!![ RRS feed

  • 问题

  • 求教:我学习了网上一些方法,有一个问题不知怎么解决,求教!!!

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    For Each btn As Label In New Label() {Me.Label14, Me.Label15, Me.Label16, Me.Label17, Me.Label18, Me.Label19, Me.Label20, Me.Label21, Me.Label22, Me.Label23, Me.Label24, Me.Label25, Me.Label26, Me.Label27, Me.Label28, Me.Label29, Me.Label30, Me.Label31, Me.Label32, Me.Label33, Me.Label34, Me.Label35, Me.Label36, Me.Label37, Me.Label38, Me.Label39, Me.Label40, Me.Label41, Me.Label42, Me.Label43, Me.Label44, Me.Label45, Me.Label46, Me.Label47, Me.Label48, Me.Label154, Me.Label157, Me.Label158, Me.Label159, Me.Label160, Me.Label161, Me.Label162}

    AddHandler btn.MouseMove, AddressOf Me.labelClick Next Private Sub labelMouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)

    MsgBox(sender.ToString)'能取得鼠标移动到的标签值

    '在这里我想取得鼠标移动到的标签在标签数组中是第几个,如移动到Me.Label14,,就显示0,移动到Me.Label16,就显示2

    End Sub

    2010年10月1日 4:49

答案

  • 你好

    或者你可以嘗試改變你的CODE

        Private LabelArray As New List(Of Label)

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

      LabelArray.AddRange({Me.Label14, Me.Label15, Me.Label16, Me.Label17, Me.Label18, Me.Label19, Me.Label20, Me.Label21, Me.Label22, Me.Label23, Me.Label24, Me.Label25, Me.Label26, Me.Label27, Me.Label28, Me.Label29, Me.Label30, Me.Label31, Me.Label32, Me.Label33, Me.Label34, Me.Label35, Me.Label36, Me.Label37, Me.Label38, Me.Label39, Me.Label40, Me.Label41, Me.Label42, Me.Label43, Me.Label44, Me.Label45, Me.Label46, Me.Label47, Me.Label48, Me.Label154, Me.Label157, Me.Label158, Me.Label159, Me.Label160, Me.Label161, Me.Label162})

            For Each btn As Label In LabelArray
                AddHandler btn.MouseMove, AddressOf Me.labelClick
            Next

            'TextBox1.Text = Form2.Label1.Text
        End Sub

     Private Sub labelClick(ByVal sender As Object, ByVal e As MouseEventArgs)
            Dim l As Label = sender
            MsgBox(LabelArray.IndexOf(sender))

        End Sub

    這想便可以用到這個LABEL ARRAY 的 INDEX 了

    Please correct me if I mis-understood your questions


    Chi
    2010年10月2日 9:27