- GUI
- Windows API tutorial
- Introduction to Windows API
- Windows API main functions
- System functions in Windows API
- Strings in Windows API
- Date & time in Windows API
- A window in Windows API
- First steps in UI
- Windows API menus
- Windows API dialogs
- Windows API controls I
- Windows API controls II
- Windows API controls III
- Advanced controls in Windows API
- Custom controls in Windows API
- The GDI in Windows API
- PyQt4 tutorial
- PyQt5 tutorial
- Qt4 tutorial
- Introduction to Qt4 toolkit
- Qt4 utility classes
- Strings in Qt4
- Date and time in Qt4
- Working with files and directories in Qt4
- First programs in Qt4
- Menus and toolbars in Qt4
- Layout management in Qt4
- Events and signals in Qt4
- Qt4 Widgets
- Qt4 Widgets II
- Painting in Qt4
- Custom widget in Qt4
- The Breakout game in Qt4
- Qt5 tutorial
- Introduction to Qt5 toolkit
- Strings in Qt5
- Date and time in Qt5
- Containers in Qt5
- Working with files and directories in Qt5
- First programs in Qt5
- Menus and toolbars in Qt5
- Layout management in Qt5
- Events and signals in Qt5
- Qt5 Widgets
- Qt5 Widgets II
- Painting in Qt5
- Custom widget in Qt5
- Snake in Qt5
- The Breakout game in Qt5
- PySide tutorial
- Tkinter tutorial
- Tcl/Tk tutorial
- Qt Quick tutorial
- Java Swing tutorial
- JavaFX tutorial
- Java SWT tutorial
- wxWidgets tutorial
- Introduction to wxWidgets
- wxWidgets helper classes
- First programs in wxWidgets
- Menus and toolbars in wxWidgets
- Layout management in wxWidgets
- Events in wxWidgets
- Dialogs in wxWidgets
- wxWidgets widgets
- wxWidgets widgets II
- Drag and Drop in wxWidgets
- Device Contexts in wxWidgets
- Custom widgets in wxWidgets
- The Tetris game in wxWidgets
- wxPython tutorial
- Introduction to wxPython
- First Steps
- Menus and toolbars
- Layout management in wxPython
- Events in wxPython
- wxPython dialogs
- Widgets
- Advanced widgets in wxPython
- Drag and drop in wxPython
- Internationalisation
- Application skeletons in wxPython
- The GDI
- Mapping modes
- Creating custom widgets
- Tips and Tricks
- wxPython Gripts
- The Tetris game in wxPython
- C# Winforms Mono tutorial
- Java Gnome tutorial
- Introduction to Java Gnome
- First steps in Java Gnome
- Layout management in Java Gnome
- Layout management II in Java Gnome
- Menus in Java Gnome
- Toolbars in Java Gnome
- Events in Java Gnome
- Widgets in Java Gnome
- Widgets II in Java Gnome
- Advanced widgets in Java Gnome
- Dialogs in Java Gnome
- Pango in Java Gnome
- Drawing with Cairo in Java Gnome
- Drawing with Cairo II
- Nibbles in Java Gnome
- QtJambi tutorial
- GTK+ tutorial
- Ruby GTK tutorial
- GTK# tutorial
- Visual Basic GTK# tutorial
- PyGTK tutorial
- Introduction to PyGTK
- First steps in PyGTK
- Layout management in PyGTK
- Menus in PyGTK
- Toolbars in PyGTK
- Signals & events in PyGTK
- Widgets in PyGTK
- Widgets II in PyGTK
- Advanced widgets in PyGTK
- Dialogs in PyGTK
- Pango
- Pango II
- Drawing with Cairo in PyGTK
- Drawing with Cairo II
- Snake game in PyGTK
- Custom widget in PyGTK
- PHP GTK tutorial
- C# Qyoto tutorial
- Ruby Qt tutorial
- Visual Basic Qyoto tutorial
- Mono IronPython Winforms tutorial
- Introduction
- First steps in IronPython Mono Winforms
- Layout management
- Menus and toolbars
- Basic Controls in Mono Winforms
- Basic Controls II in Mono Winforms
- Advanced Controls in Mono Winforms
- Dialogs
- Drag & drop in Mono Winforms
- Painting
- Painting II in IronPython Mono Winforms
- Snake in IronPython Mono Winforms
- The Tetris game in IronPython Mono Winforms
- FreeBASIC GTK tutorial
- Jython Swing tutorial
- JRuby Swing tutorial
- Visual Basic Winforms tutorial
- JavaScript GTK tutorial
- Ruby HTTPClient tutorial
- Ruby Faraday tutorial
- Ruby Net::HTTP tutorial
- Java 2D games tutorial
- Java 2D tutorial
- Cairo graphics tutorial
- PyCairo tutorial
- HTML5 canvas tutorial
- Python tutorial
- Python language
- Interactive Python
- Python lexical structure
- Python data types
- Strings in Python
- Python lists
- Python dictionaries
- Python operators
- Keywords in Python
- Functions in Python
- Files in Python
- Object-oriented programming in Python
- Modules
- Packages in Python
- Exceptions in Python
- Iterators and Generators
- Introspection in Python
- Ruby tutorial
- PHP tutorial
- Visual Basic tutorial
- Visual Basic
- Visual Basic lexical structure
- Basics
- Visual Basic data types
- Strings in Visual Basic
- Operators
- Flow control
- Visual Basic arrays
- Procedures & functions in Visual Basic
- Organizing code in Visual Basic
- Object-oriented programming
- Object-oriented programming II in Visual Basic
- Collections in Visual Basic
- Input & output
- Tcl tutorial
- C# tutorial
- Java tutorial
- AWK tutorial
- Jetty tutorial
- Tomcat Derby tutorial
- Jtwig tutorial
- Android tutorial
- Introduction to Android development
- First Android application
- Android Button widgets
- Android Intents
- Layout management in Android
- Android Spinner widget
- SeekBar widget
- Android ProgressBar widget
- Android ListView widget
- Android Pickers
- Android menus
- Dialogs
- Drawing in Android
- Java EE 5 tutorials
- Introduction
- Installing Java
- Installing NetBeans 6
- Java Application Servers
- Resin CGIServlet
- JavaServer Pages, (JSPs)
- Implicit objects in JSPs
- Shopping cart
- JSP & MySQL Database
- Java Servlets
- Sending email in a Servlet
- Creating a captcha in a Servlet
- DataSource & DriverManager
- Java Beans
- Custom JSP tags
- Object relational mapping with iBATIS
- Jsoup tutorial
- MySQL tutorial
- MySQL quick tutorial
- MySQL storage engines
- MySQL data types
- Creating, altering and dropping tables in MySQL
- MySQL expressions
- Inserting, updating, and deleting data in MySQL
- The SELECT statement in MySQL
- MySQL subqueries
- MySQL constraints
- Exporting and importing data in MySQL
- Joining tables in MySQL
- MySQL functions
- Views in MySQL
- Transactions in MySQL
- MySQL stored routines
- MySQL Python tutorial
- MySQL Perl tutorial
- MySQL C API programming tutorial
- MySQL Visual Basic tutorial
- MySQL PHP tutorial
- MySQL Java tutorial
- MySQL Ruby tutorial
- MySQL C# tutorial
- SQLite tutorial
- SQLite C tutorial
- SQLite PHP tutorial
- SQLite Python tutorial
- SQLite Perl tutorial
- SQLite Ruby tutorial
- SQLite C# tutorial
- SQLite Visual Basic tutorial
- PostgreSQL C tutorial
- PostgreSQL Python tutorial
- PostgreSQL Ruby tutorial
- PostgreSQL PHP tutorial
- PostgreSQL Java tutorial
- Apache Derby tutorial
- SQLAlchemy tutorial
- MongoDB PHP tutorial
- MongoDB Java tutorial
- MongoDB JavaScript tutorial
- MongoDB Ruby tutorial
- Spring JdbcTemplate tutorial
- JDBI tutorial
Drag & drop
This part of the Mono Visual Basic Winforms tutorial will be dedicated to the drag & drop operations.
In computer graphical user interfaces, drag-and-drop is the action of (or support for the action of) clicking on a virtual object and dragging it to a different location or onto another virtual object. In general, it can be used to invoke many kinds of actions, or create various types of associations between two abstract objects. (Wikipedia)
Drag and drop functionality is one of the most visible aspects of the graphical user interface. Drag and drop operation enables you to do complex things intuitively.
Dragging a button
In the first example, we will do the drag & drop operation on the button control. The example does the job outside the drag & drop protocol.
' ZetCode Mono Visual Basic Winforms tutorial ' ' In this program we drag & drop ' a button control ' ' author jan bodnar ' last modified May 2009 ' website www.zetcode.com Imports System.Windows.Forms Imports System.Drawing Public Class WinVBApp Inherits Form Private isDragging As Boolean = False Private oldX As Integer Private oldY As Integer Private button As Button Public Sub New Me.Text = "Drag & Drop button" Me.Size = New Size(270, 180) Me.InitUI Me.CenterToScreen End Sub Private Sub InitUI button = New Button button.Parent = Me button.Cursor = Cursors.Hand button.Text = "Button" button.Location = New Point(20, 20) AddHandler button.MouseDown, AddressOf Me.OnMouseDown AddHandler button.MouseUp, AddressOf Me.OnMouseUp AddHandler button.MouseMove, AddressOf Me.OnMouseMove End Sub Private Sub OnMouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) isDragging = True oldX = e.X oldY = e.Y End Sub Private Sub OnMouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) If isDragging button.Top = button.Top + (e.Y - oldY) button.Left = button.Left + (e.X - oldX) End If End Sub Private Sub OnMouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) isDragging = False End Sub Public Shared Sub Main Application.Run(New WinVBApp) End Sub End Class
The code example puts a regular button control on the form container. By clicking on the button surface and simultaneously dragging it with a mouse we can relocate the button.
There are some supporting variables in our example. The isDragging
variable tells us, whether we are in the process of dragging an object. The oldX
and oldY
variables store the x, y coordinates just before the dragging process begins.
AddHandler button.MouseDown, AddressOf Me.OnMouseDown AddHandler button.MouseUp, AddressOf Me.OnMouseUp AddHandler button.MouseMove, AddressOf Me.OnMouseMove
We plug in three different mouse handlers for our button. They implement three different stages of the drag & drop process. The process begins, when we click on the button. This is handled by the OnMouseDown
method. The second part is the movement. This is when we move the object to a new position. It is handled in the OnMouseMove
method. The final part is when the process stops. It happens when we release the mouse button. The appropriate task is delegated to the OnMouseUp
method.
Private Sub OnMouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) isDragging = True oldX = e.X oldY = e.Y End Sub
The OnMouseDown
method implements the first part of the process. It sets three necessary variables.
Private Sub OnMouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) If isDragging button.Top = button.Top + (e.Y - oldY) button.Left = button.Left + (e.X - oldX) End If End Sub
In the OnMouseMove
method, we relocate the button. We calculate the difference between the stored x, y coordinates and the new coordinates of the mouse pointer. The difference is added to the Top
and Left
properties of the button, thus moving it to a new position.

Dragging Text
In the previous example, we did drag & drop on the control. Next we will do a drag & drop operation on the textual data. Here we will use the drag & drop protocol provided by the Winforms library.
Drag & drop operation is a standardized communication protocol in Winforms. We have two basic objects. The drag source
and the drop target
.
' ZetCode Mono Visual Basic Winforms tutorial ' ' In this program we drag & drop ' text ' ' author jan bodnar ' last modified May 2009 ' website www.zetcode.com Imports System.Windows.Forms Imports System.Drawing Public Class WinVBApp Inherits Form Private txtBox As TextBox Private btn As Button Public Sub New Me.Text = "Drag & Drop text" Me.Size = New Size(250, 200) Me.InitUI Me.CenterToScreen End Sub Private Sub InitUI btn = New Button txtBox = New TextBox Me.SuspendLayout btn.AllowDrop = True btn.Location = New Point(150, 50) txtBox.Location = New Point(15, 50) Me.Controls.Add(btn) Me.Controls.Add(txtBox) Me.ResumeLayout AddHandler btn.DragEnter, AddressOf Me.OnDragEnter AddHandler btn.DragDrop, AddressOf Me.OnDragDrop AddHandler txtBox.MouseDown, AddressOf Me.OnMouseDown End Sub Private Sub OnDragEnter(ByVal sender As Object, ByVal e As DragEventArgs) e.Effect = DragDropEffects.Copy End Sub Private Sub OnDragDrop(ByVal sender As Object, ByVal e As DragEventArgs) sender.Text = e.Data.GetData(DataFormats.Text) End Sub Private Sub OnMouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) sender.DoDragDrop(sender.Text, DragDropEffects.Copy) End Sub Public Shared Sub Main Application.Run(New WinVBApp) End Sub End Class
We have two controls on the form. A button and a text box. We will drag text from the text box and drop it on the button.
btn.AllowDrop = True
We set the AllowDrop
property to true. Dropping is not enabled by default.
AddHandler btn.DragEnter, AddressOf Me.OnDragEnter AddHandler btn.DragDrop, AddressOf Me.OnDragDrop AddHandler txtBox.MouseDown, AddressOf Me.OnMouseDown
Again, the drag & drop process is divided into three steps. We have three methods for each particular step.
Private Sub OnDragEnter(ByVal sender As Object, ByVal e As DragEventArgs) e.Effect = DragDropEffects.Copy End Sub
The DragEnter
event is launched when the mouse pointer enters the area of the drop target control. The Effect
property must be set. The DragDropEffects of the drag source and drop target must be equal. Otherwise the operation will not work.
Private Sub OnDragDrop(ByVal sender As Object, ByVal e As DragEventArgs) sender.Text = e.Data.GetData(DataFormats.Text) End Sub
Finally we have the OnDragDrop
method. Here we get the data from the event object and set it to the button Text
property.
Private Sub OnMouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) sender.DoDragDrop(sender.Text, DragDropEffects.Copy) End Sub
In the OnMouseDown
method we initialize the drap & drop process. We initiate the process with the DoDragDrop
method. The DragDropEffects.Copy
parameter specifies the type of the operation. Esentially, we can either copy the text or move it during the drag & drop operation.

Dragging Image
In our last example, we will drag & drop image on the form.
' ZetCode Mono Visual Basic Winforms tutorial ' ' In this program we drag & drop ' an image ' ' author jan bodnar ' last modified May 2009 ' website www.zetcode.com Imports System.Windows.Forms Imports System.Drawing Public Class WinVBApp Inherits Form Private IsDragging As Boolean = False Private oldX As Integer Private oldY As Integer Private dropRect As Rectangle Private picBox As PictureBox Private image As Bitmap Private brsh As Brush Public Sub New Me.Text = "Drag & Drop image" Me.Size = New Size(350, 250) Me.InitUI Me.CenterToScreen End Sub Private Sub InitUI isDragging = False dropRect = New Rectangle(10, 10, 200, 160) brsh = Brushes.Gray picBox = New PictureBox Me.LoadImage picBox.Parent = Me picBox.Location = New Point(100, 50) picBox.Size = New Size(image.Width, image.Height) picBox.Image = image picBox.Cursor = Cursors.Hand AddHandler Me.Paint, AddressOf Me.OnPaint AddHandler picBox.MouseDown, AddressOf Me.OnMouseDown AddHandler picBox.MouseMove, AddressOf Me.OnMouseMove AddHandler picBox.MouseUp, AddressOf Me.OnMouseUp End Sub Private Sub LoadImage Try image = New Bitmap("image.jpg") Catch Console.WriteLine("Error reading image") Environment.Exit(1) End Try End Sub Private Sub OnMouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) isDragging = True oldX = e.X oldY = e.Y End Sub Private Sub OnMouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) If isDragging picBox.Top = picBox.Top + (e.Y - oldY) picBox.Left = picBox.Left + (e.X - oldX) End If End Sub Private Sub OnMouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) isDragging = False If dropRect.Contains(picBox.Bounds) brsh = Brushes.Gold Else brsh = Brushes.Gray End If Me.Refresh End Sub Private Sub OnPaint(ByVal sender As Object, ByVal e As PaintEventArgs) Dim g As Graphics = e.Graphics g.FillRectangle(brsh, dropRect) End Sub Public Shared Sub Main Application.Run(New WinVBApp) End Sub End Class
In our example we have a PictureBox
and we draw a gray rectangle. If we drop the picture inside the rectangle, the color of the rectangle changes to gold.
brsh = Brushes.Gray
The brsh
variable holds the brush of the rectangle. It is a gray color by default.
Private Sub LoadImage Try image = New Bitmap("image.jpg") Catch Console.WriteLine("Error reading image") Environment.Exit(1) End Try End Sub
The LoadImage
method loads a bitmap for the PictureBox
control.
If dropRect.Contains(picBox.Bounds) brsh = Brushes.Gold Else brsh = Brushes.Gray End If
In the OnMouseUp
method, we determine the brush of the rectangle. If the bounds of the picture box are inside the rectangle, the brush is of gold color; gray otherwise.
Me.Refresh
We must call the Refresh
method to activate the new brush color.

This chapter was dedicated to drag & drop operations using the Mono Winforms library with the Visual Basic language.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论