?
Solved

VB.net drag text onto an Excel cell

Posted on 2012-04-02
4
Medium Priority
?
488 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 2000 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

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

764 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