Looking for some "How To" answers on a GUI app

I’ve got a partial Powershell GUI app written but I’m fairly new to Powershell and I need some help to get me on the way to having a working program. I’ve listed my code below but my questions are as follows:

I’ve set the 3rd and 4th textboxes to be read only but when I run the code these textboxes still get focus and I would like to bypass these 2 boxes and have focus set on the OK button. Can anyone tell me how to bypass focus on these two fields?

My second question has to deal with using the input data from the first two text boxes to perform a calculation and then populate values in the 3rd and 4th read only textboxes. Can anyone provide some help on this as well?

Here’s my code:

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$form = New-Object System.Windows.Forms.Form
$form.Text = ‘Data Entry Form’
$form.Size = New-Object System.Drawing.Size(300,300)
$form.StartPosition = ‘CenterScreen’

$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(75,220)
$okButton.Size = New-Object System.Drawing.Size(75,23)
$okButton.Text = ‘OK’
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $okButton
$form.Controls.Add($okButton)

$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Point(150,220)
$cancelButton.Size = New-Object System.Drawing.Size(75,23)
$cancelButton.Text = ‘Cancel’
$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $cancelButton
$form.Controls.Add($cancelButton)

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = ‘Previous Balance:’
$form.Controls.Add($label)

$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(10,40)
$textBox.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox)

$label2 = New-Object System.Windows.Forms.Label
$label2.Location = New-Object System.Drawing.Point(10,70)
$label2.Size = New-Object System.Drawing.Size(280,20)
$label2.Text = ‘Current Balance:’
$form.Controls.Add($label2)

$textBox2 = New-Object System.Windows.Forms.TextBox
$textBox2.Location = New-Object System.Drawing.Point(10,90)
$textBox2.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox2)

$label3 = New-Object System.Windows.Forms.Label
$label3.Location = New-Object System.Drawing.Point(10,120)
$label3.Size = New-Object System.Drawing.Size(280,20)
$label3.Text = ‘Principal Applied:’
$form.Controls.Add($label3)

$textBox3 = New-Object System.Windows.Forms.TextBox
$textBox3.Location = New-Object System.Drawing.Point(10,140)
$textBox3.Size = New-Object System.Drawing.Size(260,20)
$textbox3.ReadOnly=$true
$form.Controls.Add($textBox3)

$label4 = New-Object System.Windows.Forms.Label
$label4.Location = New-Object System.Drawing.Point(10,170)
$label4.Size = New-Object System.Drawing.Size(280,20)
$label4.Text = ‘Interest Paid:’
$form.Controls.Add($label4)

$textBox4 = New-Object System.Windows.Forms.TextBox
$textBox4.Location = New-Object System.Drawing.Point(10,190)
$textBox4.Size = New-Object System.Drawing.Size(260,20)
$textbox4.ReadOnly=$true
$form.Controls.Add($textBox4)

$form.Topmost = $true

$form.Add_Shown({$textBox.Select()})
$result = $form.ShowDialog()

if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
$x = $textBox.Text
$x
}

Hopefully this will get you started …

First, I would change your “okButton” code to this as I think you want it to do the calculation?


$calcButton = New-Object System.Windows.Forms.Button
$calcButton.Location = New-Object System.Drawing.Point(75,220)
$calcButton.Size = New-Object System.Drawing.Size(75,23)
$calcButton.Text = ‘Calc’
$calcButton.Add_Click({if($textBox.Text -NotMatch '^[0-9]*$' -Or $textBox2.Text -NotMatch '^[0-9]*$'){$textBox.Text = ''; $textBox2.Text = ''; $textBox3.Text = 'Please enter a valid Number'}else {Get-Math -Var1 $textBox.Text -Var2 $textBox2.Text}})
$form.Controls.Add($calcButton)

Then, add this cheesy function at the beginning of your script where you can change the math to whatever you like:

function Get-Math() {
	param (
		[Int] $var1,
		[Int] $var2
	)

	$principalPaid = $var1 - $var2
	$interestPaid = $var1 - $var2
	$textBox3.Text = $principalPaid
	$textBox4.Text = $interestPaid
	$textBox.Text = ''
	$textBox2.Text = ''
}

In my test, the first TextBox had focus which I think is what you want?

Are you aware your handle is the name of one of Frank Zappa’s kids :slight_smile:

Tony,
Thanks. That got me a bit farther but the code only validates integer input and in this application the values submitted in textbox and textbox2 will be double data type not integer. I found GetType() validation but I can’t figure out how to set this up so that the test for CalcButton…Add_Click can verify the input. The code you provided worked great for integer data so thanks for the help so far.

Try this:

$calcButton.Add_Click({if($textBox.Text -NotMatch '^[0-9]\d*(\.\d+)?$' -Or $textBox2.Text -NotMatch '^[0-9]\d*(\.\d+)?$'){$textBox.Text = ''; $textBox2.Text = ''; $textBox3.Text = 'Please enter a valid Number'}else {Get-Math -Var1 $textBox.Text -Var2 $textBox2.Text}})

And this in the function

param (
		[Double] $var1,
		[Double] $var2
	)

Thanks Again. The NotMatch string is a bit cryptic. I think I tried some code with specifying $var1 and $var2 being double but when I entered some text instead of numbers I think I remember Powershell croaking on me so I’ll double check. I also tried to use GetType and I think I got it partially figured out but I’ll use what you sent later on today.

Tony,
I made some modifications to the program to bypass focus of textboxes 3 and 4 I remove them and put the results in label3.text and Label4.text below the textbox2.text field on screen. I also created a function to display an error message in red as a label if non-numeric data is entered in the input textboxes and placed this message below the textbox2.text field. Both of these changes worked great when you had good data or bad but I had a time figuring out how to get rid of the error message if I made an error and then fixed it as the label3.text was not overwriting the error message that was previously displayed. I finally found the RemoveByKey code snippet that removes the error message. I thought perhaps some of this code might help some other soul that’s just learning Powershell. Here’s my Powershell script and thanks again for the help. That being said if you see anything that needs improvement, let me know.

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

function Display-Error() {
   $error1 = New-Object System.Windows.Forms.Label
   $error1.Location = New-Object System.Drawing.Point(10,120)
   $error1.Size = New-Object System.Drawing.Size(280,20)
   $error1.Name = "Error1"
   $error1.Forecolor = 'Red'
   $error1.Font = 'Bold'
   $error1.Text = '               Please Enter a Valid Number'
   $form.Controls.Add($error1)
}

function Get-Math() {
	param (
		$var1,
		$var2
	)
    if ($form.Controls.ContainsKey("Error1")) {
        # the label is already present. Remove it
        $form.Controls.RemoveByKey("Error1")
    }
    $MortgagePaid = 759.89
    $Var1Valid = $var1.GetType().FullName
    if ($Var1Valid = "System.Double") {
      $PrincipalApplied = $var1 - $var2
      $PrincipalApplied = ([Math]::Round($PrincipalApplied + 0.005, 2)) 
    }
    Else {
    }
    $Var2Valid = $var2.GetType().FullName
    if ($Var2Valid = "System.Double") {
      $MortgateInterest = $MortgagePaid - $PrincipalApplied
    }
    Else {
    }
    $label3 = New-Object System.Windows.Forms.Label
    $label3.Location = New-Object System.Drawing.Point(10,120)
    $label3.Size = New-Object System.Drawing.Size(280,20)
    $label3.Text = 'Principal Applied:       ' + $PrincipalApplied
    $form.Controls.Add($label3)

    $label4 = New-Object System.Windows.Forms.Label
    $label4.Location = New-Object System.Drawing.Point(10,140)
    $label4.Size = New-Object System.Drawing.Size(280,20)
    $label4.Text = 'Interest Paid:              ' + $MortgateInterest
    $form.Controls.Add($label4)


}
$form = New-Object System.Windows.Forms.Form
$form.Text = 'Mortgage Calculator'
$form.Size = New-Object System.Drawing.Size(300,250)
$form.StartPosition = 'CenterScreen'


$calcButton = New-Object System.Windows.Forms.Button
$calcButton.Location = New-Object System.Drawing.Point(75,170)
$calcButton.Size = New-Object System.Drawing.Size(75,23)
$calcButton.Text = ‘Calc’
$calcButton.Add_Click({if($textBox.Text -NotMatch '^[0-9]\d*(\.\d+)?$' -Or $textBox2.Text -NotMatch '^[0-9]\d*(\.\d+)?$'){$textBox.Text = ''; $textBox2.Text = '';  display-error }else {Get-Math -Var1 $textBox.Text -Var2 $textBox2.Text}})
$form.Controls.Add($calcButton)

$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Point(150,170)
$cancelButton.Size = New-Object System.Drawing.Size(75,23)
$cancelButton.Text = 'Cancel'
$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $cancelButton
$form.Controls.Add($cancelButton)

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'Previous Balance:'
$form.Controls.Add($label)

$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(10,40)
$textBox.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox)

$label2 = New-Object System.Windows.Forms.Label
$label2.Location = New-Object System.Drawing.Point(10,70)
$label2.Size = New-Object System.Drawing.Size(280,20)
$label2.Text = 'Current Balance:'
$form.Controls.Add($label2)

$textBox2 = New-Object System.Windows.Forms.TextBox
$textBox2.Location = New-Object System.Drawing.Point(10,90)
$textBox2.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox2)


$form.Topmost = $true

$form.Add_Shown({$textBox.Select()})
$result = $form.ShowDialog()

if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
    Start-Sleep -s 160
}
1 Like

William,
Welcome to the forum. :wave:t3:

Please, when you post code, sample data, console output or error messages format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org 1 <---- Click :point_up_2:t4: :wink:

( !! Sometimes the preformatted text button hides behind the settings gear symbol. :wink: )

You may go back, edit your posts once again and fix the formatting of your code. :point_up:t3:

Olaf,

Thanks for informing me of my formatting error. I’m new on this forum and was not aware of what the preformatted text button did until I received your message and as you suggested, I rewrote my message as I actually found the code snippet I needed and decided to display my working script that TonyD helped write.

1 Like

If one of the posts is the solution to your actual question you may mark it as such with the check mark to help others looking for a solution for the same or a similar issue to find the solution faster. :wink:
And you can give a little reward by liking the post actually helped you. :wink:

Thanks for that. Many times, users tend to forget this :slight_smile: