Thursday, October 7, 2010

99 names. Creating quick powershell GUI applications.

I thought it would be nice to try creating a GUI application in Powershell. During the Microsoft scripting games, I came across PrimalForms from Sapien. This simple tool allows you to drag and drop window elements and create a basic shell of a form that you can export for use in Powershell. From here you can add code for event handlers that were defined in your form. So as a demonstration of a very basic usage of this, I created a simple application to show the 99 names of Allah in transliterated form and English. The form allows for simple forward, backwards and random name features.




The Code:


########################################################################
# Code Generated By: SAPIEN Technologies PrimalForms (Community Edition) v1.0.8.0
# Generated On: 10/7/2010 1:21 PM
# Generated By: Nathan_Linley
########################################################################

 
$transliterations = ("Allah","Al-'Aziz","Al-'Alim","Al-'Azim","Al-'Aliyy","Al-Ahad","Al-Awwal","Al-Akhir",
 "Al-'Afuw","Al-Akram","Al-A'la","Al-'Allam","Al-Bari'","Al-Basir","Al-Batin","Al-Barr","Al-Badi'","Al-Ba'ith",
 "Al-Baqi","Al-Fattah","Al-Fatir","Al-Ghaffar","Al-Ghafur","Al-Ghani","Al-Ghafir","Ghalib","Al-Halim",
 "Al-Hafiiz","Al-Hasib","Al-Hakim","Al-Haqq","Al-Hameed","Al-Hayy","Al-Hadi","Al-Haafiz","Al-Hafiyy",
 "Al-Ilah","Al-Jabbar","Al-Jame'","Al-Jaleel","Al-Khaliq","Al-Khabir","Al-Kabir","Al-Kafil","Al-Khallaq",
 "Al-Kafi","Al-Latif","Al-Malik","Al-Mu'min","Al-Muhaimin","Al-Mutakabbir","Al-Musawwir","Al-Muqit",
 "Al-Mujib","Al-Majid","Al-Matin","Al-Muqtadir","Al-Muta'Ali","Al-Mubin","Al-Maula","Al-Maliik",
 "Al-Muhit","Al-Musta'an","Al-Mannan","Al-Muhyi","Al-Mumit","An-Nur","Al-Nasir","Al-Quddus",
 "Al-Qahhar","Al-Qarib","Al-Qawi","Al-Qayyum","Al-Qaadir","Al-Qadiir","Al-Qahir","Ar-Rahman",
 "Ar-Rahim","Ar-Razzaq","Ar-Raqeeb","Ar-Ra'uf","Ar-Rabb","Ar-Rafi'","As-Salam","As-Sami'","Ash-Shakur",
 "Ash-Shahid","As-Samad","Ash-Shaakir","At-Tawwab","Al-Wahhab","Al-Wasi","Al-Wadud","Al-Wakil","Al-Wali",
 "Al-Waahid","Al-Waali","Al-Waarith","Az-Zahir")

$translations = ( "God","The Mighty One","The All-Knowing","The Great One","The Sublime","The One","The First",
 "The Last","The Pardoner","The Most Bounteous","The Most High","The Omniscient","The Maker","The All seeing One",
 "The Hidden","The Source of All Goodness","The Originator","The Awakener","The Everlasting One","The Judge",
 "The Creator","The Great Forgiver","The All Forgiving","The Self Sufficient","The Forgiver","The Predominant",
 "The Clement","The Preserver","The Reckoner","The Wise","The Truth","The Praiseworthy","The Alive",
 "The Guide","The Protector","The Gracious","God","The Compeller","The Gatherer","The Glorious","The Creator",
 "The Aware","The Most Great","The Surety","The Creator","The Sufficient One","The Subtle One","The Sovereign",
 "The Giver of Peace","The Protector","The Majestic","The Fashioner","The Maintainer","The Responsive",
 "The Most Glorious One","The Firm One","The Powerful","The Most Exalted","The Manifest","The Patron",
 "The King, The Sovereign","All Pervading","One Who is Called Upon For Help","The Gracious","The Giver of Life",
 "The Giver of Death","The Light","The Helper","The Holy","The Dominant","The Nigh","The Most Strong",
 "The Self-Subsisting","The Able","The Mighty","The Omnipotent","The Compassionate","The Merciful",
 "The Provider","The Watchful","The Compassionate","The Sustainer","The Sublime","The Provider of Peace",
 "The All-Hearing","The Appreciative","The Witness","The Eternal","The Appreciative","The Acceptor of Repentance",
 "The Bestower","The All-Embracing","The Loving","The Trustee","The Protecting Friend","The One","The Governor",
 "The Inheritor","The Manifest")

 

function getName([int]$nameval) {
 $result = New-Object PSobject
 Add-Member -InputObject $result Noteproperty Transliteration $transliterations[$nameval-1]
 Add-Member -InputObject $result Noteproperty Translation $translations[$nameval-1]
 return $result
 
}


#Generated Form Function
function GenerateForm {


#region Import the Assemblies
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
#endregion

#region Generated Form Objects
$form1 = New-Object System.Windows.Forms.Form
$lbl99 = New-Object System.Windows.Forms.Label
$lblCounter = New-Object System.Windows.Forms.Label
$label2 = New-Object System.Windows.Forms.Label
$lblTranslit = New-Object System.Windows.Forms.Label
$txtTranslation = New-Object System.Windows.Forms.TextBox
$txtTransliteration = New-Object System.Windows.Forms.TextBox
$btnRandom = New-Object System.Windows.Forms.Button
$btnNext = New-Object System.Windows.Forms.Button
$btnPrev = New-Object System.Windows.Forms.Button
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
#endregion Generated Form Objects

#----------------------------------------------
#Generated Event Script Blocks
#----------------------------------------------
#Provide Custom Code for events specified in PrimalForms.

$handler_btnNext_Click= 
{
 $counter = [int]$lblCounter.text
 if ($counter -eq 99) {
  $counter = 1
 } else { $counter += 1 }
 $name = &getname $counter
 $txtTransliteration.text = $name.Transliteration
 $txtTranslation.text = $name.Translation
 $lblCounter.text = $counter
 $form1.refresh()
}

$handler_btnRandom_Click= 
{

 $rnd = New-Object system.Random
 $counter = $rnd.next(1,99)
 $name = &getname $counter
 $txtTransliteration.text = $name.Transliteration
 $txtTranslation.text = $name.Translation
 $lblCounter.text = $counter
 $form1.refresh()
}


$handler_btnPrev_Click= 
{

 $counter = [int]$lblCounter.text
 if ($counter -eq 1) {
  $counter = 99
 } else { $counter -= 1 }
 $name = &getname $counter
 $txtTransliteration.text = $name.Transliteration
 $txtTranslation.text = $name.Translation
 $lblCounter.text = $counter
 $form1.refresh()
}

$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form issue
 $form1.WindowState = $InitialFormWindowState
}

#----------------------------------------------
#region Generated Form Code
$form1.Text = "99 Names of Allah"
$form1.Name = "form1"
$form1.CausesValidation = $False
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 284
$System_Drawing_Size.Height = 230
$form1.ClientSize = $System_Drawing_Size

$lbl99.TabIndex = 9
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 48
$System_Drawing_Size.Height = 23
$lbl99.Size = $System_Drawing_Size
$lbl99.Text = "of 99"

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 93
$System_Drawing_Point.Y = 170
$lbl99.Location = $System_Drawing_Point
$lbl99.DataBindings.DefaultDataSourceUpdateMode = 0
$lbl99.Name = "lbl99"

$form1.Controls.Add($lbl99)

$lblCounter.TabIndex = 8
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 20
$System_Drawing_Size.Height = 23
$lblCounter.Size = $System_Drawing_Size
$lblCounter.Text = "1"

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 67
$System_Drawing_Point.Y = 170
$lblCounter.Location = $System_Drawing_Point
$lblCounter.DataBindings.DefaultDataSourceUpdateMode = 0
$lblCounter.Name = "lblCounter"

$form1.Controls.Add($lblCounter)

$label2.TabIndex = 7
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 100
$System_Drawing_Size.Height = 16
$label2.Size = $System_Drawing_Size
$label2.Text = "Translation"

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 13
$System_Drawing_Point.Y = 40
$label2.Location = $System_Drawing_Point
$label2.DataBindings.DefaultDataSourceUpdateMode = 0
$label2.Name = "label2"

$form1.Controls.Add($label2)

$lblTranslit.TabIndex = 6
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 100
$System_Drawing_Size.Height = 16
$lblTranslit.Size = $System_Drawing_Size
$lblTranslit.Text = "Transliteration"

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 13
$System_Drawing_Point.Y = 110
$lblTranslit.Location = $System_Drawing_Point
$lblTranslit.DataBindings.DefaultDataSourceUpdateMode = 0
$lblTranslit.Name = "lblTranslit"

$form1.Controls.Add($lblTranslit)

$txtTranslation.CausesValidation = $False
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 248
$System_Drawing_Size.Height = 20
$txtTranslation.Size = $System_Drawing_Size
$txtTranslation.DataBindings.DefaultDataSourceUpdateMode = 0
$txtTranslation.Name = "txtTranslation"
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 13
$System_Drawing_Point.Y = 10
$txtTranslation.Location = $System_Drawing_Point
$txtTranslation.Enabled = $False
$txtTranslation.TabIndex = 5
$txtTranslation.text = $translations[0]

$form1.Controls.Add($txtTranslation)

$txtTransliteration.CausesValidation = $False
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 249
$System_Drawing_Size.Height = 20
$txtTransliteration.Size = $System_Drawing_Size
$txtTransliteration.DataBindings.DefaultDataSourceUpdateMode = 0
$txtTransliteration.Name = "txtTransliteration"
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 13
$System_Drawing_Point.Y = 80
$txtTransliteration.Location = $System_Drawing_Point
$txtTransliteration.Enabled = $False
$txtTransliteration.TabIndex = 4
$txtTransliteration.text = $transliterations[0]

$form1.Controls.Add($txtTransliteration)

$btnRandom.TabIndex = 3
$btnRandom.Name = "btnRandom"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 75
$System_Drawing_Size.Height = 23
$btnRandom.Size = $System_Drawing_Size
$btnRandom.UseVisualStyleBackColor = $True

$btnRandom.Text = "Random"

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 197
$System_Drawing_Point.Y = 200
$btnRandom.Location = $System_Drawing_Point
$btnRandom.DataBindings.DefaultDataSourceUpdateMode = 0
$btnRandom.add_Click($handler_btnRandom_Click)

$form1.Controls.Add($btnRandom)

$btnNext.TabIndex = 2
$btnNext.Name = "btnNext"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 75
$System_Drawing_Size.Height = 23
$btnNext.Size = $System_Drawing_Size
$btnNext.UseVisualStyleBackColor = $True

$btnNext.Text = "Next"

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 93
$System_Drawing_Point.Y = 200
$btnNext.Location = $System_Drawing_Point
$btnNext.DataBindings.DefaultDataSourceUpdateMode = 0
$btnNext.add_Click($handler_btnNext_Click)

$form1.Controls.Add($btnNext)

$btnPrev.TabIndex = 1
$btnPrev.Name = "btnPrev"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 75
$System_Drawing_Size.Height = 23
$btnPrev.Size = $System_Drawing_Size
$btnPrev.UseVisualStyleBackColor = $True

$btnPrev.Text = "Previous"

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 200
$btnPrev.Location = $System_Drawing_Point
$btnPrev.DataBindings.DefaultDataSourceUpdateMode = 0
$btnPrev.add_Click($handler_btnPrev_Click)

$form1.Controls.Add($btnPrev)


#endregion Generated Form Code

#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState
#Init the OnLoad event to correct the initial state of the form
$form1.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$form1.ShowDialog()| Out-Null

} #End Function

#Call the Function
GenerateForm



NOTE: This post was updated from its original form to use a more authentic list of the 99 names. (Aug 2011) You may want to verify this list if you wish to use this application for learning. Also note, the arabic text has been removed as I couldn't find a non-graphical copy of the names to use.

No comments:

Post a Comment