How to code multiple events

Drag and drop text numbers from buttons to text box.

Page 1 of 1

3 Replies - 1915 Views - Last Post: 22 July 2010 - 02:56 AM Rate Topic: -----

#1 jo_robo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 21-June 10

How to code multiple events

Posted 19 July 2010 - 03:17 AM

Hi. I am really new at this and need to find out how to code multiple events so that I don't have to type the same thing out many times as you can see I have done below.
I am sure there is a way to do it but I am lost.
It is for a Mastermind game and I have created 8 buttons from which you can drag the text number on the button into a text box. there are 20 text boxes. 5 x rows of 4 textboxes.
I have started to try to do it for button 1 but I am confused. I have tried to copy from other people's code but it is just confusing me more. Can someone please put me on the right track.

 

Private Sub Button1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles 
        Button1.MouseDown, Button2.MouseDown,Button3.MouseDown,Button4.MouseDown,_
        Button5.MouseDown, Button6.MouseDown, Button7.MouseDown, Button8.MouseDown
        Dim Txtbox As TextBox
        Txtbox = CType(sender, TextBox)
        Txtbox.SelectAll()
        Button1.DoDragDrop(Button1.Text, DragDropEffects.Copy)

    End Sub

    Private Sub txtGuess1_R1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles txtGuess1.DragEnter
        If (e.Data.GetDataPresent(DataFormats.Text)) Then
            e.Effect = DragDropEffects.Copy
        Else
            e.Effect = DragDropEffects.None
        End If

    End Sub
    Private Sub txtGuess1_R1_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles txtGuess1.DragDrop
        txtGuess1.Text = e.Data.GetData(DataFormats.Text).ToString

        Button1.Text = "1"
        Button2.Text = "2"
        Button3.Text = "3"
        Button4.Text = "4"
        Button5.Text = "5"
        Button6.Text = "6"
        Button7.Text = "7"
        Button8.Text = "8"

    End Sub
     Private Sub Button2_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button2.MouseDown
        Button2.DoDragDrop(Button2.Text, DragDropEffects.Copy)
    End Sub
    Private Sub txtGuess2_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles txtGuess2.DragEnter
        If (e.Data.GetDataPresent(DataFormats.Text)) Then
            e.Effect = DragDropEffects.Copy
        Else
            e.Effect = DragDropEffects.None
        End If
    End Sub  


 Private Sub txtGuess2_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles txtGuess2.DragDrop
        txtGuess2.Text = e.Data.GetData(DataFormats.Text).ToString

        Button1.Text = "1"
        Button2.Text = "2"
        Button3.Text = "3"
        Button4.Text = "4"
        Button5.Text = "5"
        Button6.Text = "6"
        Button7.Text = "7"
        Button8.Text = "8"

    End Sub
    Private Sub Button3_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button3.MouseDown
        Button3.DoDragDrop(Button3.Text, DragDropEffects.Copy)
    End Sub
    Private Sub txtGuess3_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles txtGuess3.DragEnter
        If (e.Data.GetDataPresent(DataFormats.Text)) Then
            e.Effect = DragDropEffects.Copy
        Else
            e.Effect = DragDropEffects.None
        End If
    End Sub
    Private Sub txtGuess3_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles txtGuess3.DragDrop
        txtGuess3.Text = e.Data.GetData(DataFormats.Text).ToString

        Button1.Text = "1"
        Button2.Text = "2"
        Button3.Text = "3"
        Button4.Text = "4"
        Button5.Text = "5"
        Button6.Text = "6"
        Button7.Text = "7"
        Button8.Text = "8"

    End Sub
    Private Sub Button4_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button4.MouseDown
        Button4.DoDragDrop(Button4.Text, DragDropEffects.Copy)
    End Sub
    Private Sub txtGuess4_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles txtGuess4.DragEnter
        If (e.Data.GetDataPresent(DataFormats.Text)) Then
            e.Effect = DragDropEffects.Copy
        Else
            e.Effect = DragDropEffects.None
        End If
    End Sub
    Private Sub txtGuess4_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles txtGuess4.DragDrop
        txtGuess4.Text = e.Data.GetData(DataFormats.Text).ToString

        Button1.Text = "1"
        Button2.Text = "2"
        Button3.Text = "3"
        Button4.Text = "4"
        Button5.Text = "5"
        Button6.Text = "6"
        Button7.Text = "7"
        Button8.Text = "8"

    End Sub
    Private Sub Button5_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button5.MouseDown
        Button5.DoDragDrop(Button5.Text, DragDropEffects.Copy)
    End Sub
    Private Sub txtGuess5_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles txtGuess5.DragEnter
        If (e.Data.GetDataPresent(DataFormats.Text)) Then
            e.Effect = DragDropEffects.Copy
        Else
            e.Effect = DragDropEffects.None
        End If
    End Sub
    Private Sub txtGuess5_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles txtGuess5.DragDrop
        txtGuess5.Text = e.Data.GetData(DataFormats.Text).ToString

        Button1.Text = "1"
        Button2.Text = "2"
        Button3.Text = "3"
        Button4.Text = "4"
        Button5.Text = "5"
        Button6.Text = "6"
        Button7.Text = "7"
        Button8.Text = "8"

    End Sub
    Private Sub Button6_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button6.MouseDown
        Button6.DoDragDrop(Button6.Text, DragDropEffects.Copy)
    End Sub

    Private Sub Button7_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button7.MouseDown
        Button7.DoDragDrop(Button7.Text, DragDropEffects.Copy)
    End Sub

    Private Sub Button8_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button8.MouseDown
        Button8.DoDragDrop(Button8.Text, DragDropEffects.Copy)
    End Sub
    Private Sub txtGuess6_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles txtGuess6.DragEnter
        If (e.Data.GetDataPresent(DataFormats.Text)) Then
            e.Effect = DragDropEffects.Copy
        Else
            e.Effect = DragDropEffects.None
        End If
    End Sub
    Private Sub txtGuess6_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles txtGuess6.DragDrop
        txtGuess6.Text = e.Data.GetData(DataFormats.Text).ToString

        Button1.Text = "1"
        Button2.Text = "2"
        Button3.Text = "3"
        Button4.Text = "4"
        Button5.Text = "5"
        Button6.Text = "6"
        Button7.Text = "7"
        Button8.Text = "8"

    End Sub

    Private Sub txtGuess7_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles txtGuess7.DragEnter
        If (e.Data.GetDataPresent(DataFormats.Text)) Then
            e.Effect = DragDropEffects.Copy
        Else
            e.Effect = DragDropEffects.None
        End If
    End Sub
    Private Sub txtGuess7_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles txtGuess7.DragDrop
        txtGuess7.Text = e.Data.GetData(DataFormats.Text).ToString

        Button1.Text = "1"
        Button2.Text = "2"
        Button3.Text = "3"
        Button4.Text = "4"
        Button5.Text = "5"
        Button6.Text = "6"
        Button7.Text = "7"
        Button8.Text = "8"

    End Sub
    Private Sub txtGuess8_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles txtGuess8.DragEnter
        If (e.Data.GetDataPresent(DataFormats.Text)) Then
            e.Effect = DragDropEffects.Copy
        Else
            e.Effect = DragDropEffects.None
        End If
    End Sub
    Private Sub txtGuess8_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles txtGuess8.DragDrop
        txtGuess8.Text = e.Data.GetData(DataFormats.Text).ToString

        Button1.Text = "1"
        Button2.Text = "2"
        Button3.Text = "3"
        Button4.Text = "4"
        Button5.Text = "5"
        Button6.Text = "6"
        Button7.Text = "7"
        Button8.Text = "8"

    End Sub
    Private Sub txtGuess9_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles txtGuess1_R3.DragEnter
        If (e.Data.GetDataPresent(DataFormats.Text)) Then
            e.Effect = DragDropEffects.Copy
        Else
            e.Effect = DragDropEffects.None
        End If
    End Sub
    Private Sub txtGuess9_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles txtGuess1_R3.DragDrop
        txtGuess1_R3.Text = e.Data.GetData(DataFormats.Text).ToString

        Button1.Text = "1"
        Button2.Text = "2"
        Button3.Text = "3"
        Button4.Text = "4"
        Button5.Text = "5"
        Button6.Text = "6"
        Button7.Text = "7"
        Button8.Text = "8"

    End Sub
    Private Sub txtGuess10_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles txtGuess2_R3.DragEnter
        If (e.Data.GetDataPresent(DataFormats.Text)) Then
            e.Effect = DragDropEffects.Copy
        Else
            e.Effect = DragDropEffects.None
        End If
    End Sub
    Private Sub txtGuess10_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles txtGuess2_R3.DragDrop
        txtGuess2_R3.Text = e.Data.GetData(DataFormats.Text).ToString

        Button1.Text = "1"
        Button2.Text = "2"
        Button3.Text = "3"
        Button4.Text = "4"
        Button5.Text = "5"
        Button6.Text = "6"
        Button7.Text = "7"
        Button8.Text = "8"

    End Sub
    Private Sub txtGuess11_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles txtGuess3_R3.DragEnter
        If (e.Data.GetDataPresent(DataFormats.Text)) Then
            e.Effect = DragDropEffects.Copy
        Else
            e.Effect = DragDropEffects.None
        End If
    End Sub
    Private Sub txtGuess11_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles txtGuess3_R3.DragDrop
        txtGuess3_R3.Text = e.Data.GetData(DataFormats.Text).ToString

        Button1.Text = "1"
        Button2.Text = "2"
        Button3.Text = "3"
        Button4.Text = "4"
        Button5.Text = "5"
        Button6.Text = "6"
        Button7.Text = "7"
        Button8.Text = "8"

    End Sub



Is This A Good Question/Topic? 0
  • +

Replies To: How to code multiple events

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6111
  • View blog
  • Posts: 23,671
  • Joined: 23-August 08

Re: How to code multiple events

Posted 19 July 2010 - 04:55 AM

Moved to VB.NET
Was This Post Helpful? 0
  • +
  • -

#3 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1623
  • View blog
  • Posts: 5,200
  • Joined: 25-September 09

Re: How to code multiple events

Posted 19 July 2010 - 05:17 AM

You can just combine the handles
Private Sub txtGuess1_R1_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles txtGuess1.DragDrop, txtGuess2.DragDrop, txtGuess3.DragDrop

You can then use sender to determine what control is triggering the event.

Same goes for your DragEnter events.
Was This Post Helpful? 1
  • +
  • -

#4 jo_robo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 21-June 10

Re: How to code multiple events

Posted 22 July 2010 - 02:56 AM

View PostCharlieMay, on 19 July 2010 - 04:17 AM, said:

You can just combine the handles
Private Sub txtGuess1_R1_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles txtGuess1.DragDrop, txtGuess2.DragDrop, txtGuess3.DragDrop

You can then use sender to determine what control is triggering the event.

Same goes for your DragEnter events.



Thanks for the reply. I'll try to work that out.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1