Solved

VB.net drag text onto an Excel cell

Posted on 2012-04-02
4
483 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:Murray Brown
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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:Murray Brown
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:Murray Brown
ID: 37803026
Thanks very much
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
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.

695 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