when I run the following code it seems to run in the ISE, but when I run it a regular shell I get an error stating the property font can’t be found
[pre]
Add-Type -AssemblyName System.Windows.Forms
$myinput = Read-Host -Prompt “enter your message”
$form = New-Object System.Windows.Forms.Form
$form.size = “518,518”
$form.TopMost = $true
$form.BackColor = “yellow”
$form.StartPosition = ‘CenterScreen’
$Font = New-Object System.Drawing.Font(“arial”,25,[System.Drawing.FontStyle]::regular)
$Form.Controls.Add($mytextbox)
$mytextbox.Font = $Font
$mytextbox = New-Object System.Windows.Forms.Textbox
$mytextbox.Size = New-Object System.Drawing.Size(518, 100)
$mytextbox.WordWrap = $True
$mytextbox.Text = $myinput
#$mytextbox.ScrollBar = [System.Windows.Forms.ScrollBars]::Vertical
$mytextbox.multiline = $true
#this starts the show
$form.ShowDialog() | Out-Null
[/pre]
Olaf
December 21, 2018, 7:41pm
2
Sorry for the late answer.
Am I wrong or do you have to define the text box font before you use it?
Add-Type -AssemblyName System.Windows.Forms
$myinput = Read-Host -Prompt “enter your message”
$form = New-Object System.Windows.Forms.Form
$form.size = “518,518”
$form.TopMost = $true
$form.BackColor = “yellow”
$form.StartPosition = ‘CenterScreen’
$Font = New-Object System.Drawing.Font(“arial”,25,[System.Drawing.FontStyle]::regular)
$mytextbox.Font = $Font
$mytextbox = New-Object System.Windows.Forms.Textbox
$mytextbox.Size = New-Object System.Drawing.Size(518, 100)
$mytextbox.WordWrap = $True
$mytextbox.Text = $myinput
#$mytextbox.ScrollBar = [System.Windows.Forms.ScrollBars]::Vertical
$mytextbox.multiline = $true
$Form.Controls.Add($mytextbox)
#this starts the show
$form.ShowDialog() | Out-Null
… works for me this way.
This appears you are trying to do this all by hand, manually.
You can make you life easier by using a GUI from designer. Either download and use Visual Studio Community Edition.
https://visualstudio.microsoft.com/vs/community
This is full Visual Studio not Visual Studio Code. Then use these instructions.
https://foxdeploy.com/2015/04/10/part-i-creating-powershell-guis-in-minutes-using-visual-studio-a-new-hope
Or if you don’t wan to go through all that level of a learning curve, just use this site.
https://poshgui.com
Try this replica of your firm quickly pulled together from there. Tested in ISE, VSCode, and console host and all worked as designed.
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$myinput = Read-Host -Prompt "enter your message"
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '400,400'
$Form.text = "Form"
$Form.BackColor = "#f8e71c"
$Form.TopMost = $false
$form.StartPosition = 'CenterScreen'
$TextBox1 = New-Object system.Windows.Forms.TextBox
$TextBox1.multiline = $false
$TextBox1.width = 204
$TextBox1.height = 20
$TextBox1.location = New-Object System.Drawing.Point(20,20)
$TextBox1.Font = 'Arial,25'
$TextBox1.Text = $myinput
$Form.controls.AddRange(@($TextBox1))
$form.ShowDialog() | Out-Null
Also, WinForms which is what you are using and what this online site does, the industry is using WPF (Windows Presentation Foundation / XAML) GUI development instead.
https://learn-powershell.net/2012/09/13/powershell-and-wpf-introduction-and-building-your-first-window
Yet, nothing wrong with learning both.
palhan
December 22, 2018, 11:09am
4
You have to load one more assembly…
Add-Type -AssemblyName System.Drawing
phansen : Add-Type -AssemblyName System.Drawing
Not something that is really needed, to resolve the issue the OP is seeing, as demo’s by the tested example I posted.
palhan
December 22, 2018, 3:20pm
6
[quote quote=131169]
phansen : Add-Type -AssemblyName System.Drawing
Not something that is really needed, to resolve the issue the OP is seeing, as demo's by the tested example I posted.[/quote]
I think it will resolve that specific problem. “Font” is unknown… because the assembly is not loaded. ISE loads more assemblies by default than the regular shell. My 5.1 shell on Windows 10 loads both of them by default, but it appears to not be the case here. If I start verion 2, none of them are loaded by default.
Edit: If I look closer, Add-Type -AssemblyName System.Windows.Forms seems to also add System.Drawing. So the problem might be something else.
This font thing can and does still occur using that ‘Add-Type -AssemblyName System.Drawing’ being added. How do I know, been there done that.