locked
the woes of optimization RRS feed

  • Question

  • $form = New-Object Windows.Forms.Form
    $form.text = "Chess Bored"
    $form.size = New-Object Drawing.Point 625,725
    
    $x = 25
    $y = 25
    
    $label = @() #array
    For($i=0;$i -lt 8;$i++) {
        For($j=0;$j -lt 8;$j++) {
            $x = 25 + $i * 70
            $y = 25 + $j * 80
            $label = New-Object Windows.Forms.Label
            $label.Location = New-Object Drawing.Point $x,$y
            $label.Size = New-Object Drawing.Point 70,80
            $label.Text = "($i,$j)"
            $label.BorderStyle = [System.Windows.Forms.BorderStyle]::FixedSingle
            $label.BackColor = $c
            $label.add_Click({Write-Host "$Label.Text"})
            $form.controls.add($label)
        }
    }
    
    $form.ShowDialog()
    
    

    The above code does create a 8x8 set of labels (0,0) .. (7,7) but when I click on a label I get only "(7,7)". and yes I don't know forms. so help me please, I would like the Add_Click function to return the label text (x,y) associated with the one I clicked.
    Sunday, March 2, 2014 5:30 AM

Answers

  •  $label.add_Click({Write-Host $this.Text})

    Stop with the quoting of everything that isn't nailed down.  It is annoying and causes problems.


    ¯\_(ツ)_/¯


    • Edited by jrv Sunday, March 2, 2014 9:44 AM
    • Marked as answer by Powershell Beginner Sunday, March 2, 2014 5:21 PM
    Sunday, March 2, 2014 6:08 AM
  • here is a more comprehensive example;

    Add-Type -AssemblyName System.Windows.Forms
    Add-Type -AssemblyName System.Drawing
    
    #create form
    $form = New-Object Windows.Forms.Form
    $form.text = "Chess Bored"
    $form.size = New-Object Drawing.Point 825,725
    
    # buttonOK
    #
    $buttonOk=New-Object System.Windows.Forms.Button
    $buttonOK.Anchor = 'Bottom, Right'
    $buttonOK.DialogResult = 'OK'
    $buttonOK.Location = '650, 90'
    $buttonOK.Name = "buttonOK"
    $buttonOK.Size = '75, 23'
    $buttonOK.TabIndex = 0
    $buttonOK.Text = "OK"
    $buttonOK.UseVisualStyleBackColor = $True
    $form.controls.add($buttonOK)
    
    #label for showing hits
    $hit=New-Object Windows.Forms.Label
    $hit.Text = 'Not hit yet'
    $hit.font=New-Object System.Drawing.Font([System.Drawing.FontFamily]::GenericSansSerif,14.0,[System.Drawing.FontStyle]::Bold)
    $hit.Location = New-Object Drawing.Point 650,50
    $form.controls.add($hit)
    
    $Form_StateCorrection_Load={
         #Correct the initial state of the form to prevent the .Net maximized form issue
         $form.WindowState = $InitialFormWindowState
    }
    
    # default label event
    $action={
        $hit.Text=$this.Name
        $this.BackColor='blue'
    }
    
    # generate label matrix
    For($i=0;$i -lt 8;$i++) {
       for($j=0;$j -lt 8;$j++){
            $x = 25 + $i * 70
            $y = 25 + $j * 70
            $label = New-Object Windows.Forms.Label
            $label.Location = New-Object Drawing.Point $x,$y
            $label.Size = New-Object Drawing.Point 70,70
            $label.Name = "[$i,$j]"
            $label.BorderStyle = [System.Windows.Forms.BorderStyle]::FixedSingle
            $label.BackColor = 'white'
            $label.add_Click($action)
            $form.controls.add($label)
       }
    }
    
    #Init the OnLoad event to correct the initial state of the form
    $InitialFormWindowState=$form.WindowState
    $form.add_Load($Form_StateCorrection_Load)
    
    $form.ShowDialog()
    


    ¯\_(ツ)_/¯

    Sunday, March 2, 2014 11:13 AM

All replies

  •  $label.add_Click({Write-Host $this.Text})

    Stop with the quoting of everything that isn't nailed down.  It is annoying and causes problems.


    ¯\_(ツ)_/¯


    • Edited by jrv Sunday, March 2, 2014 9:44 AM
    • Marked as answer by Powershell Beginner Sunday, March 2, 2014 5:21 PM
    Sunday, March 2, 2014 6:08 AM
  • here is a more comprehensive example;

    Add-Type -AssemblyName System.Windows.Forms
    Add-Type -AssemblyName System.Drawing
    
    #create form
    $form = New-Object Windows.Forms.Form
    $form.text = "Chess Bored"
    $form.size = New-Object Drawing.Point 825,725
    
    # buttonOK
    #
    $buttonOk=New-Object System.Windows.Forms.Button
    $buttonOK.Anchor = 'Bottom, Right'
    $buttonOK.DialogResult = 'OK'
    $buttonOK.Location = '650, 90'
    $buttonOK.Name = "buttonOK"
    $buttonOK.Size = '75, 23'
    $buttonOK.TabIndex = 0
    $buttonOK.Text = "OK"
    $buttonOK.UseVisualStyleBackColor = $True
    $form.controls.add($buttonOK)
    
    #label for showing hits
    $hit=New-Object Windows.Forms.Label
    $hit.Text = 'Not hit yet'
    $hit.font=New-Object System.Drawing.Font([System.Drawing.FontFamily]::GenericSansSerif,14.0,[System.Drawing.FontStyle]::Bold)
    $hit.Location = New-Object Drawing.Point 650,50
    $form.controls.add($hit)
    
    $Form_StateCorrection_Load={
         #Correct the initial state of the form to prevent the .Net maximized form issue
         $form.WindowState = $InitialFormWindowState
    }
    
    # default label event
    $action={
        $hit.Text=$this.Name
        $this.BackColor='blue'
    }
    
    # generate label matrix
    For($i=0;$i -lt 8;$i++) {
       for($j=0;$j -lt 8;$j++){
            $x = 25 + $i * 70
            $y = 25 + $j * 70
            $label = New-Object Windows.Forms.Label
            $label.Location = New-Object Drawing.Point $x,$y
            $label.Size = New-Object Drawing.Point 70,70
            $label.Name = "[$i,$j]"
            $label.BorderStyle = [System.Windows.Forms.BorderStyle]::FixedSingle
            $label.BackColor = 'white'
            $label.add_Click($action)
            $form.controls.add($label)
       }
    }
    
    #Init the OnLoad event to correct the initial state of the form
    $InitialFormWindowState=$form.WindowState
    $form.add_Load($Form_StateCorrection_Load)
    
    $form.ShowDialog()
    


    ¯\_(ツ)_/¯

    Sunday, March 2, 2014 11:13 AM
  • Thank you for this simple solution.

    Sunday, March 2, 2014 5:22 PM
  • and thank you for answering my next question, just the example code I was looking for.

    Hats off (`)

    Sunday, March 2, 2014 5:23 PM
  • Sunday, March 2, 2014 5:52 PM