code will only run in ISE without error

Welcome Forums General PowerShell Q&A code will only run in ISE without error

This topic contains 6 replies, has 4 voices, and was last updated by

 
Participant
4 months ago.

  • Author
    Posts
  • #128888

    Participant
    Topics: 24
    Replies: 34
    Points: 28
    Rank: Member

    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

    
    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
    
    

     

  • #131103

    Participant
    Topics: 1
    Replies: 1267
    Points: 1,395
    Helping Hand
    Rank: Community Hero

    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.

  • #131151

    Participant
    Topics: 2
    Replies: 876
    Points: 1,354
    Helping Hand
    Rank: Community Hero

    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.

    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.

  • #131162

    Participant
    Topics: 0
    Replies: 38
    Points: 197
    Helping Hand
    Rank: Participant

    You have to load one more assembly...

    Add-Type -AssemblyName System.Drawing
  • #131169

    Participant
    Topics: 2
    Replies: 876
    Points: 1,354
    Helping Hand
    Rank: Community Hero

    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.

  • #131178

    Participant
    Topics: 0
    Replies: 38
    Points: 197
    Helping Hand
    Rank: Participant

    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.

    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.

  • #131264

    Participant
    Topics: 2
    Replies: 876
    Points: 1,354
    Helping Hand
    Rank: Community Hero

    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.

The topic ‘code will only run in ISE without error’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort