PowerShell - Windows form Label and Calendar output different

Alex Un 1 Reputation point
2021-10-11T08:57:44.857+00:00

Hi all

Did anyone have same experience about,
when the windows form scripts run in PowerShell ISE, the pixel is good.
After save as .ps1 file and directly "right click -> run with PowerShell ", the pixel go down. And also, the size will change.

Here is the output and my scripts.
139451-inside-powershell-ise.jpg

139436-right-click-run-with-powershell.jpg

---------------------------------------------------------------------------------

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$S_Form = New-Object Windows.Forms.Form -Property @{  
    StartPosition = [Windows.Forms.FormStartPosition]::CenterScreen  
    #Size          = New-Object Drawing.Size 200, 200 #size (280,250)  
    Text          = "Select a Start Date."  
    TopMost       = $true  
    AutoSize      = $true  
}  

 $S_Label = New-Object windows.forms.label -property @{  
    Location      = New-Object drawing.point 0,0  
    Font          = New-Object System.Drawing.Font ("Times New Roman",14)  
    #Size          = New-Object drawing.size 250,30  
    AutoSize      = $true  
    Text          = "Please Select Start Time."  
}  
$S_Form.Controls.Add($S_Label)  

$S_Calendar = New-Object Windows.Forms.MonthCalendar -Property @{  
    Location          = New-Object drawing.point 0,($S_Label.Size.Height)  
    #Size              = New-Object drawing.size 200,200  
    AutoSize          = $true  
    ShowTodayCircle   = $false  
    ShowToday         = $false  
    MaxSelectionCount = 1  
}  
$S_Form.Controls.Add($S_Calendar)  

write-host "label size is " $S_Label.size  
write-host "Calendar size is "$S_Calendar.Size  

$result = $S_Form.ShowDialog()

------------------------------------------------------------------------------

I tried to fixed the label and calendar size, the label can work but the calendar still getting smaller.

My question is, How can let the size don't change ?

Best regards,
Thank you

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Limitless Technology 39,916 Reputation points
    2021-10-13T09:00:56.42+00:00

    Hello @Alex Un ,

    Thank you for your question.

    I recommend that you consult the article below because you may have passed on some unnoticed information on how to create this feature correctly:

    https://learn.microsoft.com/en-us/powershell/scripting/samples/creating-a-custom-input-box?view=powershell-7.1

    You can also use the "MonthCalendar Class" feature in the article below, I believe it can be useful for you:

    https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.monthcalendar?view=windowsdesktop-5.0

    ------------------------------------------------------------------------------------------------------------------------------------

    If the answer is helpful, please vote positively and accept as an answer.

    0 comments No comments

  2. Alex Un 1 Reputation point
    2021-10-18T03:09:15.583+00:00

    Hello LimitlessTechnology-2700,

    Thank you for your information.

    This two articles, I read before and now I fixed the size and location to avoid the error.

    BRs

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.