0

我希望你能帮助我。提前谢谢了。

我的问题与捕获数据网格视图中的鼠标点击次数有关。我在 Visual Studio 2012 中使用 Visual Basic。

我正在构建一个大小为 10x10 的矩阵或数组,由 1 填充。我也有一个相同大小的datagridview。我的目标是在矩阵的位置放置一个零,该矩阵具有与在 datagridview 中使用鼠标执行的单击相同的坐标“rowindex”和“columnindex”。到目前为止,我已经成功了。

但我想更进一步:我希望在 1 的矩阵中拥有与单击 datagridview 一样多的零。我的问题是我只能有一个零,最后一个。

如果我在 datagridview(1,1)、datagridview(2,2) 和 datagridview(3,3) 中执行点击,我最后只有一个零 (3,3)

我无法记录点击次数。

这里有我的代码:

      Private Sub ClickMouse(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView.CellMouseClick
    MsgBox(e.Clicks & e.ColumnIndex & e.RowIndex)
    Dim matrix As Integer(,) = PopulateMatrix()

    matrix(e.ColumnIndex, e.RowIndex) = 0

    Matrixtomatrixdef(matrix)
End Sub
Private Function PopulateMatrix() As Integer(,)  
    Dim matrix(10, 10) As Integer
    For rown = 0 To 9
        For columnn = 0 To 9
            matrix(columnn, rown) = 1
        Next
    Next
    Return matrix
End Function
Private Sub Matrixtomatrixdef(matrix As Integer(,))     
    Dim Matrixdef(10, 10) As Integer
    For rown = 0 To 9
        For columnn = 0 To 9
            Matrixdef(columnn, rown) = matrix(columnn, rown)
            Debug.Write(Matrixdef(columnn, rown).ToString & " ")
        Next
        Debug.WriteLine("")
    Next
End Sub
4

1 回答 1

0

您应该将矩阵定义并填充为全局变量,而不是在 ClickMouse 事件处理程序内(否则每次单击都会删除您在前一个设置中设置的零,只留下最后一个)

Public Class Form1
    Private matrix As Integer(,) = PopulateMatrix()

    Private Sub ClickMouse(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView.CellMouseClick
        MsgBox(e.Clicks & e.ColumnIndex & e.RowIndex)

        matrix(e.ColumnIndex, e.RowIndex) = 0

        Matrixtomatrixdef(matrix)
    End Sub

    Private Function PopulateMatrix() As Integer(,)  
        Dim matrix(10, 10) As Integer
        For rown = 0 To 9
            For columnn = 0 To 9
                matrix(columnn, rown) = 1
            Next
        Next
        Return matrix
    End Function

    Private Sub Matrixtomatrixdef(matrix As Integer(,))     
        Dim Matrixdef(10, 10) As Integer
        For rown = 0 To 9
            For columnn = 0 To 9
                Matrixdef(columnn, rown) = matrix(columnn, rown)
                Debug.Write(Matrixdef(columnn, rown).ToString & " ")
            Next
            Debug.WriteLine("")
        Next
    End Sub
End Class   
于 2013-05-19T22:57:51.903 回答