Windows Develop Bookmark and Share   
 index > Windows Forms General > Swap values of two textboxes using drag and drop
 

Swap values of two textboxes using drag and drop

I'm very new to .net .... any help is much appreciated!

I'm using vb.net and visual studio.net to build a form. This form has two textboxes (textbox1 and textbox2). I'm trying to drag and drop the value of textbox2 into textbox1. Further, after the drop, I went textbox2 to retain the value of textbox1. A swap. See illustration below:


Figure 1 - The initial state of both textboxes
Textbox1 Textbox2
15 37


Figure 2 - The state of the textboxes after the drag and drop
Textbox1 Textbox2
37 <----- 15



So far, I've gotten this to work:
Textbox1 Textbox2
37 <----- 0


I've debugged what I've done so far, and checked the call stack. What's happing so far is:
1. The mousedown event occurs on textbox2. This even triggers the drag and drop event of textbox1
2. In the textbox1.drag_and_drop(), I've added some code to set textbox1.text =
3. Control is returned back to textbox2.mousedown() where I set textbox2.text = 0

I've tried overloading the procedure textbox1.drag_and_drop() to include a 3rd, byRef, argument oldValue as String, that I set to the original value of textbox1.text. However this doesn't compile because textbox1.drag_and_drop() HANDLES dragdrop.

My question is this: What's the easiest way of getting this done? If I'm going down the right road, how do I overload the dragdrop event so that I can implement this 3rd argument? If I overload the event, do I need to recreate all the code associated with dragdrop?

MigrationUser 1  Wednesday, February 05, 2003 6:23 PM
Here is what I got to work. This does a swap between Textbox2 and Textbox1. If you want it to be able to go both ways just replicate the code for each textbox.

Enjoy.



Private Sub TextBox2_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox2.MouseDown
DoDragDrop(Me.TextBox2, DragDropEffects.All)

End Sub

Private Sub TextBox1_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragOver
e.Effect = DragDropEffects.All
End Sub

Private Sub TextBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragDrop
If e.Data.GetDataPresent(GetType(TextBox)) Then
Dim cTb As TextBox
Dim sSwap As String

cTb = e.Data.GetData(GetType(TextBox))

sSwap = cTb.Text

cTb.Text = Me.TextBox1.Text
Me.TextBox1.Text = sSwap

End If
End Sub

 
MigrationUser 1  Thursday, February 13, 2003 9:24 AM
Here is what I got to work. This does a swap between Textbox2 and Textbox1. If you want it to be able to go both ways just replicate the code for each textbox.

Enjoy.



Private Sub TextBox2_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox2.MouseDown
DoDragDrop(Me.TextBox2, DragDropEffects.All)

End Sub

Private Sub TextBox1_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragOver
e.Effect = DragDropEffects.All
End Sub

Private Sub TextBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragDrop
If e.Data.GetDataPresent(GetType(TextBox)) Then
Dim cTb As TextBox
Dim sSwap As String

cTb = e.Data.GetData(GetType(TextBox))

sSwap = cTb.Text

cTb.Text = Me.TextBox1.Text
Me.TextBox1.Text = sSwap

End If
End Sub

 
MigrationUser 1  Thursday, February 13, 2003 9:24 AM

You can use google to search for other answers

Custom Search

More Threads

• Problem using DHTML Edit Control for IE5 and Windows Form in Windows Service for Printing HTML programatically
• Gridview Autocomplete Related Stuff
• C# Windows : Graphical User Interface
• Help regarding data grids
• how to void the TabIndex property so labels cannot be tabbed through?
• Capture ScrollBars True Value
• Having problems changing to 3rd form. Gives "An unhandled exception of type 'System.NullReferenceException' occurred in System.Windows.Forms.dll" error.
• Custom TaskBar Button API's?
• Is it possible to get a controls original location on a DragDrop event?
• maximising a running process in system tray in c#