Minh Cường
Minh Cường

Reputation: 347

Form Hide() then Show() always rerendering contents

I have a sample code to create a form with a label with text and a small image.

Add-Type -AssemblyName System.Windows.Forms
[Windows.Forms.Application]::EnableVisualStyles()

$label = [Windows.Forms.Label]@{
    Image  = $image
    Height = $image.Height
    Text   = 'Sample text.'
}
$form = [Windows.Forms.Form]@{
    ControlBox      = $false
    AutoSizeMode    = "GrowAndShrink"
    AutoSize        = $true
    FormBorderStyle = "fixedDialog"
    AutoScaleMode   = "dpi"
    StartPosition   = "centerScreen"
}
$form.Controls.Add($label)
$form.Show()

The form was shown but the content of $label was loaded slower with a white background color. I tried to Hide() then Show() the form and saw it reload the content of $label every time.

Please advise me how I can make the content of $label loaded already when I Show() the form so I will not see the content loading with a white background every time.

Upvotes: 0

Views: 102

Answers (1)

iRon
iRon

Reputation: 23788

I am not able to reproduce the issue but following your description, I would tackle this as follows:
First place the your $form outside the screen's boundary. This will cause the form to be 'invisible' but all events related to the rendering to work as if it was shown on the screen. Than find a event that comes after the concerned items are loaded. I think the event you are looking for is: $form.Activated but you might also consider to first focus your label $form.Add_Activated({$label.focus}) and then use e.g. the validated event of the $label ($label.Add_Validated({...})).
Anyway, upon this event, center your form back on the screen. I am quiet sure that moving your form will not completely re-rendered it as happens with hide() - show() methods:

Add-Type -AssemblyName System.Windows.Forms
[Windows.Forms.Application]::EnableVisualStyles()

$label = [Windows.Forms.Label]@{
    Text   = 'Sample text.'
}
$form = [Windows.Forms.Form]@{
    ControlBox      = $false
    AutoSizeMode    = "GrowAndShrink"
    AutoSize        = $true
    FormBorderStyle = "fixedDialog"
    AutoScaleMode   = "dpi"
    StartPosition   = "manual"
    Left            = -9999
    Top             = -9999
}
$form.Add_Activated({
    $Screen = [Windows.Forms.Screen]::PrimaryScreen.Bounds
    $Form.Left = ($Screen.Width  - $Form.Width)  / 2
    $Form.Top  = ($Screen.Height - $Form.Height) / 2
})
$form.Controls.Add($label)
$form.Show()

Upvotes: 1

Related Questions