tic tac toe game, drag a control to another control

Posted on 2005-02-24
Medium Priority
Last Modified: 2010-04-23
Hi there,

I am working on a tic tac toe game, in which the X's and O's are user defined controls. I have a board which is a user defined control as well. The board has 9 cells, all these cells are labels.

I need to do a click, drag and drop moves on the controls to make a move.

So far i've used drag_enter and mouse_down functions but haven't been able to show the control move with the drag and drop it on the cell.

I am aware that the coordinates are different for screen and the actual form, and i would have to work with pointtoclient and and pointtoscreen methods to make them move and drop right.

Could someone please tell me how would i do so, or give me an example of a drag and drop. There was one on msdn for image drag and drop but it did not help me with dragging a control and dropping it.

Question by:askcool

Author Comment

ID: 13411251
Hi there,
Could someone just tell me how to do a simple drag and drop? Actually i have to do this for user defined controls. But if someone can just tell me how to drag a label and drop it on another label I can use it for my game. Its really urgent. Please help!

LVL 86

Accepted Solution

Mike Tomlinson earned 200 total points
ID: 13411968
Play with this.  Drop the X on any of the blank cells.  After each drop, the X will change to O and vice versa.  You cannot drop onto a cell that is already taken:

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()

        'This call is required by the Windows Form Designer.

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
            End If
        End If
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Label4 As System.Windows.Forms.Label
    Friend WithEvents Label5 As System.Windows.Forms.Label
    Friend WithEvents Label6 As System.Windows.Forms.Label
    Friend WithEvents Label7 As System.Windows.Forms.Label
    Friend WithEvents Label8 As System.Windows.Forms.Label
    Friend WithEvents Label9 As System.Windows.Forms.Label
    Friend WithEvents Label10 As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Label1 = New System.Windows.Forms.Label
        Me.Label3 = New System.Windows.Forms.Label
        Me.Label2 = New System.Windows.Forms.Label
        Me.Label4 = New System.Windows.Forms.Label
        Me.Label5 = New System.Windows.Forms.Label
        Me.Label6 = New System.Windows.Forms.Label
        Me.Label7 = New System.Windows.Forms.Label
        Me.Label8 = New System.Windows.Forms.Label
        Me.Label9 = New System.Windows.Forms.Label
        Me.Label10 = New System.Windows.Forms.Label
        Me.Label1.AllowDrop = True
        Me.Label1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.Label1.Location = New System.Drawing.Point(8, 8)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(32, 32)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "X"
        Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        Me.Label3.AllowDrop = True
        Me.Label3.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.Label3.Location = New System.Drawing.Point(112, 56)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(32, 32)
        Me.Label3.TabIndex = 2
        Me.Label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        Me.Label2.AllowDrop = True
        Me.Label2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.Label2.Location = New System.Drawing.Point(72, 56)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(32, 32)
        Me.Label2.TabIndex = 3
        Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        Me.Label4.AllowDrop = True
        Me.Label4.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.Label4.Location = New System.Drawing.Point(152, 56)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(32, 32)
        Me.Label4.TabIndex = 4
        Me.Label4.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        Me.Label5.AllowDrop = True
        Me.Label5.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.Label5.Location = New System.Drawing.Point(72, 96)
        Me.Label5.Name = "Label5"
        Me.Label5.Size = New System.Drawing.Size(32, 32)
        Me.Label5.TabIndex = 5
        Me.Label5.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        Me.Label6.AllowDrop = True
        Me.Label6.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.Label6.Location = New System.Drawing.Point(112, 96)
        Me.Label6.Name = "Label6"
        Me.Label6.Size = New System.Drawing.Size(32, 32)
        Me.Label6.TabIndex = 6
        Me.Label6.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        Me.Label7.AllowDrop = True
        Me.Label7.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.Label7.Location = New System.Drawing.Point(152, 96)
        Me.Label7.Name = "Label7"
        Me.Label7.Size = New System.Drawing.Size(32, 32)
        Me.Label7.TabIndex = 7
        Me.Label7.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        Me.Label8.AllowDrop = True
        Me.Label8.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.Label8.Location = New System.Drawing.Point(72, 136)
        Me.Label8.Name = "Label8"
        Me.Label8.Size = New System.Drawing.Size(32, 32)
        Me.Label8.TabIndex = 8
        Me.Label8.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        Me.Label9.AllowDrop = True
        Me.Label9.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.Label9.Location = New System.Drawing.Point(112, 136)
        Me.Label9.Name = "Label9"
        Me.Label9.Size = New System.Drawing.Size(32, 32)
        Me.Label9.TabIndex = 9
        Me.Label9.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        Me.Label10.AllowDrop = True
        Me.Label10.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.Label10.Location = New System.Drawing.Point(152, 136)
        Me.Label10.Name = "Label10"
        Me.Label10.Size = New System.Drawing.Size(32, 32)
        Me.Label10.TabIndex = 10
        Me.Label10.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        Me.AllowDrop = True
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(248, 222)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Name = "Form1"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "Label Drag & Drop"

    End Sub

#End Region

    Private Sub Label1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseDown
        Label1.DoDragDrop(Label1, DragDropEffects.All)
    End Sub

    Private Sub Label_DragOver(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Label2.DragOver, Label3.DragOver, Label4.DragOver, Label5.DragOver, Label6.DragOver, Label7.DragOver, Label8.DragOver, Label9.DragOver, Label10.DragOver
        If sender.text = "" Then
            Dim lbl As New Label
            If e.Data.GetDataPresent(lbl.GetType) Then
                lbl = CType(e.Data.GetData(lbl.GetType), Label)
                If lbl Is Me.Label1 Then
                    e.Effect = DragDropEffects.Copy
                End If
            End If
        End If
    End Sub

    Private Sub Label_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Label2.DragDrop, Label3.DragDrop, Label4.DragDrop, Label5.DragDrop, Label6.DragDrop, Label7.DragDrop, Label8.DragDrop, Label9.DragDrop, Label10.DragDrop
        Dim lbl As New Label
        If e.Data.GetDataPresent(lbl.GetType) Then
            lbl = CType(e.Data.GetData(lbl.GetType), Label)
            If Not IsNothing(lbl) Then
                sender.text = lbl.Text
                Select Case lbl.Text
                    Case "X"
                        lbl.Text = "O"
                    Case "O"
                        lbl.Text = "X"
                End Select
            End If
        End If
    End Sub

End Class

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Suggested Courses

571 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