Search code examples
vb.netwinformsuserform

Visual Basic secondary form slide over main, slide off main, not closing


I have been creating a Visual Basic Win App for a few different things. Something I was wanting to add onto it was having the main form in the background, when a side button is clicked, as shown below "Contact", the "Contact" form slides in from the right side of the screen, going left. Once fully on it stops and can be interacted with. This all works. My problem is that when the "X" button (representing close) is pressed, I want the form to slide off the screen then close instead of remaining open.

I have been able to get the form to slide off the screen. I have also been able to get the form to close. However, I have not been able to get it to slide off the screen then close. Below are some images of the form, and the code I have for the "X" button on my frmContact form. I have used a Timer Tick for the animation. I have already seen that someone else has made something like this using a "DOWN" function with psTimerFunc and piCount, however when I was altering this to try make it suit my application it did not work. (I would also just like to mention I have not done much in the way of Visual Basic, and this is me playing around/testing stuff that I can do, any help or tips are appreciated)

Here is the code for loading the form, when the "X" button is clicked, bringing the form onto the screen "tmrContact_Tick" and then taking the form off the screen (does not work) "tmrContact2":

Sub LoadForm(MessageDescription As String)
    Me.Top = 0
    Me.Left = Screen.PrimaryScreen.Bounds.Width
    Me.TopMost = True
    Me.BringToFront()
    Me.Show()
    tmrContact.Enabled = True
End Sub

Private Sub btnX_Click(sender As Object, e As EventArgs) Handles btnX.Click
    tmrContact2.Enabled = True
End Sub

Private Sub tmrContact_Tick(sender As Object, e As EventArgs) Handles tmrContact.Tick
    Me.Left = Me.Left - 5
    If Me.Left + Me.Width <= Screen.PrimaryScreen.Bounds.Width + 10 Then
        tmrContact.Enabled = False
    End If
End Sub

Private Sub tmrContact2_Tick(sender As Object, e As EventArgs) Handles tmrContact2.Tick
    Do While Me.Left + Me.Width >= Screen.PrimaryScreen.Bounds.Width - 10
        Me.Left = Me.Left + 1
        Application.DoEvents() ' Allow the application to process other events
    Loop

    tmrContact2.Enabled = False
    Me.Close()
End Sub

The Main UserForm, Contact Form when "Contact" is clicked

UPDATE:

I have updated the code inline with some advice from user "Jimi" using AnimateWindows. Below is some code I was able to put together and get working, I have manually chosen the starting location of the Contact Form so now when I click open the Contact Form, it does slide onto the screen going from right to left, it isn't very clean, but it will do for now, however when I click the X button, it does not go from left to right, it just closes. Below is the code:

Imports System.Runtime.InteropServices

Public Class frmContact <DllImport("user32.dll")> Private Shared Function AnimateWindow(ByVal hWnd As IntPtr, ByVal time As Integer, ByVal flags As Integer) As Boolean End Function

Private Sub frmContact_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    AnimateWindow(Me.Handle, 100, &H2)
End Sub

Private Sub btnX_Click(sender As Object, e As EventArgs) Handles btnX.Click
    AnimateWindow(Me.Handle, 100, &H1)
End Sub

End Class

Here is an unlisted YT video link showing the problem in action: https://youtu.be/ocePY2Dl_SY


Solution

  • I have been working on the program all afternoon/evening trying to come up with a solution. Luckily, I was able to figure it out. In the below code, when the Contact Form is loaded, the properties are set with their colours, as well as the positioning/sizing of the form and where it should be in relation to the main form (frmDDEF); also, the form is brought to the front and shown with the sliding in animation.

    Once "X" is clicked, the form slides off the screen; once off, it is sent to the back and closed. This makes it so that, if the main form is not maximised, without Me.SendToBack(), you would be able to see the form sliding still and then closing once it is off the screen; this line of code prevents that.

    Public Class frmContact
    
        Public Sub LoadForm(MessageDescription As String)
            grpCONTACTTOP.BackColor = Color.FromArgb(2, 7, 26)
            pnlContact.BackColor = Color.FromArgb(39, 47, 76)
            Me.Top = frmDDEF.Top
            Me.Left = frmDDEF.Left + frmDDEF.Width ' Appears from the right side of the form
            Me.Height = frmDDEF.Height ' Matches the height of Contact with DDEF
            Me.TopMost = True
            Me.BringToFront()
            Me.Show()
            tmrContact.Enabled = True
        End Sub
    
        Private Sub lblX_Click(sender As Object, e As EventArgs) Handles lblX.Click
            tmrContact2.Enabled = True
        End Sub
    
        Private Sub tmrContact_Tick(sender As Object, e As EventArgs) Handles tmrContact.Tick ' Move left
            Me.Left = Me.Left - 5
            If Me.Left + Me.Width <= frmDDEF.Left + frmDDEF.Width + 10 Then
                tmrContact.Enabled = False
            End If
        End Sub
    
        Private Sub tmrContact2_Tick(sender As Object, e As EventArgs) Handles tmrContact2.Tick ' Move right
            Me.Left = Me.Left + 5
            If Me.Left >= frmDDEF.Left + frmDDEF.Width Then
                Me.SendToBack()
                tmrContact2.Enabled = False
                Me.Close()
            End If
        End Sub
    End Class