Search code examples
vb.netvariablesresizepicturebox

How to make form snap to certain heights when the form is resized vertically?


I have 3 PictureBoxes on a form that are tiled on top of each other. the form has a minimum vaule of (502, 416) and a maximum of (502, 1080).

because the user can select from a MenuStrip to display '1', '2' 0r '3' PictureBoxes at once '3' being the bottom and '1' being the top PictureBox. What i need is when the user drags down the form it snaps to the next PictureBoxes position, so it goes down in blocks this is what i have so far which is pretty far from working.

    If Me.Height <= (1079) Then
        Me.Height = (732)

    ElseIf Me.Height <= (732) Then
        Me.Height = (424)
    ElseIf 
    ...
    End If 

I also thought i might be able to figure it out if i new how to create a variable like this

    If Me.Height <= (1079 to 733) Then
        Me.Height = (732)

I know that it isn't the correct syntax but it's kind of the idea

If you can make sense from my not so good description and point me in the right direction/code example i will be most grateful :)

Thank you for your help


Solution

  • You could try using a Select Statement:

    Select Case Me.Height
    
        Case 425 To 732
            Me.Height = 424
    
        Case 733 To 1079
            Me.Height = 732
    
    End Select
    

    To answer your further question. If you are going to use the Form Resize event any animation you have is going to get interesting because as you change the Form Height it will retrigger the Event. Personally if I were you I would stay with your initial idea of snapping to the next height and if you are wanting to animate the Form Height I would seriously look into WPF. But here is the SubRoutine that I said I would show you, I have used three buttons to intiatiate the resizing. Be carefull if you put this in your Form Resize Event if you don't block the event from rerunning the Subroutine it will freeze your computer.

    Public Class Form1
    
        Private Sub Form1_Resize(sender As Object, e As System.EventArgs) Handles Me.Resize
            Me.Text = Me.Height
        End Sub
    
        Public Sub ChangeFormHeight(fromHeight As Integer, toHeight As Integer)
            If fromHeight > toHeight Then
                For newHeight As Integer = fromHeight To toHeight Step -1
                    Me.Height = newHeight
                Next
            Else
                For newHeight As Integer = fromHeight To toHeight
                    Me.Height = newHeight
                Next
            End If
        End Sub
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            ChangeFormHeight(Me.Height, 424)
        End Sub
    
        Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
            ChangeFormHeight(Me.Height, 733)
        End Sub
    
        Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
            ChangeFormHeight(Me.Height, 1080)
        End Sub
    
    End Class