none
Select Case 控件出错 RRS feed

  • 问题

  • 两个label点一下,变一下颜色,所对应的数字01互换,程序中,颜色改变正确,但数字互换出错

     

    label1对应数字lab(1),label2对应lab(2)

    label所对应的lan(2)永远是0,添加多个label后也只有label1对应的数字会变,所有的数字改变都改在了label1 上,如点两下label2,label1对应的数字从0变1,再变成0

     

    有问题的程序

    设计页手工添加textbox1和button1用于测试

    Public Class Form1
        Private WithEvents label1 As New System.Windows.Forms.Label
        Private WithEvents label2 As New System.Windows.Forms.Label
        Private lab(2) As Integer
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim Label1 As New Label, Label2 As New Label
            Label1.Text = "1"
            Label1.Size = New Size(27, 27)
            Label1.Location = New Point(0, 0)
            Label1.BackColor = Color.White
            Label2.Text = "2"
            Label2.Size = New Size(27, 27)
            Label2.Location = New Point(27, 0)
            Label2.BackColor = Color.White
            Me.Controls.Add(Label1)
            AddHandler Label1.Click, AddressOf Label_labelClick
            Me.Controls.Add(Label2)
            AddHandler Label2.Click, AddressOf Label_labelClick
        End Sub
        Private Sub Label_labelClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles label1.Click, label2.Click
            Dim label1 As Label, label2 As Label
            label1 = CType(sender, Label)
            label2 = CType(sender, Label)
            Select Case DirectCast(sender, Label).Name
                Case label1.Name
                    If lab(1) = 0 Then
                        lab(1) = 1
                        label1.BackColor = Color.Black
                    Else
                        lab(1) = 0
                        label1.BackColor = Color.White
                    End If
                Case label2.Name
                    If lab(2) = 0 Then
                        lab(2) = 1
                        label2.BackColor = Color.Black
                    Else
                        lab(2) = 0
                        label2.BackColor = Color.White
                    End If
            End Select
        End Sub
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            TextBox1.Text = ""
            TextBox1.Text = lab(1) & lab(2)
        End Sub
    End Class

    ××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××

    修改如下:结果单击label无任何反应

    Public Class Form1
        Private WithEvents label1 As New System.Windows.Forms.Label
        Private WithEvents label2 As New System.Windows.Forms.Label
        Private lab(2) As Integer
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim Label1 As New Label, Label2 As New Label
            Label1.Text = "1"
            Label1.Size = New Size(27, 27)
            Label1.Location = New Point(0, 0)
            Label1.BackColor = Color.White
            Label2.Text = "2"
            Label2.Size = New Size(27, 27)
            Label2.Location = New Point(27, 0)
            Label2.BackColor = Color.White
            Label1.Name = "l1"
            Me.Controls.Add(Label1)
            AddHandler Label1.Click, AddressOf Label_labelClick
            Label2.Name = "l2"
            Me.Controls.Add(Label2)
            AddHandler Label2.Click, AddressOf Label_labelClick
        End Sub
        Private Sub Label_labelClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles label1.Click, label2.Click
            Dim lbl As Label
            lbl = CType(sender, Label)
            Select Case lbl.Name
                Case "l1"
                    If lab(1) = 0 Then
                        lab(1) = 1
                        label1.BackColor = Color.Black
                    Else
                        lab(1) = 0
                        label1.BackColor = Color.White
                    End If
                Case "l2"
                    If lab(2) = 0 Then
                        lab(2) = 1
                        label2.BackColor = Color.Black
                    Else
                        lab(2) = 0
                        label2.BackColor = Color.White
                    End If
            End Select
        End Sub
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            TextBox1.Text = ""
            TextBox1.Text = lab(1) & lab(2)
        End Sub
    End Class

     

    ******************************************************

     Select Case 中写有如下代码

                   If  lab(1) = 0 Then
                        lab(1) = 1
                        label1.BackColor = Color.Black
                    Else
                        lab(1) = 0
                        label1.BackColor = Color.White
                    End If

    这里

                   If  lab(1) = 0 Then
                        label1.BackColor = Color.Black
                    Else
                        label1.BackColor = Color.White
                    End If

    是用于改变label1的颜色

                   If  lab(1) = 0 Then
                        lab(1) = 1
                    Else
                        lab(1) = 0
                    End If

    是用于改变label1所对应的数组

    然后,我在上面写道

    label1对应数字lab(1),label2对应lab(2)

    至于你如何知道数字改变了没,我在上面也写道

    设计页手工添加textbox1和button1用于测试

    你按一下button1就输出label所对应的数字

    我想,你不至于连   

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            TextBox1.Text = ""
            TextBox1.Text = lab(1) & lab(2)
        End Sub

    也看不懂

    最后,你复制我如下的问题,

    label1对应数字lab(1),label2对应lab(2)

    label所对应的lan(2)永远是0,添加多个label后也只有label1对应的数字会变,所有的数字改变都改在了label1 上,如点两下label2,label1对应的数字从0变1,再变成0

    我想,你可能我的第一句话 "两个label点一下,变一下颜色,所对应的数字01互换,程序中,颜色改变正确,但数字互换出错"这句话没弄懂:

    两个label点一下,变一下颜色,你在代码中应该可以看到是黑白两色的互换,而所对应的数字01互换就是,原来是0,点一下就变为1,原来是1点一下就变成0,

    我在源程序中,颜色是点一下变一下的,也就是点一下白的变黑黑的变白,但数字的互换却产生问题,

    再把你复制的话展开一下:在label成功变换颜色的同时,他们所对应的数组(lab())的变化却产生了意想不到的问题,label2单击时,他的颜色的确在改变,但数字却永远是0,是不会变的,本来是应该点一下变一下的,你再单击button1时可以在textbox1中观察到这样的结果,另一个有趣的现象是,label2单击时,本不应该发生变化的label1所对应的lab(1)的值却在改变着,也就是,label1、label2单击在背景颜色的改变上,处理是正确的,在数组值的改变上是错误的(他们都只对lab(1)进行处理)。我的提问目的是要让lab(2)也能发生变化

     

    2009年1月21日 2:44

答案

  •  

    试验了N久终于成功了

    Public Class Form1
        Private WithEvents label1 As New Label
        Private WithEvents label2 As New Label
        Private lab(2) As Integer
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim Label1 As New Label, Label2 As New Label
            Label1.Name = "1"
            Label1.Size = New Size(27, 27)
            Label1.Location = New Point(0, 0)
            Label1.BackColor = Color.White
            Label2.Name = "2"
            Label2.Size = New Size(27, 27)
            Label2.Location = New Point(27, 0)
            Label2.BackColor = Color.White
            GroupBox1.Controls.Add(Label1)
            GroupBox1.Controls.Add(Label2)
            AddHandler Label1.Click, AddressOf Label_labelClick
            AddHandler Label2.Click, AddressOf Label_labelClick
        End Sub
        Private Sub Label_labelClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles label1.Click, label2.Click
            Dim label1 As Label, label2 As Label
            label1 = CType(sender, Label)
            label2 = CType(sender, Label)
            Select Case CType(sender, Label).Name
                Case 1
                    If lab(1) = 0 Then
                        lab(1) = 1
                        label1.BackColor = Color.Black
                    Else
                        lab(1) = 0
                        label1.BackColor = Color.White
                    End If
                Case 2
                    If lab(2) = 0 Then
                        lab(2) = 1
                        label2.BackColor = Color.Black
                    Else
                        lab(2) = 0
                        label2.BackColor = Color.White
                    End If
            End Select
            TextBox1.Text = vbCrLf & lab(1) & lab(2)
        End Sub
    End Class



    • 已标记为答案 ww-air 2009年2月9日 2:41
    2009年2月9日 2:41

全部回复

  • 你的理解有问题,sender表示出发事件的控件

     

    Code Snippet

    Label1.Name="l1"

    Me.Controls.Add(Label1)

    Label2.Name="l2"

    Me.Controls.Add(Label2)

     

     



    Code Snippet

     Dim lbl As Label

           lbl = CType(sender, Label)
            Select Case lbl.Name
                Case "l1"

     

     

     

     

    • 取消答案标记 ww-air 2009年1月30日 7:21
    • 已标记为答案 ww-air 2009年2月8日 15:15
    • 取消答案标记 ww-air 2009年2月9日 2:41
    2009年1月21日 3:11
    版主
  • 你好,根据你的意思,如果点击了label2,label1的数字和背景也会发生改变,但是从你的代码里没有反映出这点。
    如果你的问题仍然没有解答,请具体说明点击label时候两个label的处理,如分别点击label1、label2后会怎么样。
    还有你的意思是说点击label1,那么label1的数字和背景会发生改变,那label2的呢。

     ww-air 写:

     

    label1对应数字lab(1),label2对应lab(2)

    label所对应的lan(2)永远是0,添加多个label后也只有label1对应的数字会变,所有的数字改变都改在了label1 上,如点两下label2,label1对应的数字从0变1,再变成0

    ............................................................
        Private Sub Label_labelClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles label1.Click, label2.Click
            Dim label1 As Label, label2 As Label
            label1 = CType(sender, Label)
            label2 = CType(sender, Label)
            Select Case DirectCast(sender, Label).Name
                Case label1.Name
                    If lab(1) = 0 Then
                        lab(1) = 1
                        label1.BackColor = Color.Black
                    Else
                        lab(1) = 0
                        label1.BackColor = Color.White
                    End If
                Case label2.Name
                    If lab(2) = 0 Then
                        lab(2) = 1
                        label2.BackColor = Color.Black
                    Else
                        lab(2) = 0
                        label2.BackColor = Color.White
                    End If
            End Select
        End Sub
     ...........................................................................
    End Class

    2009年1月23日 3:31
  •  

    试验了N久终于成功了

    Public Class Form1
        Private WithEvents label1 As New Label
        Private WithEvents label2 As New Label
        Private lab(2) As Integer
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim Label1 As New Label, Label2 As New Label
            Label1.Name = "1"
            Label1.Size = New Size(27, 27)
            Label1.Location = New Point(0, 0)
            Label1.BackColor = Color.White
            Label2.Name = "2"
            Label2.Size = New Size(27, 27)
            Label2.Location = New Point(27, 0)
            Label2.BackColor = Color.White
            GroupBox1.Controls.Add(Label1)
            GroupBox1.Controls.Add(Label2)
            AddHandler Label1.Click, AddressOf Label_labelClick
            AddHandler Label2.Click, AddressOf Label_labelClick
        End Sub
        Private Sub Label_labelClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles label1.Click, label2.Click
            Dim label1 As Label, label2 As Label
            label1 = CType(sender, Label)
            label2 = CType(sender, Label)
            Select Case CType(sender, Label).Name
                Case 1
                    If lab(1) = 0 Then
                        lab(1) = 1
                        label1.BackColor = Color.Black
                    Else
                        lab(1) = 0
                        label1.BackColor = Color.White
                    End If
                Case 2
                    If lab(2) = 0 Then
                        lab(2) = 1
                        label2.BackColor = Color.Black
                    Else
                        lab(2) = 0
                        label2.BackColor = Color.White
                    End If
            End Select
            TextBox1.Text = vbCrLf & lab(1) & lab(2)
        End Sub
    End Class



    • 已标记为答案 ww-air 2009年2月9日 2:41
    2009年2月9日 2:41