locked
Richtextbox drag and drop problems RRS feed

  • Question

  • I am dragging and dropping text all within a single richtextbox. Pretty easy, almost no code required by setting properties!  However, I can't set the drag/drop effect, copy or move, because I can't seem to get the keystate in:

    Private ctrlMask As Byte = 8
    ....
        Private Sub RichTextBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles RichTextBox1.DragEnter
            If (e.KeyState And ctrlMask) = 8 Then
                e.Effect = DragDropEffects.Move
            Else
                e.Effect = DragDropEffects.Copy
            End If
        End Sub

    When I look at the keystate it's always 1, independent of the control key state.

    Also, I'd like to set the cursor to the end of the dropped data, which I can get from:

    Me.RichTextBox1.SelectionStart = Me.RichTextBox1.GetCharIndexFromPosition(New Point(e.X, e.Y))

    I don't seem to be able to set the selection start at the end of the DrapDrop event.

    Any thoughts on either/both of my problems
    Friday, April 27, 2007 4:54 PM

Answers

  • vsone,

     

    1. Set the AllowDrop property of the RichTextBox control to true.

     

    2. Write code in the event handler of the DragEnter event. Use an if statement to ensure that the data being dragged is of an acceptable type (in this case, text). The DragEventArgs.Effect property can be set to any value of the DragDropEffects enumeration.

     

    Code Snippet

       

    Private Sub RichTextBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles RichTextBox1.DragEnter

            If (e.Data.GetDataPresent(DataFormats.Text)) Then

                e.Effect = DragDropEffects.Copy

            Else

                e.Effect = DragDropEffects.None

            End If

        End Sub

     

    3. Write code to handle the DragDrop event. Use the System.Windows.Forms.DataObject.GetData method to retrieve the data being dragged. In the example below, the code sets the Text property of the RichTextBox control equal to the data being dragged. If there is already text in the RichTextBox control, the dragged text is inserted at the insertion point.

     

    Code Snippet

        Private Sub RichTextBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles RichTextBox1.DragDrop

            Dim i As Int16

            Dim s As String

     

            ' Get start position to drop the text.

            i = RichTextBox1.SelectionStart

            s = RichTextBox1.Text.Substring(i)

            RichTextBox1.Text = RichTextBox1.Text.Substring(0, i)

     

            ' Drop the text on to the RichTextBox.

            RichTextBox1.Text = RichTextBox1.Text + _

               e.Data.GetData(DataFormats.Text).ToString()

            RichTextBox1.Text = RichTextBox1.Text + s

        End Sub

     

     

    You can also take a look at the following link with DragEventArgs Class

     

    http://msdn2.microsoft.com/en-us/library/system.windows.forms.drageventargs(VS.71).aspx

     

    Friday, May 4, 2007 2:57 AM

All replies

  • Why not try this

    Code Snippet

    Microsoft.VisualBasic.ChrW(Keys.Return)



    and replace 'Return' w/ whatever you are trying to compare to.
    Friday, April 27, 2007 9:43 PM
  • Thanks, but I think the problem is with keystate.
    Saturday, April 28, 2007 12:14 AM
  • vsone,

     

    1. Set the AllowDrop property of the RichTextBox control to true.

     

    2. Write code in the event handler of the DragEnter event. Use an if statement to ensure that the data being dragged is of an acceptable type (in this case, text). The DragEventArgs.Effect property can be set to any value of the DragDropEffects enumeration.

     

    Code Snippet

       

    Private Sub RichTextBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles RichTextBox1.DragEnter

            If (e.Data.GetDataPresent(DataFormats.Text)) Then

                e.Effect = DragDropEffects.Copy

            Else

                e.Effect = DragDropEffects.None

            End If

        End Sub

     

    3. Write code to handle the DragDrop event. Use the System.Windows.Forms.DataObject.GetData method to retrieve the data being dragged. In the example below, the code sets the Text property of the RichTextBox control equal to the data being dragged. If there is already text in the RichTextBox control, the dragged text is inserted at the insertion point.

     

    Code Snippet

        Private Sub RichTextBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles RichTextBox1.DragDrop

            Dim i As Int16

            Dim s As String

     

            ' Get start position to drop the text.

            i = RichTextBox1.SelectionStart

            s = RichTextBox1.Text.Substring(i)

            RichTextBox1.Text = RichTextBox1.Text.Substring(0, i)

     

            ' Drop the text on to the RichTextBox.

            RichTextBox1.Text = RichTextBox1.Text + _

               e.Data.GetData(DataFormats.Text).ToString()

            RichTextBox1.Text = RichTextBox1.Text + s

        End Sub

     

     

    You can also take a look at the following link with DragEventArgs Class

     

    http://msdn2.microsoft.com/en-us/library/system.windows.forms.drageventargs(VS.71).aspx

     

    Friday, May 4, 2007 2:57 AM
  • I am seeing the same thing as the original poster, i.e. KeyState is always 1 in the DragEnter handler.

    Also, drag & drop doesn't work the first time (I get the "circle-slash" cursor).

    Besides that, this code works fine.

    This is in C++, but you get the idea. (I had to introduce some spaces to supress "Emoticons").

    -------

    this->richTextBox1->DragDrop += gcnew System::Windows::Forms:: DragEventHandler(this, &richTextBox1_DragDrop);

    this->richTextBox1->DragEnter += gcnew System::Windows::Forms:: DragEventHandler(this, &richTextBox1_DragEnter);

    private: void richTextBox1_DragEnter( System:: Object^ sender, System::Windows::Forms:: DragEventArgs^ e )

    {

    if (!e->Data->GetDataPresent(DataFormats::Text))

       e->Effect = DragDropEffects::None;

    else

       if ( ((e->KeyState & 8) == 8) && ((e->AllowedEffect & DragDropEffects::Copy) == DragDropEffects::Copy) )

          // CTL KeyState for copy. *** never gets here - KeyState always 1 ***

          e->Effect = DragDropEffects::Copy;

       else

          if ((e->AllowedEffect & DragDropEffects::Move) == DragDropEffects::Move)

             e->Effect = DragDropEffects::Move;

    }

    private: void richTextBox1_DragDrop( System:: Object^ sender, System::Windows::Forms:: DragEventArgs^ e )

    {

       //...

    }

    Tuesday, May 8, 2007 9:30 PM
  •  

    Adding a DragOver event handler that is the same as the DragEnter one above fixes the first problem for me: I can now copy as well as move.

    But it still doesn't work the first time!

     

    Tuesday, May 8, 2007 9:41 PM