VB .Net form windowState to keep same

Joined
Sep 4, 2022
Messages
150
Reaction score
16
Hello helpers !

I try to improve a VB .Net software by using a switch between normal/maximized window.

As triggering a scale on Controls is easy and done,
I am stuck when changing from Form1 to Form2 and "keeping current" the windowState ( 0:normal, or 2:maximized ).

It's a logical problem ( hard time this one ) and maybe a mischoice about 'event'.


Code:
Public Sub set_form_scaling()

  'del is the current and active Form

  If del.WindowState = 0 Then ' normal window

     apply_resizing({(1215 / Screen.PrimaryScreen.Bounds.Width), (690 / Screen.PrimaryScreen.Bounds.Height), 12}) ' shrink apply

  End If

  If del.WindowState = 2 Then ' maximized window

     apply_resizing({(Screen.PrimaryScreen.Bounds.Width / 1215), (Screen.PrimaryScreen.Bounds.Height / 690), 18}) ' grow apply

  End If

  If del.WindowState = 1 Then ' minimized window

     ' no action

  End If

End Sub

I have lot of try already , using windowState , using a token about the current form, few events.

About event , which one will be the more relevant to fire this operation on a Form ?
I try with events : Layout / resize / shown... there is always one fork failing
I miss something but can not find the good one.


Thanks in advance ! :)
 
Joined
Jul 4, 2023
Messages
575
Reaction score
77
Have you tried using a shared module to store the WindowState across forms?

Code:
Public Module GlobalState
    ' Variable to hold the current WindowState
    Public CurrentWindowState As FormWindowState = FormWindowState.Normal
End Module
Code:
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Apply the shared WindowState
        Me.WindowState = GlobalState.CurrentWindowState
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' Update the shared WindowState variable
        GlobalState.CurrentWindowState = Me.WindowState

        ' Open Form2
        Dim form2 As New Form2()
        form2.Show()

        Me.Hide() ' Optional: Hide Form1
    End Sub
End Class
Code:
Public Class Form2

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Apply the shared WindowState
        Me.WindowState = GlobalState.CurrentWindowState
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' Update the shared WindowState
        GlobalState.CurrentWindowState = Me.WindowState

        ' Return to Form1
        Dim form1 As New Form1()
        form1.Show()

        Me.Close() ' Close Form2
    End Sub
End Class
1737988963594.png

1737988866474.png



BTW,
you can use FormWindowState Enum
Code:
del.WindowState = 0
to
Code:
del.WindowState = FormWindowState.Normal
 
Joined
Sep 4, 2022
Messages
150
Reaction score
16
Thank you VBService ,

I was stuck by my 'Forms Handler', and the code to fix was apart.
It's now done.

For sure the scaling of all my windows is ok now.


C-like:
// WindowState enum values :

windowState.Maximized = 2
windowState.Minimized = 1
windowState.Normal = 0

// all those can be set by 'const integer' use.
windowState = 0 // set the form in 'Normal' State

// giving "myForm.windowState = 0|1|2"

to keep the display state :
I use one 'token' as clue
and 'switching states'
and
'loading' a Form are the 2 events triggering the 'scaling'.

Thank you, It's now fixed !
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,249
Messages
2,571,244
Members
47,876
Latest member
Kiptechie

Latest Threads

Top