PowerShell: Recursive dropdown boxes [Sudoku solver]

Before you continue please consider clicking on one of the horrible ads. I know they are a pain but they help me pay for the hosting of this site. It owes me a lot of money. Sob story over.

I am currently undertaking a large, il-advised and totally unnecessary project. Creating a Sudoku solver in PowerShell. Yes I know this is the wrong language for such a thing but I am doing it anyway.

Why you ask? Well mainly because I have stopped drinking for a month or 2, but also to keep my scripting chops sharp.

There will be lots of parts to this so I will blog it in nice bite size pieces that might be useful in general scripting applications, and create a new tag on the site that you can use to bring them all together.

The first is some work on the visuals.  I need 81 drop down boxes, which is a lot of drop down boxes, hence we need a way to recursively generate them.

The following code looks like his when run:

I have annotated the code so hopefully it should be quite easy to follow.

# Set the size of your form
    $Form = New-Object System.Windows.Forms.Form
    $Form.Width = 600
    $Form.height = 600
    $Form.Text = ”Mat Grumps dropdown boxes example”

# Set the font of the text to be used within the form
    $Font = New-Object System.Drawing.Font("Ariel",8)
    $Form.Font = $Font

#Set some valus to set starting points
    $i= 0
    $x=30
    $y=30

#Now we create 81 dropdown boxes
 1..81 | ForEach-Object {"ComboBox$PSitem"} | ForEach{
        $CurrentObj = $null
        $CurrentObj = New-Object System.Windows.Forms.ComboBox

#increment position
    if ((($i/9) -is [int]) -and ($i -gt 0))
        {
            $y=$y + 50
            $x=$x - 540    
        }

#Set location of the boxes
    $CurrentObj.Location = new-object System.Drawing.Size($x,($y))
#Set Size
    $CurrentObj.Size = new-object System.Drawing.Size (55,40)
    #define selections
     @('blank',‘1’,’2’,’3',‘4’,’5’,’6’,‘7’,’8’,’9’) | ForEach-Object {[void] $CurrentObj.Items.Add($_)}

#Set default selection
    $CurrentObj.SelectedIndex = 0

#Create dynamic variables to store choices
#Make sure the variable doesnt exist already
    Remove-Variable -Name "value$i" -ErrorAction Ignore
#Create the dynamic variable
    New-Variable "value$i" $CurrentObj

    #add the buttons to the form
    $form.Controls.Add($CurrentObj)
        
    #increment some stuff
    $x = $x + 60
    $i++
    }

    #Draw the form and dropdown boxes
    $form.ShowDialog()

I have added the code to capture the results of this form in part but it really needs its own post, hence we wont be covering that just yet. Maybe subscribe so you don't miss the rest.

to be continued...