Results 1 to 5 of 5

Thread: [RESOLVED] how to drag and drop a textbox .backcolor

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2013
    Posts
    484

    Resolved [RESOLVED] how to drag and drop a textbox .backcolor

    Hello
    I have the following code that drags and drops a textbox.text. It works the way I want it to. The problem is I want to drag and drop the textbox.backcolor and am at a loss for how to do it.
    Code:
    Public Class Form1
        Private MouseIsDown As Boolean = False 'variable declaration
        Private Sub TextBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown, TextBox3.MouseDown
            'Set a flag to show that the mouse is down.
            MouseIsDown = True
        End Sub
        Private Sub TextBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseMove, TextBox3.MouseMove
            If MouseIsDown Then
                ' Initiate dragging.
                sender.DoDragDrop(sender.Text, DragDropEffects.Copy)
            End If
            MouseIsDown = False
        End Sub
        Private Sub TextBox2_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox2.DragEnter, TextBox4.DragEnter
            'Check the format of the data being dropped.
            If (e.Data.GetDataPresent(DataFormats.Text)) Then
                ' Display the copy cursor.
                e.Effect = DragDropEffects.Copy
            Else
                ' Display the no-drop cursor.
                e.Effect = DragDropEffects.None
            End If
        End Sub
        Private Sub TextBox2_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox2.DragDrop, TextBox4.DragDrop
            'Paste the text.
            sender.Text = e.Data.GetData(DataFormats.Text)
        End Sub
        End Class
    Thank You
    George

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109,991

    Re: how to drag and drop a textbox .backcolor

    It's basically no different. You are telling it what to drag here:
    vb.net Code:
    1. sender.DoDragDrop(sender.Text, DragDropEffects.Copy)
    If you want to drag the BackColor instead of the Text, do that. You are telling it what to drop here:
    vb.net Code:
    1. sender.Text = e.Data.GetData(DataFormats.Text)
    If you want to drop the BackColor instead of the Text, do that.

    The only thing you actually need to think about is the data format. You can try just passing the BackColor as is and then see what format you get out at the other end, or you can keep using DataFormats.Text and deliberately convert the Color value to and from a String. I suggest that you read this to understand the principles involved.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109,991

    Re: how to drag and drop a textbox .backcolor

    By the way, for the code you have to actually compile, you must have Option Strict Off. That's bad. You should always set Option Strict On, which will force you to write better code by disallowing implicit narrowing conversions and late binding. You should set Option Strict On in the project properties and then fix all the errors that get flagged by performing explicit casts and conversions where required. For instance, you are using sender.Text twice there when sender is type Object, which has no Text property. If the sender is a TextBox then you should be casting as type TextBox. You should also set Option Strict On in the VS options, so it will be On by default for all projects.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,372

    Re: how to drag and drop a textbox .backcolor

    Code:
    Public Class Form1
        Private MouseIsDown As Boolean = False 'variable declaration
        Private Sub TextBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown, TextBox3.MouseDown
            'Set a flag to show that the mouse is down.
            MouseIsDown = True
        End Sub
        Private Sub TextBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseMove, TextBox3.MouseMove
            If MouseIsDown Then
                ' Initiate dragging.
                DirectCast(sender, TextBox).DoDragDrop(DirectCast(sender, TextBox).BackColor, DragDropEffects.Copy)
            End If
            MouseIsDown = False
        End Sub
        Private Sub TextBox2_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox2.DragEnter, TextBox4.DragEnter
            'Check the format of the data being dropped.
            If (e.Data.GetDataPresent(GetType(Color))) Then
                ' Display the copy cursor.
                e.Effect = DragDropEffects.Copy
            Else
                ' Display the no-drop cursor.
                e.Effect = DragDropEffects.None
            End If
        End Sub
        Private Sub TextBox2_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox2.DragDrop, TextBox4.DragDrop
            'Change the BackColor.
            DirectCast(sender, TextBox).BackColor = DirectCast(e.Data.GetData(GetType(Color)), Color)
        End Sub
    End Class

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2013
    Posts
    484

    Re: how to drag and drop a textbox .backcolor

    Thank You both so much. Love having working code to learn from.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width