【问题标题】:PictureBox array index or tag referencePictureBox 数组索引或标记引用
【发布时间】:2013-04-03 00:14:33
【问题描述】:

需要知道 Visual Basic 2010 中单击的 PictureBox 数组元素的索引和标记。 TagIndex 作为公共整数变量变暗。 Gbox 是 PictureBox 数组的名称。 Tag = sender.tag 有效。 Index = NameofArray.Indexof(sender as PictureBox) 不起作用 Index = clicked.Indexof(sender as PictureBox)

    Private Sub Gbox_Click(sender As Object, e As EventArgs)
    Dim clicked As PictureBox = CType(sender, PictureBox)
    SelectedColor.Image = clicked.Image
    Tag = sender.tag
    Index = Gbox.IndexOf(sender as picturebox)  'Does not work
    Index = clicked.Indexof(sender as PictureBox)  ' Does not work        
    End Sub

我需要在计算中使用预先分配的标签和索引。感谢您的帮助。

【问题讨论】:

    标签: vb.net visual-studio-2010


    【解决方案1】:

    这将返回它的索引,试试这个

    Index = Array.IndexOf(Gbox, sender)
    

    [ 注意:上面的代码将返回object中的first occurance在那个array(从它的lower bound - 0)。顺便说一句,如果searched object 不存在于那个特定的array 中,它将返回-1 ]

    【讨论】:

    • 非常感谢 RajaPrabhu。您的解决方案很简单并且有效。我会将我的问题标记为已回答。
    【解决方案2】:

    最好的答案来自 RajaPrabhu,我要感谢他。他的解决方案很简单,解决了几个问题。

        Index = Array.IndexOf(Gbox, sender)
    

    我确实为这个特定程序找到了另一种解决方案,但是感谢 RajaPrabhu,我将能够在其他几个实例中实施他的解决方案。

    这是我找到的解决方案: 部分问题是用户可能会改变主意并做出不同的选择,从而覆盖选择的标签属性。通过使用选择的标签属性以及更大数组中放置的标签属性来解决。较大的数组标记属性被分配了一个等于创建循环增量的标记,这也是索引。

    这得到了所选项目的标签:

        Private Sub SC_Click(sender As Object, e As EventArgs)
           Dim clicked As PictureBox = CType(sender, PictureBox)
           SelectedColor.Image = clicked.Image
           Tag = sender.tag
        End Sub
    

    然后这得到了放置数组的标记,即索引:

        Private Sub Gbox_Click(sender As Object, e As EventArgs)
           Dim clicked As PictureBox = CType(sender, PictureBox)
           clicked.Image = SelectedColor.Image
           Index = sender.tag
        End Sub
    

    在这里测试并使用了输出:

        Private Sub Guess_Click(sender As System.Object, e As System.EventArgs) Handles Guess.Click
           Dim PositionInRow As Integer
           TextBox1.Text = "Tag: " + Tag.ToString + ", Index: " + Index.ToString
           'MessageBox.Show(index.ToString)
           Row = Row + 1
           PositionInRow = Index Mod 5 + 1 'there are 13 rows of 5 positions
           'more code.......
        End Sub
    

    感谢您抽出宝贵时间进行审核。这是一个很棒的网站,充满了非常有用的信息和乐于助人的同事。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-25
      • 2018-01-22
      • 2023-03-30
      • 1970-01-01
      相关资源
      最近更新 更多