C
C
Chingiz Khalafov2020-12-03 14:41:02
PowerShell
Chingiz Khalafov, 2020-12-03 14:41:02

How to dynamically calculate button position in powershell?

Good afternoon, dear ones!

I have this code:

Function Popup ($PopupMessage){
        $PopupWin = New-Object System.Windows.Forms.Form
        $PopupWin.StartPosition  = "CenterScreen"
        $PopupWin.Text = "Sending password via SMS"
        $PopupWin.Width = 200
        $PopupWin.Height = 120
        $PopupWin.ControlBox = 0
        $PopupWin.AutoSize = 1
        $PopupWin.AutoSizeMode = "GrowAndShrink"
        #Border style and font.
        $PopupWin.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::Fixed3D
        $PopupWin.Font = New-Object System.Drawing.Font("Verdana",11)		


        $PopupWinOKButton = New-Object System.Windows.Forms.Button
        $PopupWinOKButton.add_click({ $PopupWin.Close() })
        $PopupWinOKButton.Text = "Close"
        $PopupWinOKButton.Size = New-Object System.Drawing.Size(90,25)
  $PopupWinOKButton.Location = New-Object System.Drawing.Point(($PopupWin.Width*1),50)

        $PopupLabel = New-Object System.Windows.Forms.Label
        $PopupLabel.Text = $PopupMessage
        $PopupLabel.AutoSize = 1
        $PopupLabel.Location  = New-Object System.Drawing.Point(10,10)


        $PopupWin.Controls.Add($PopupLabel)
        $PopupWin.Controls.Add($PopupWinOKButton)
        $PopupWin.ShowDialog() | Out-Null
    }


So here it is necessary that the position of the Close button is calculated dynamically, based on the size of the window, and is always in the middle. Tried with Anchor, didn't work. The size of the window itself depends on the length of the PopupLabel.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MaxKozlov, 2020-12-03
@MaxKozlov

Substitute here
$PopupWinOKButton.Location = New-Object System.Drawing.Point(($PopupWin.Width*1),50)
the calculated size instead of the mysterious $PopupWin.Width*1
In general, anchors should work

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question