WFControlsLIB.psm1

<#
    My Function
#>


Add-Type -AssemblyName System.Windows.Forms

function form {
    param(
        [System.Drawing.Size]$size
        [string]$startposition = "CenterScreen",
        [bool]$maximizebox,
        [bool]$controlbox = $true,
        [string]$formborderstyle = "FixedSingle"

    )
    $form = New-Object System.Windows.Forms.Form
    $form.Size = New-Object System.Drawing.Point($size)
    $form.StartPosition = $startposition
    $form.MaximizeBox = $maximizebox
    $form.ControlBox = $controlbox

    return $form
}
function button {
    param(
        [string]$text,
        [string]$font,
        [string]$textalign = "MiddleCenter",
        [bool]$autosize = $true,
        [System.Drawing.Size]$size = (25,25)
        [System.Drawing.Point]$location,
        [string]$backcolor,
        [string]$flatstyle = "Flat",
        [int]$flatappearance_bordersize,
        [string]$imagepath,
        [string]$imagelayout = "Center",
        [bool]$visible = $true,
        [scriptblock]$onclick

    )
    
    $button = New-Object System.Windows.Forms.Button
    $button.Text = $text
    $button.Font = $font
    $button.TextAlign = $textalign
    $button.AutoSize = $autosize
    $button.Size = New-Object System.Drawing.Size($size)
    $button.Location = New-Object System.Drawing.Point($location)
    $button.BackColor = $backcolor
    $button.FlatStyle = $flatstyle
    $button.FlatAppearance.BorderSize = $flatappearance_bordersize
    $button.Visible = $visible

    if($size){
        $autosize = $false
    }
  
    if ($imagepath) {
        $image = [System.Drawing.Image]::FromFile($imagepath)
        $button.BackgroundImage = $image
        $button.BackgroundImageLayout = $imagelayout
    }
    
    if($onclick) {
        $button.Add_Click($OnClick)
    }

    return $button
}
function label{
    param(
        [string]$text,
        [string]$font,
        [bool]$autosize = $true,
        [System.Drawing.Size]$size = (100,25)
        [System.Drawing.Point]$location,
        [bool]$visible = $true
    )

    $label = New-Object System.Windows.Forms.Label
    $label.Text = $text
    $label.font = $font
    $label.AutoSize = $autosize
    $label.Size = New-Object System.Drawing.Size($size)
    $label.Location = New-Object System.Drawing.Point($location)
    $label.Visible = $visible
    
    if($size){
        $autosize = $false
    }

    return $label
}
function textbox {
    param(
        [string]$text,
        [string]$font,
        [string]$textalign = "Left",
        [string]$borderstyle = "FixedSingle",
        [bool]$multiline,
        [bool]$autosize = $true,
        [System.Drawing.Size]$size = (100,25)
        [System.Drawing.Point]$location,
        [bool]$visible = $true
    )

    $textbox = New-Object System.Windows.Forms.TextBox
    $textbox.Text = $text
    $textbox.Font = $font
    $textbox.TextAlign = $textalign
    $textbox.BorderStyle = $borderstyle
    $textbox.Multiline = $multiline
    $textbox.Size = New-Object System.Drawing.Size($size)
    $textbox.Location = New-Object System.Drawing.Point($location)
    $textbox.Visible = $visible

    if($size){
        $autosize = $false
    }

    return $textbox
}
function checkbox{
    param(
        [System.Drawing.Point]$location,
        [bool]$checked,
        [bool]$visible = $true,
        [scriptblock]$checkedchanged
    )

    $checkbox = New-Object System.Windows.Forms.CheckBox
    $checkbox.Location = New-Object System.Drawing.Point($location)
    $checkbox.Checked = $checked
    $checkbox.Visible = $visible
        if($checkedchanged) {
            $checkbox.Add_CheckedChanged($checkedchanged)
        }

    return $checkbox
}
function combobox{
    param(
        [string]$dropdownstyle = "DropDown",
        [string]$font,
        [bool]$autosize = $true,
        [System.Drawing.Size]$size = (100,25)
        [System.Drawing.Point]$location,
        [string[]]$items,
        [string]$flatstyle = "Flat",
        [string]$defaultselection,
        [bool]$visible = $true
    )
    
    $combobox = New-Object System.Windows.Forms.ComboBox
    $combobox.Font = $font
    $combobox.Size = New-Object System.Drawing.Size($size)
    $combobox.AutoSize = $autosize
    $combobox.Location = New-Object System.Drawing.Point($location)
    $combobox.Items.AddRange(@($items))
    $combobox.FlatStyle = $flatstyle
    $combobox.DropDownStyle = [System.Windows.Forms.ComboBoxStyle]::$dropdownstyle
    $combobox.Visible = $visible
    $defaultcbindex = $combobox.FindStringExact($defaultselection)

    if($size){
        $autosize = $false
    }

    if($defaultcbindex -ge 0){
            $combobox.SelectedIndex = $defaultcbindex
    }
    
    
    return $combobox
}
function tab{
    param(
        [System.Drawing.Size]$size = (100,25)
        [System.Drawing.Point]$location,
        [bool]$visible = $true
    )
    
    $tab = New-Object System.Windows.Forms.TabControl
    $tab.Size = New-Object System.Drawing.Size($size)
    $tab.Location = New-Object System.Drawing.Point($location)
    $tab.Visible = $visible

    return $tab
}
function tabpage{
    param(
        [string]$text,
        [bool]$visible = $true
    )

    $tabpage = New-Object System.Windows.Forms.TabPage
    $tabpage.Text = $text
    $tabpage.Visible = $visible

    return $tabpage
}
function checkedlistbox{
    param (
        [bool]$autosize = $true,
        [System.Drawing.Size]$size = (100,25)
        [System.Drawing.Point]$location,
        [string[]]$items
    )

    $checkedlistbox = New-Object System.Windows.Forms.CheckedListBox
    $checkedlistbox.Size = New-Object System.Drawing.Size($size)
    $checkedlistbox.Location = New-Object System.Drawing.Point($location)
    $checkedlistbox.AutoSize = $autosize
        
    foreach($item in $items){
            $checkedlistbox.items.addrange(@("$item"))
    }

    if($size){
        $autosize = $false
    }


    return $checkedlistbox
}
function tooltip{
    param(
        [System.Windows.Forms.Control]$control,
        [string]$text
    )

    $tooltip = New-Object System.Windows.Forms.Tooltip
    $tooltip.SetToolTip($control, $text)
}
function listbox {
    param(
        [bool]$autosize = $true,
        [System.Drawing.Size]$size = (100,25)
        [System.Drawing.Point]$location,
        [string]$font,
        [int]$itemheight,
        [string[]]$items,
        [hashtable]$controlgroup,
        [int]$selecteditem = 0
    )

    $listbox = New-Object System.Windows.Forms.ListBox
    $listbox.Size = New-Object System.Drawing.Size($size)
    $listbox.Location = New-Object System.Drawing.Point($location)
    $listbox.Font = $font
    $listbox.ItemHeight = $itemheight
    $listbox.Items.AddRange(@($items))
    $listBox.SelectedIndex = $selecteditem

    if($size){
        $autosize = $false
    }

    $listbox.add_SelectedIndexChanged({
        $selecteditem = $listbox.SelectedItem

        foreach ($control in $controlgroup.Values) {
            $control | ForEach-Object { $_.Visible = $false }
        }

        foreach ($control in $controlgroup[$selecteditem]) {
            $control | ForEach-Object { $_.Visible = $true }
        }
    })

    $form.add_Load({
    $selectedItem = $listBox.SelectedItem

    foreach ($control in $controlgroup.Values) {
        $control | ForEach-Object { $_.Visible = $false }
    }

    foreach ($control in $controlgroup[$selectedItem]) {
        $control | ForEach-Object { $_.Visible = $true }
    }
})


    return $listbox
}