Solved

VB.net drag text onto an Excel cell

Posted on 2012-04-02
4
470 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
Comment Utility
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
Comment Utility
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
Comment Utility
Excel will handle it as long as its plain text.
0
 

Author Closing Comment

by:murbro
Comment Utility
Thanks very much
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

728 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now