Home > Back-end >  How to make a textbox function in a form?
How to make a textbox function in a form?

Time:07-10

I have an application I am making with multiple text boxes and I am trying to clean it up by making a text box function. However the name parameter itself needs to return a variable name and I am just not quite sure how to do that. I tried giving the parameter the [psvariable] type but that does not seem to be working.

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()


$Form                                 = New-Object system.Windows.Forms.Form
$Form.StartPosition                   = "CenterScreen"
$Form.Size                            = New-Object System.Drawing.Point(415,838)
$Form.text                            = "Test Form"
$Form.TopMost                         = $false
$Form.MaximumSize                     = $Form.Size
$Form.MinimumSize                     = $Form.Size


function textBox {
[CmdletBinding()]
param (
    [Parameter (Mandatory = $True)]
    [psvariable]$name,

    [Parameter (Mandatory = $True)]
    [string]$ml,

    [Parameter (Mandatory = $True)]
    [int]$lx,

    [Parameter (Mandatory = $True)]
    [int]$ly,

    [Parameter (Mandatory = $True)]
    [string]$text
)

$name                        = New-Object system.Windows.Forms.TextBox
$name.multiline              = $ml
$name.Size                   = New-Object System.Drawing.Size(300,60)
$name.location               = New-Object System.Drawing.Point($lx,$ly)
$name.Font                   = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$name.Text                   = $text
}

textBox -name '$source' -ml $True -lx 15 -ly 100 -text "test it"                  

$Form.controls.AddRange(@($source))

[void]$Form.ShowDialog()
exit

CodePudding user response:

It's unclear why is the $name parameter there in your function to begin with, I don't see any use for it. The other problem is that your function is not returning anything and your function invocation is not capturing anything either.

Add-Type -AssemblyName System.Windows.Forms, System.Drawing
[System.Windows.Forms.Application]::EnableVisualStyles()

# Form code here

function textBox {
    [CmdletBinding()]
    param (
        [Parameter (Mandatory = $True)]
        [bool] $ml,

        [Parameter (Mandatory = $True)]
        [int] $lx,

        [Parameter (Mandatory = $True)]
        [int] $ly,

        [Parameter (Mandatory = $True)]
        [string] $text
    )

    [Windows.Forms.TextBox]@{
        Multiline = $ml
        Size      = [Drawing.Size]::new(300,60)
        Location  = [Drawing.Point]::new($lx, $ly)
        Font      = [Drawing.Font]::new('Microsoft Sans Serif', 10)
        Text      = $text
    }
}

# Capture here
$txtBox = textBox -ml $True -lx 15 -ly 100 -text "test it"
$Form.Controls.AddRange(@($txtBox))

[void] $Form.ShowDialog()

To clarify further, the $Name Parameter was constraint to be a PSVariable:

[psvariable] $Name = $null

Hence when, in your function's body, you try to assign an instance different than a PSVariable:

$name = New-Object System.Windows.Forms.TextBox

You would receive this error, which is basically telling you that PowerShell cannot convert an instance of TextBox to the constraint type:

Cannot convert the "System.Windows.Forms.TextBox, Text: " value of type "System.Windows.Forms.TextBox" to type "System.Management.Automation.PSVariable".

  • Related