none
How to handle size of form and other tools to make it appropriately in all screen using PowerShell? RRS feed

  • Question

  • Hi, anyone can help me please how to fix my problem. I really struggle with the size and location. In my work screen it show good all the tools. But once I execute in other screen different resolution, all my tools are mesh up. :( Please help me. Thank you.

    Add-Type -AssemblyName PresentationFramework
    Add-Type -AssemblyName System.Windows.Forms
    [reflection.assembly]::LoadWithPartialName( "System.Windows.Forms")
    [reflection.assembly]::LoadWithPartialName( "System.Drawing")
    [System.Windows.Forms.Application]::EnableVisualStyles()
    
    $Form                            = New-Object system.Windows.Forms.Form
    $Form.text                       = "."
    $Form.BackColor                  = "#ffffff"
    $Form.TopMost                    = $false
    $Form.FormBorderStyle            = "FixedDialog"
    $Form.MaximizeBox                = $false
    $Form.startposition              = "centerscreen"
    # $Form.WindowState                = 'Maximized'
    
    $Label1                          = New-Object system.Windows.Forms.Label
    $Label1.text                     = "Please:"
    $Label1.AutoSize                 = $true
    $Label1.width                    = 25
    $Label1.height                   = 10
    $Label1.location                 = New-Object System.Drawing.Point(35,50)
    $label1.AutoSize                 = $true
    $label1.Font                     = $SubTextFont
    
    $Label2                          = New-Object system.Windows.Forms.Label
    $Label2.text                     = "Information"
    $Label1.AutoSize                 = $true
    $Label2.width                    = 25
    $Label2.height                   = 10
    
    $Label2.AutoSize                 = $true
    $Label2.Font                     = $SubTextFont
    
    $Button2                         = New-Object system.Windows.Forms.Button
    $Button2.BackColor               = "#0095d9"
    $Button2.ForeColor               = "#ffffff"
    $Button2.text                    = "Refresh"
    $Button2.width                   = 70
    $Button2.height                  = 25
    $Button2.UseCompatibleTextRendering = $True
    $Button2.UseVisualStyleBackColor = $False
    $Button2.AutoSize                = $true
    $Button2.Add_Click({
        # Remove all items from the ComboBox
        $Combo.Items.Clear()
        # Repopulate the ComboBox, just like when it was created
        $Combo.Items.AddRange(@(Get-ChildItem -Directory ".\").Name)
    
    })
    
    $Button3                         = New-Object system.Windows.Forms.Button
    $Button3.BackColor               = "#0095d9"
    $Button3.ForeColor               = "#ffffff"
    $Button3.text                    = "Delete"
    $Button3.width                   = 70
    $Button3.height                  = 25
    $Button3.UseCompatibleTextRendering = $True
    $Button3.UseVisualStyleBackColor = $False
    $Button3.AutoSize                = $true
    $Button3.Add_Click({
    $UserResponse = [System.Windows.Forms.MessageBox]::Show("Are you sure to delete?","Message" , 4)
    
    if ($UserResponse -eq "YES" ) 
    {
        #Yes activity
        $getfile = ($CheckedListBox.CheckedItems).Name
        [int]$Get = ($CheckedListBox.CheckedItems).Count
        $Get
        $getfile | Out-File .\SelectFile.txt -Force
        $Form.Close()
    } 
    else 
    { 
        #No activity
    }     
    })
    $Button3.Visible = $false
    
    $Button4                         = New-Object system.Windows.Forms.Button
    $Button4.BackColor               = "#0095d9"
    $Button4.ForeColor               = "#ffffff"
    $Button4.text                    = "Done"
    $Button4.width                   = 70
    $Button4.height                  = 25
    $Button4.UseCompatibleTextRendering = $True
    $Button4.UseVisualStyleBackColor = $False
    $Button4.AutoSize                = $true
    $Button4.Add_Click({$Form.Close()})
    $Button4.Visible = $false
    $Button4.Font                   = $SubTextFont
    
    $Panel = New-Object System.Windows.Forms.TableLayoutPanel
    $panel.Dock = "Fill"
    $panel.ColumnCount = 1
    $panel.RowCount = 1
    $panel.CellBorderStyle = "single"
    $panel.ColumnStyles.Add((new-object System.Windows.Forms.ColumnStyle([System.Windows.Forms.SizeType]::Percent, 100)))
    $panel.RowStyles.Add((new-object System.Windows.Forms.RowStyle([System.Windows.Forms.SizeType]::Percent, 100)))
    
    $Groupbox1                       = New-Object system.Windows.Forms.Groupbox
    $Groupbox1.text                  = "."
    $Groupbox1.Padding               = New-Object -TypeName System.Windows.Forms.Padding -ArgumentList (0,5,5,0)
    
    
    $Combo                        = New-Object system.Windows.Forms.ComboBox
    $Combo.width                  = 190
    $Combo.height                 = 35
    $Combo.AutoSize               = $true
    $Combo.DropDownStyle          = "DropDownList"
    $Combo.Font                   = $SubTextFont
    $Combo.Items.AddRange(@(Get-ChildItem -Directory ".\").Name)
    
    $SelectedFile= {
    #     Write-Host $Combo.SelectedItem
        $files = [collections.arraylist](Get-ChildItem $Combo.SelectedItem -File *.que)
        $checkedListBox.DataSource = $files
        $checkedListBox.DisplayMember = 'Name'
        $Button3.Visible = $true
        $Button4.Visible = $true
    }
    $Combo.add_SelectedIndexChanged($SelectedFile)
    $Combo.DisplayMember = 'Name'
    
    
    $checkedListBox = New-Object System.Windows.Forms.CheckedListbox
    $checkedListBox.CheckOnClick = $true
    $checkedListBox.Enabled = $true
    $checkedListBox.BackColor = "#ffffff"
    
    
    $Groupbox1.Controls.Add($checkedListBox)
    $form.controls.add($Panel)
    $panel.controls.AddRange(@($Groupbox1))
    $Groupbox1.controls.AddRange(@($Combo, $Label1, $Button2, $Label2, $Button3, $Button4))
    [void]$Form.Show()
    
    $Width = [System.Windows.Forms.Screen]::AllScreens.bounds.width
    $Height = [System.Windows.Forms.Screen]::AllScreens.bounds.height
    $w = ($Width[0] / 2) * 1.1
    $h = ($Width[0] / 3)
    
    $Form.Width = $w
    $Form.Height = $h
    $Groupbox1.Dock = "fill"
    $Label2.location = New-Object System.Drawing.Point(35,150)
    
    $w_lb = $w / 3
    $h_lb = $h - 50
    $checkedListBox.Location = "35,180"
    $checkedListBox.Size = "$h_lb, $w_lb"
    
    $h_b3 = $h - 90
    $w_b3 = $h_lb - 150
    $w_b4 = $w_b3 + 110
    
    $h_b2 = $w / 4
    $w_b2 = $h / 8
    
    $h_cb = (35 - $w) + $w
    $h_cb
    $Button3.location                = New-Object System.Drawing.Point($w_b3,$h_b3)
    $Button4.location                = New-Object System.Drawing.Point($w_b4,$h_b3)
    $Button2.location                = New-Object System.Drawing.Point($h_b2,$w_b2)
    $Combo.location                  = New-Object System.Drawing.Point($h_cb,$w_b2)
    
    # $Groupbox1.location              = New-Object System.Drawing.Point(35,110)
    # $Groupbox1.size                  = New-Object System.Drawing.Size($g2w,$g2h)
    $Groupbox1.AutoSize              = $true
    
    $Form.Visible = $false
    [void]$Form.ShowDialog()

    Monday, December 2, 2019 10:14 AM

All replies