Instant of custom control is disappear when click

2019-06-09 08:20发布

I need to make "work schedule bar".

In MainForm has a bar. when user click on the bar.A ctrlPin will created. (ctrlPin is custom control has methods for drag-drop. So it' compound with Mousedown,MouseMove and MouseUp Events )

(I'am sorry. My account can't attach image. But you can see screen capture by this link.)

-Problem clicking >>> http://imgur.com/3lxfB2b and http://imgur.com/mG6L5lD

In mainFrom.vb will create new instant of my custom control.

[MainForm.vb]

        Private Sub picBar_Day01_MouseClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles picBar_Day01.MouseClick
            Dim ctrlPin_01 As New ctrlPin(picBar_Day01, e.Location, "Break")
            ctrlPin_01.Ctrl_mouseDown(Me)  
            ctrlPin_01.Ctrl_mouseMove(Me)      
            ctrlPin_01.Ctrl_mouseUp(Me)        
            ctrlPin_01.Location = New System.Drawing.Point(e.Location.X , e.Location.Y )
            lblCtrlX.Text = ctrlPin_01.Location.X
            Me.Controls.Add(ctrlPin_01)
        End Sub      

In my custom control (ctrlPin.vb) has three event for drag-drop : mouseDown, mouseMove and mouseUpEvent. I need to call All events form Mainfrm.vb so I made methods for sending mainform by addHandle then call event by addressOf.

[ctrlPin.vb]

      Public Sub Ctrl_mouseMove(ByVal frm As Form)       
           AddHandler frm.MouseMove, AddressOf ctrlPin_MouseMove    
      End Sub

The AddressOf will call inner event of custom control like this.

     Private Sub ctrlPin_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs)   Handles Me.MouseMove        
        If isDrag Then
            Me.Left += e.X - Me.m_intPosPin.X
        End If                     
     End Sub

OK, It's can drag and move by clicking from mainForm. But if i move cursor outside the custom control then it's disappear.


RESOLVED: I just use Capture function for detecting mouse instead inner boolean flag (isDrag) In mainForm.vb just create new instant of custom control.

[mainForm.Vb]

    Private Sub picBar_Day01_MouseClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles picBar_Day01.MouseClick
        Dim ctrlPin_01 As New ctrlPin(picBar_Day01, e.Location, "Break")
        Me.Controls.Add(ctrlPin_01)
    End Sub

Important, In custome control(ctrlPin.vb) just change flag variable (isDrag) to be "Me.Capture". It's work!!!!!!

[ctrlPin.vb]

        Private Sub ctrlPin_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove     
           If Me.Capture Then                  
               Me.Left += e.X - Me.m_intPosPin.X
           End If        
        End Sub

0条回答
登录 后发表回答