Solved

VB.net drag text onto an Excel cell

Posted on 2012-04-02
4
477 Views
Last Modified: 2012-04-03
Hi

In my VB.net Excel add-in I am using the following code to drag the text of a TreeView node into a DataGridView cell by first automatically finding which cell the mouse is over. Can this code be adapted to drop the text into the cell that the mouse is over in the Active spreadsheet


    Private Sub DataGridView1_DragEnter(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles DataGridView1.DragEnter
        Try

            If e.Data.GetDataPresent("System.Windows.Forms.TreeNode", True) Then
                'TreeNode found allow move effect
                'e.Effect = DragDropEffects.Move
                e.Effect = DragDropEffects.All

            Else
                'No TreeNode found, prevent move
                e.Effect = DragDropEffects.None
                Exit Sub
            End If

        Catch ex As Exception

        End Try
    End Sub


    Private Sub DataGridView1_DragDrop(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles DataGridView1.DragDrop
        Try

            'Check that there is a TreeNode being dragged
            If e.Data.GetDataPresent("System.Windows.Forms.TreeNode", _
                  True) = False Then Exit Sub

            'Get the TreeNode being dragged
            Dim dropNode As TreeNode = _
                  CType(e.Data.GetData("System.Windows.Forms.TreeNode"),  _
                  TreeNode)
            'TextBox1.Text = dropNode.Text

            Dim p As System.Drawing.Point = Me.DataGridView1.PointToClient(New System.Drawing.Point(e.X, e.Y))
            Dim hitTest As DataGridView.HitTestInfo = DataGridView1.HitTest(p.X, p.Y)

            If hitTest.RowIndex <> -1 AndAlso hitTest.ColumnIndex <> -1 Then
                'IF THERE IS A HIT
                Dim cell As DataGridViewCell = Me.DataGridView1(hitTest.ColumnIndex, hitTest.RowIndex)
                'Dim oRowIndex As Integer = hitTest.RowIndex
                Dim oColumnIndex As Integer = hitTest.ColumnIndex
                'MsgBox(CStr(oColumnIndex))
                DataGridView1.Rows(0).Cells(oColumnIndex).Value = dropNode.Text
            End If


        Catch
            MsgBox("There was a problem with the drag and drop ")
        End Try
    End Sub
0
Comment
Question by:murbro
  • 2
  • 2
4 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37801974
Because Excel cell is beyond your control, you would need to put node text on the drag rather than the node itself as Excel wont be able to understand node.
0
 

Author Comment

by:murbro
ID: 37801990
How would I handle the drop event onto the spreadsheet. Do I add a handler?
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 37802035
Excel will handle it as long as its plain text.
0
 

Author Closing Comment

by:murbro
ID: 37803026
Thanks very much
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question