6 Replies - 702 Views - Last Post: 07 December 2012 - 02:02 AM Rate Topic: -----

#1 deanobravo  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 131
  • Joined: 02-January 12

how to call a controls mouse events from another control

Posted 06 December 2012 - 02:13 AM

The code that follows allows me to click and drag a label around the screen and is quite effective.
The label appears when a particular cell is selected in a datagridview using the mousedown event.
What I would like to do is commence the dragging as soon as the cell is mousedowned(?) so I don't have to release the button and then click the label to drag it
Private Sub lblTransfer_MouseDown(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.MouseEventArgs) Handles lblTransfer.MouseDown
        
        mouseButtonIsDown = True
        movingPatient = True
    End Sub

    Private Sub lblTransfer_MouseMove(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.MouseEventArgs) Handles lblTransfer.MouseMove
        Dim Button As Short = eventArgs.Button \ &H100000
                
        'DRAG THE label AROUND ON THE SCREEN
        'WHILE THE LEFT BUTTON IS DOWN
        If Button = 1 Then

            lblTransfer.Left = lblTransfer.Left + (eventArgs.X + 8)
            lblTransfer.Top = lblTransfer.Top + (eventArgs.Y + 15)
              
           
        End If

    End Sub
I tried calling these events from the cell mousedown events and the labels events do get called but the label dragging does not start.
Any ideas?




Is This A Good Question/Topic? 0
  • +

Replies To: how to call a controls mouse events from another control

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,662
  • Joined: 02-June 10

Re: how to call a controls mouse events from another control

Posted 06 December 2012 - 08:15 AM

The first click is to make the button have focus.
You can set focus programmatically.
http://msdn.microsof...trol.focus.aspx
Was This Post Helpful? 0
  • +
  • -

#3 deanobravo  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 131
  • Joined: 02-January 12

Re: how to call a controls mouse events from another control

Posted 06 December 2012 - 11:58 AM

I had a bit of play with the Select method last night.
After reading the MSDN topic you pointed me to it turns out Labels are neither selectable or focusable. I guess what i'm really after is trying to make vb think that I have clicked and dragged the label without the physical click.
Was This Post Helpful? 0
  • +
  • -

#4 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: how to call a controls mouse events from another control

Posted 06 December 2012 - 02:20 PM

If you look at any event handling method, you'll see that it's just a method. For example, throw a button on a form, and double click it. Write in it some MessageBox.Show code, for example:
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        MessageBox.Show("Clicked!")
    End Sub



Now add the code for Form.Load event handler:
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Button1_Click(Me, Nothing)
    End Sub



See what we are doing inside this method? Calling Button1_Click method, and passing it parameters that are demanded: Object = Me (the Form itself), EventArgs = Nothing. You could also pass both parameters as Nothing, and it would still work, because you don't use passed objects anywhere inside Button1.click event handler.

It's the same logic as calling any other method. If you have method that accepts one argument of String, you'd pass it some String when calling it.

Here's a bit more interesting example, extending previous two chunks of code:

Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        MessageBox.Show("Clicked!")
        If TypeOf (e) Is ExtendedEventArgsExample Then
            MessageBox.Show(TryCast(e, ExtendedEventArgsExample).Text)
        End If
    End Sub

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Button1_Click(Nothing, Nothing)
        ClickAndShowPassedText("Wow...")
    End Sub

    Private Sub ClickAndShowPassedText(text As String)
        Button1_Click(Nothing, New ExtendedEventArgsExample With {.Text = text})
    End Sub

    Private Class ExtendedEventArgsExample
        Inherits EventArgs
        Public Property Text As String
    End Class
End Class



See... it's all just calling methods and feeding it with parameters it demands from you.
Was This Post Helpful? 0
  • +
  • -

#5 deanobravo  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 131
  • Joined: 02-January 12

Re: how to call a controls mouse events from another control

Posted 06 December 2012 - 11:54 PM

Soooo... are you saying because my mouse events subs are just methods I can't actually do what I want to do?
Was This Post Helpful? 0
  • +
  • -

#6 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: how to call a controls mouse events from another control

Posted 07 December 2012 - 01:25 AM

I'm saying the opposite. Try the below code in new Forms project. Don't add anything to form, just paste the code, and run it. The code acts when you single click on the label. Moving mouse around, moves label too. Clicking it again stops label moving. Then it mimics click on label, if you click anywhere inside the form:

Public Class MimicEvents

    Dim MouseDown As Boolean = False

    Private Sub lblDragMe_MouseClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles lblDragMe.MouseClick
        If MouseDown Then
            MouseDown = False
        Else
            MouseDown = True
        End If
    End Sub

    Private Sub Form1_Click(sender As Object, e As System.EventArgs) Handles Me.Click
        'this is the example of mimicking the click on label:
        lblDragMe_MouseClick(Me, New MouseEventArgs(Nothing, 0, MousePosition.X, MousePosition.Y, 0))
    End Sub

    Private Sub Form1_MouseMove(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
        If MouseDown Then
            lblDragMe.Location = e.Location
        End If
    End Sub

#Region "Designer generated code"
    Private Sub Initialize()
        Me.lblDragMe = New System.Windows.Forms.Label()
        Me.SuspendLayout()
        '
        'lblDragMe
        '
        Me.lblDragMe.AutoSize = True
        Me.lblDragMe.Location = New System.Drawing.Point(85, 55)
        Me.lblDragMe.Name = "lblDragMe"
        Me.lblDragMe.Size = New System.Drawing.Size(88, 13)
        Me.lblDragMe.TabIndex = 0
        Me.lblDragMe.Text = "Drag Me Around!"
        '
        'MimicEvents
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(392, 247)
        Me.Controls.Add(Me.lblDragMe)
        Me.Name = "MimicEvents"
        Me.Text = "Mimic Events Example - lucky3"
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub
    Friend WithEvents lblDragMe As System.Windows.Forms.Label
#End Region

    Private Sub MimicEvents_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Initialize()
    End Sub
End Class



Was This Post Helpful? 1
  • +
  • -

#7 deanobravo  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 131
  • Joined: 02-January 12

Re: how to call a controls mouse events from another control

Posted 07 December 2012 - 02:02 AM

Awesome, I'll study it tomorrow.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1