0

私は自分が設定したことを達成するために苦労しています todo! 一言で言えば、Google ドライブ API を使用して、VB.net でファイルの表示/ダウンロードなどを可能にするアプリを作成しています。

基本的に、私が持っているさまざまなクラウド プロバイダーに対して、いくつかの異なる API を使用することを計画しています。私がたどり着いた段階は、ファイルのコレクションがあり、リストにさまざまなプロパティがあるということです。ロード時に、Googleアカウントがプログラムに追加されているかどうかを確認しています。追加されている場合は、タブコントロールに新しいタブページを作成し、データグリッドビューを作成して新しいタブページのコレクションに追加します. これは正常に機能し、すべてのデータがそのまま表示されます。

私が達成しようとしているのは、URL を表示する代わりに、クリック可能なリンクをダウンロード列に追加することです。vb.net に変換した後、C# DataGridViewLinkCell Displayで見つかったコードを操作しようとしています。これは基本的に私が最終的に得たものです:

If My.Settings.GoogleClientID <> "" Then
            Dim GD As New Properties.GDrive
            GD.APIClientID = My.Settings.GoogleClientID
            GD.APIClientSecret = My.Settings.GoogleClientSecret

            clsDrive.GD = GD
            clsDrive.GetSpace()
            clsDrive.RefreshFiles()
            Dim GoogleDriveTab As New TabPage
            GoogleDriveTab.Text = "Google Drive"
            tc_CloudManager.TabPages.Add(GoogleDriveTab)

            Dim GoogleDriveDGV As New DataGridView
            GoogleDriveDGV.Name = "GoogleDriveDGV"
            GoogleDriveDGV.Dock = DockStyle.Fill
            GoogleDriveDGV.Columns.Add("FileTitle", "File Title")
            GoogleDriveDGV.Columns.Add("FileExtension", "File Extension")
            GoogleDriveDGV.Columns.Add("FileSize", "File Size")
            Dim dgvlc As New DataGridViewLinkColumn()
            dgvlc.ActiveLinkColor = Color.Blue
            dgvlc.HeaderText = "Download"
            GoogleDriveDGV.Columns.Add(dgvlc)
            GoogleDriveDGV.RowHeadersVisible = False
            Dim c As New customcolumn()
            GoogleDriveDGV.Columns.Add(c)

            For i As Integer = 0 To GoogleDriveDGV.Columns.Count - 1
                GoogleDriveDGV.Columns(i).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
            Next
            Try
                For Each DriveFile In GD.DriveFiles
                    Dim row As DataGridViewRow = DirectCast(GoogleDriveDGV.Rows(0).Clone, DataGridViewRow)
                    Dim title As String = DriveFile.Title
                    If title.Length > 60 Then
                        title = title.Substring(0, 60)
                    End If

                    Dim fs As Integer = DriveFile.FileSize
                    Dim fsf As String
                    If fs > 1048576 Then
                        fsf = Math.Round(fs / 1048576, 2) & " MB"
                    Else
                        fsf = fs & " Bytes"
                    End If
                    Dim fe As String
                    If DriveFile.FileExtension = "" Then
                        fe = "Folder"
                    Else
                        fe = DriveFile.FileExtension
                    End If

                    row.Cells(0).Value = title
                    row.Cells(1).Value = fe
                    row.Cells(2).Value = fsf
                    row.Cells(3).Value = "Download File"
                    DirectCast(GoogleDriveDGV.Columns(3), customcolumn).urls.Add(3, DriveFile.DownloadUrl)
                Next
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try

            GoogleDriveTab.Controls.Add(GoogleDriveDGV)
        End If
    End Sub
    Class customcolumn
        Inherits System.Windows.Forms.DataGridViewLinkColumn
        Public urls As New Dictionary(Of Integer, String)()
    End Class

    Private Sub GoogleDriveDGV_CellContentClick(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs)
        For Each url As KeyValuePair(Of Integer, String) In DirectCast(GoogleDriveDGV.Columns(e.ColumnIndex), customcolumn).urls
            If url.Key = e.RowIndex Then
                Process.Start(url.Value)
                Exit For
            End If
        Next
    End Sub

私が理解できない2つの問題があります:

1) GoogleDriveDGV はここでは宣言されていません: For Each url As KeyValuePair(Of Integer, String) In DirectCast(GoogleDriveDGV.Columns(e.ColumnIndex), customcolumn).urls

2) GoogleDriveDGV_CellContentClick サブルーチンをコメント アウトして、データグリッド ビューに入力しようとすると、 System.InvalidCastException: Unable to cast object of type 'System.Windows.Forms.DataGridViewLinkColumn' to type 'customcolumn' here DirectCast(GoogleDriveDGV.Columns(3) が発生します。 ), customcolumn).urls.Add(3, DriveFile.DownloadUrl)

私が達成しようとしていることが可能かどうか、また何かヒントがあれば教えてもらえますか?

アップデート!!!

私は今これを持っています:

If My.Settings.GoogleClientID <> "" Then
            Dim GD As New Properties.GDrive
            GD.APIClientID = My.Settings.GoogleClientID
            GD.APIClientSecret = My.Settings.GoogleClientSecret

            clsDrive.GD = GD
            clsDrive.GetSpace()
            clsDrive.RefreshFiles()


            Dim GoogleDriveTab As New TabPage
            GoogleDriveTab.Text = "Google Drive"
            tc_CloudManager.TabPages.Add(GoogleDriveTab)

            Dim GoogleDriveDGV As New DataGridView
            GoogleDriveDGV.Name = "GoogleDriveDGV"
            GoogleDriveDGV.Dock = DockStyle.Fill
            GoogleDriveDGV.Columns.Add("FileTitle", "File Title")
            GoogleDriveDGV.Columns.Add("FileExtension", "File Extension")
            GoogleDriveDGV.Columns.Add("FileSize", "File Size")
            Dim c As New customcolumn()
            GoogleDriveDGV.Columns.Add(c)
            GoogleDriveDGV.RowHeadersVisible = False

            For i As Integer = 0 To GoogleDriveDGV.Columns.Count - 1
                GoogleDriveDGV.Columns(i).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
            Next
            Dim trow As Integer = 0
            Try
                For Each DriveFile In GD.DriveFiles
                    Dim row As DataGridViewRow = DirectCast(GoogleDriveDGV.Rows(0).Clone, DataGridViewRow)
                    Dim title As String = DriveFile.Title
                    If title.Length > 60 Then
                        title = title.Substring(0, 60)
                    End If

                    Dim fs As Integer = DriveFile.FileSize
                    Dim fsf As String
                    If fs > 1048576 Then
                        fsf = Math.Round(fs / 1048576, 2) & " MB"
                    Else
                        fsf = fs & " Bytes"
                    End If
                    Dim fe As String
                    If DriveFile.FileExtension = "" Then
                        fe = "Folder"
                    Else
                        fe = DriveFile.FileExtension
                    End If

                    row.Cells(0).Value = title
                    row.Cells(1).Value = fe
                    row.Cells(2).Value = fsf
                    row.Cells(3).Value = "Download"

                    DirectCast(GoogleDriveDGV.Columns(3), customcolumn).urls.Add(trow, DriveFile.DownloadUrl)
                    GoogleDriveDGV.Rows.Add(row)
                    trow = trow + 1

                Next
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
            GoogleDriveTab.Controls.Add(GoogleDriveDGV)
        End If
    End Sub


    Class customcolumn
        Inherits System.Windows.Forms.DataGridViewLinkColumn
        Public urls As New Dictionary(Of Integer, String)()
    End Class


    'Private Sub GoogleDriveDGV_CellContentClick(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs)
    '    For Each url As KeyValuePair(Of Integer, String) In DirectCast(GoogleDriveDGV.Columns(e.ColumnIndex), customcolumn).urls
    '        If url.Key = e.RowIndex Then
    '            Process.Start(url.Value)
    '            Exit For
    '        End If
    '    Next
    'End Sub

ほとんどそこにあります。アイテムとダウンロードリンクで満たされたdatagridviewがありますが、上記の問題1を解決する方法がわかりません:-(

4

1 に答える 1