Functions of FormParts

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$pathTop = [Environment]::GetFolderPath('Desktop')

. .\Func_Form.ps1

function GetFormChar{
    param(
        $AryChar,
        $FormOwner    
    )
    $nRow =((shou ($AryChar.count-1) 3)+1) 
    $TableChar = GetNewTable -nCol 3 -nRow $nRow -wCol 40

    # btnsChar[]
    $BtnsChar = @() 
    for($i = 0; $i -lt $AryChar.count; $i++) {
        $BtnsChar += GetNewButton $AryChar[$i]
        [void](SetToTable $TableChar -nCol  ($i % 3 ) -nRow (0 + (shou $i 3)) -ctrl $BtnsChar[$i])
    }

    # Form 定義
    $FormChar1 = GetNewForm -text "Char Input"
    $FormChar1.Owner = $FormOwner
    $FormChar1.ControlBox = $False

    # Form return
    $FormChar1.Controls.Add($TableChar)
    return $FormChar1,$BtnsChar
}
function GetFormNum{
    param(
        $FormOwner    
    )
    $TableNum = GetNewTable -nCol 3 -nRow 4 -wCol 40

    # btnsNum[]
    $btnsNum = @() 
    for($i = 0; $i -lt 12; $i++) {
        $btnsNum += GetNewButton ([string]$i)
        switch ($i) {
        0 { [void]( $btnsNum[$i] | SetToTable $TableNum 0 3 -spanCol 1 ) }
        {$_ -in (1..9)}{
            [void]( $btnsNum[$i] | SetToTable $TableNum (($i-1) % 3 ) (0 + (shou ($i-1) 3)) -spanCol 0 ) }
        {$_ -in (10,11)}{
            [void]( $btnsNum[$i] | SetToTable $TableNum (($i-0) % 3 ) (0 + (shou ($i-1) 3)) -spanCol 0 ) }
        default {}
        }
    }
    $btnsNum[10].text="."
    $btnsNum[11].text="BS"
    

    # Form 定義
    $FormNum1 = GetNewForm -text "Number Input"
    $FormNum1.Owner = $FormOwner
    $FormNum1.ControlBox = $False

    # Form return
    $FormNum1.Controls.Add($TableNum)
    return $FormNum1,$btnsNum
}
function FormNum_ClickChar($FormNumIn){
    $strNum = $FormNumIn.ActiveControl.Text
    return $strNum
}
function FormNum_ClickChar2($FormNumIn,$TxbIn){
    $nSel = $TxbIn.selectionstart
    $lenSel = $TxbIn.SelectionLength
    
    $ret=FormNum_ClickChar $FormNumIn
    switch ($ret){
        "CR" {$TxbIn.text = ""}
        "BS" {            
            if($lenSel -gt 0){
                $TxbIn.text = $TxbIn.text.remove($nSel,$lenSel);
                $TxbIn.selectionstart = $nSel;
            }else{
                if($nSel -le 0){return};
                $TxbIn.text = $TxbIn.text.remove($nSel-1,1);
                $TxbIn.selectionstart = $nSel-1;
            }
        }
        default {
            if($lenSel -gt 0){
                $TxbIn.text = $TxbIn.text.remove($nSel,$lenSel);
                $TxbIn.selectionstart = $nSel;
            }            
            $TxbIn.text = $TxbIn.text.Insert($nSel,$ret);
            $TxbIn.selectionstart = $nSel+1;
        }
    }
}


function GetByFormInputTxb{
    param(
        $TextInLabel1 = "Input Text",
        $FormTitle = "Input Form",
        $InitText,
        [int]$MinLength = 1,
        $FormOwner,
        [int[]]$xy,
        $TxbSpanRow =1,
        [switch]$ShowFormNum,[switch]$ShowFormChar
        )
    if($TxbSpanRow -le 1){$TxbSpanRow=1}

    # Table 定義
    $Table1 = GetNewTable -nCol 2 -nRow (5+$TxbSpanRow-1) -SideMargin 5

    # Controls 定義
    $Lab1= GetNewLabel $TextInLabel1 | SetToTable $Table1 -nRow 0 -spanCol 2
    $Txb1 = GetNewTextBox | SetToTable $Table1 -nRow 1 -spanCol 2 -spanRow $TxbSpanRow
    if($TxbSpanRow -ge 2){ $Txb1.Multiline =$true; $Txb1.WordWrap=$true }
    $Table1.RowStyles[2+$TxbSpanRow-1].Height = 5
    $Btn1 = GetNewButton "OK" | SetToTable $Table1 -nRow (3+$TxbSpanRow-1) -nCol 1
    $LabOut = GetNewLabel "" -MojiSize 9 | SetToTable $Table1 -nRow (4+$TxbSpanRow-1) -spanCol 2

    $Txb1.text = $InitText

    # function for Event
    $ClickOK={
        if(($Txb1.text).Length -ge $MinLength){
            $LabOut.text=""
            $Form1.Close()
            $Form1.DialogResult = $resOK
        }else{
            $LabOut.text="input error"
        } 
    } 

    # Event
    $Btn1.Add_Click($ClickOK)

    # Form 定義
    $Form1 = GetNewForm -text $FormTitle
    $form1.Owner = $FormOwner
    # Form Event
    $FormShown = {        
        if($ShowFormNum){            
            $FormNum.visible=$true
            $FormNum.Location = [string]($Form1.Location.X + $Form1.Width)+","+[string]$Form1.Location.Y
            $BtnsNum[11].select()
        }
        if($ShowFormChar){            
            $FormChar.visible=$true
            if($ShowFormNum){ 
                $FormChar.Location = [string]($Form1.Location.X + $Form1.Width)+","+ `
                    [string]($FormNum.Location.Y + $FormNum.Height)                
            }else{
                $FormChar.Location = [string]($Form1.Location.X + $Form1.Width)+","+[string]$Form1.Location.Y
            }
            $BtnsChar[5].select()
        }
        $Form1.Activate() 
    }
    $FormCloseing = { }    
    $Form1.Add_Shown($FormShown)
    $form1.Add_Closing($CloseForm)
    $Form1.AcceptButton = $Btn1

    # Subform
    # FormNum
    $buf = GetFormNum -FormOwner $Form1
    $FormNum = $buf[0];$BtnsNum = $buf[1];
    foreach($btn in $btnsNum){
        $btn.Add_Click({FormNum_ClickChar2 $FormNum $Txb1}) 
        $btn.Add_DoubleClick({FormNum_ClickChar2 $FormNum $Txb1}) 
    }
    # FormChar
    $buf = GetFormChar -FormOwner $Form1 -AryChar @("A","B","C","D","BS","CR")
    $FormChar = $buf[0];$BtnsChar = $buf[1];
    foreach($btn in $BtnsChar){
        $btn.Add_Click({FormNum_ClickChar2 $FormChar $Txb1}) 
        $btn.Add_DoubleClick({FormNum_ClickChar2 $FormChar $Txb1}) 
    }

    # Form 表示
    $Form1.Controls.Add($Table1)
    if($xy.Count -eq 2){
        $Form1.StartPosition = "Manual"
        $Form1.Location =([string]$xy[0] + "," + [string]$xy[1] )
    }
    $res = $Form1.ShowDialog() 
    if($res -eq $resOK){
        return $Txb1.text 
    }
}

function GetByFormInputCmb{
    param(
        $TextInLabel1 = "Input Combo",
        $FormTitle = "Combo Form",
        [int]$CntCmb = 1,
        $InitText,
        [int]$MinLength = 1,
        $FormOwner,
        $CmbItems,
        [int[]]$xy
        )

    # Table 定義
    $Table1 = GetNewTable -nCol 2 -nRow (4+$CntCmb) -SideMargin 5

    # Controls 定義
    $Lab1= GetNewLabel $TextInLabel1 | SetToTable $Table1 -nRow 0 -spanCol 2
    $Cmbs = @()
    for($i=0;$i -lt $CntCmb;$i++){
        $Cmbs += GetNewCombo -CmbItems $CmbItems | SetToTable $Table1 -nRow (1+$i) -spanCol 2
        $Cmbs[$i].text = $InitText
    }
    if($InitText.Count -eq 1){$Cmbs[0].text = $InitText}
    if($InitText.Count -gt 1){
        for($i=0;$i -lt $InitText.Count;$i++){
            $Cmbs[$i].text = $InitText[$i]
        }
    }

    $Table1.RowStyles[1+$CntCmb].Height = 5
    $Btn1 = GetNewButton "OK" | SetToTable $Table1 -nRow (2+$CntCmb) -nCol 1
    $LabOut = GetNewLabel "" -MojiSize 9 | SetToTable $Table1 -nRow (3+$CntCmb) -spanCol 2

    #endregion

    # function for Event
    $ClickOK={
        if(($Cmbs[0].text).Length -ge $MinLength){
            $LabOut.text=""
            $Form1.Close()
            $Form1.DialogResult = $resOK            
        }else{
            $LabOut.text="input error"
        } 
    } 

    # Event
    $Btn1.Add_Click($ClickOK)

    # Form 定義
    $Form1 = GetNewForm -text $FormTitle
    if($xy.Count -eq 2){
        $Form1.StartPosition = "Manual"
        $Form1.Location =([string]$xy[0] + "," + [string]$xy[1] )
    }

    $form1.Owner = $FormOwner
    # Form Event
    $FormShown = {
        $Form1.Activate()
        foreach($el in  $Cmbs){$el.select()}
        $Btn1.select()    
    }
    $FormCloseing = { }    
    $Form1.Add_Shown($FormShown)
    $form1.Add_Closing($CloseForm)

    # Form 表示
    $Form1.Controls.Add($Table1)
    $res = $Form1.ShowDialog() 
    if($res -eq $resOK){
        if($CntCmb -eq 1){
            return $Cmbs[0].text
        }else{
            return @($Cmbs.text)
        }
         
    }
}