My script takes in 3 number inputs and performs functions accordingly. For now I need to stop the entire script and re-run if I have new numbers to input. How can I use the same session to enter new numbers?
Add-Type -assembly System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$main_form = New-Object System.Windows.Forms.Form
# Box size
$main_form.Text ='ABC Pattern Calculator'
$main_form.Width = 450
$main_form.Height = 350
$main_form.AutoSize = $true
# Label1
$intitalLabel = New-Object System.Windows.Forms.Label
$intitalLabel.Text = "Click on Bull or Bear tab"
$intitalLabel.Location = New-Object System.Drawing.Point(30,20)
$intitalLabel.Font = [System.Drawing.Font]::new("Microsoft Sans Serif", 12, [System.Drawing.FontStyle]::Bold)
$intitalLabel.AutoSize = $true
$main_form.Controls.Add($intitalLabel)
############################# TABS #######################################
# Tab master control
$MainTab = New-Object System.Windows.Forms.TabControl
$MainTab.Size = '400,250'
$MainTab.Location = '30,50'
$MainTab.Multiline = $true
$MainTab.AutoSize = $true
$MainTab.Anchor = 'Top,Left,Bottom,Right'
# Tab pages
$TabPage1 = New-Object System.Windows.Forms.TabPage
$Tabpage1.TabIndex = 1
$Tabpage1.Text = 'Bullish Pattern'
$TabPage1.Name = 'Tab1'
$TabPage2 = New-Object System.Windows.Forms.TabPage
$Tabpage2.TabIndex = 2
$Tabpage2.Text = 'Bearish Pattern'
$TabPage2.Name = 'Tab2'
# Add tabs to tab control
$MainTab.Controls.AddRange(@($TabPage1,$TabPage2))
$main_form.controls.Add($MainTab)
########################## Bull Tab #######################################
# Textbox = Point A
$aBullTextbox = New-Object System.Windows.Forms.TextBox
$aBullTextbox.Location = New-Object System.Drawing.Point(82,30)
$aBullTextbox.Size = New-Object System.Drawing.Size(140)
$main_form.StartPosition = 'CenterScreen'
$main_form.Controls.Add($aBullTextbox)
# Textbox = Point B
$bBullTextbox = New-Object System.Windows.Forms.TextBox
$bBullTextbox.Location = New-Object System.Drawing.Point(82,80)
$bBullTextbox.Size = New-Object System.Drawing.Size(140)
$main_form.StartPosition = 'CenterScreen'
$main_form.Controls.Add($bBullTextbox)
# Textbox = Point C
$cBullTextbox = New-Object System.Windows.Forms.TextBox
$cBullTextbox.Location = New-Object System.Drawing.Point(82,136)
$cBullTextbox.Size = New-Object System.Drawing.Size(140)
$main_form.StartPosition = 'CenterScreen'
$main_form.Controls.Add($cBullTextbox)
# Label1
$aBullLabel = New-Object System.Windows.Forms.Label
$aBullLabel.Text = "A Low:"
$aBullLabel.Location = New-Object System.Drawing.Point(10,30)
$aBullLabel.Font = [System.Drawing.Font]::new("Microsoft Sans Serif", 12, [System.Drawing.FontStyle]::Bold)
$aBullLabel.AutoSize = $true
$main_form.Controls.Add($aBullLabel)
# Label2
$bBullLabel = New-Object System.Windows.Forms.Label
$bBullLabel.Text = "B High:"
$bBullLabel.Location = New-Object System.Drawing.Point(10,80)
$bBullLabel.Font = [System.Drawing.Font]::new("Microsoft Sans Serif", 12, [System.Drawing.FontStyle]::Bold)
$bBullLabel.AutoSize = $true
$main_form.Controls.Add($bBullLabel)
# Label3
$cBullLabel = New-Object System.Windows.Forms.Label
$cBullLabel.Text = "C Low:"
$cBullLabel.Location = New-Object System.Drawing.Point(10,136)
$cBullLabel.Font = [System.Drawing.Font]::new("Microsoft Sans Serif", 12, [System.Drawing.FontStyle]::Bold)
$cBullLabel.AutoSize = $true
$main_form.Controls.Add($cBullLabel)
# Radio button = Calculate
$bullCalculateButton = New-Object System.Windows.Forms.Button
$bullCalculateButton.Location = New-Object System.Drawing.Size(50,180)
$bullCalculateButton.Size = New-Object System.Drawing.Size(110,23)
$bullCalculateButton.Text = "Calculate"
$main_form.Controls.Add($bullCalculateButton)
# Math function
$bullCalculateButton.Add_Click({
$aBullCalc = $aBullTextbox.Text
$bBullCalc = $bBullTextbox.Text
$cBullCalc = $cBullTextbox.Text
$pointD = $bBullCalc - $aBullCalc $cBullCalc
# D Label
$dBullLabel = New-Object System.Windows.Forms.Label
$dBullLabel.Text = "Point D will be at:"
$dBullLabel.Location = New-Object System.Drawing.Point(230,50)
$dBullLabel.Font = [System.Drawing.Font]::new("Microsoft Sans Serif", 12, [System.Drawing.FontStyle]::Bold)
$dBullLabel.AutoSize = $true
$main_form.Controls.Add($dBullLabel)
# Answer to D
$dcalcLabel = New-Object System.Windows.Forms.Label
$dcalcLabel.Text = $pointD
$dcalcLabel.Location = New-Object System.Drawing.Point(280,70)
$dcalcLabel.Font = [System.Drawing.Font]::new("Microsoft Sans Serif", 12, [System.Drawing.FontStyle]::Bold)
$dcalcLabel.AutoSize = $true
$main_form.Controls.Add($dcalcLabel)
$TabPage1.Controls.AddRange(@($dcalcLabel,$dBullLabel))
})
$TabPage1.Controls.AddRange(@($aBullTextbox,$bBullTextbox,$cBullTextbox,$aBullLabel,$bBullLabel,$cBullLabel,$bullCalculateButton))
########################## Bear Tab #######################################
# Textbox = Point A
$aBearTextbox = New-Object System.Windows.Forms.TextBox
$aBearTextbox.Location = New-Object System.Drawing.Point(82,30)
$aBearTextbox.Size = New-Object System.Drawing.Size(140)
$main_form.StartPosition = 'CenterScreen'
$main_form.Controls.Add($aBearTextbox)
# Textbox = Point B
$bBearTextbox = New-Object System.Windows.Forms.TextBox
$bBearTextbox.Location = New-Object System.Drawing.Point(82,80)
$bBearTextbox.Size = New-Object System.Drawing.Size(140)
$main_form.StartPosition = 'CenterScreen'
$main_form.Controls.Add($bBearTextbox)
# Textbox = Point C
$cBearTextbox = New-Object System.Windows.Forms.TextBox
$cBearTextbox.Location = New-Object System.Drawing.Point(82,136)
$cBearTextbox.Size = New-Object System.Drawing.Size(140)
$main_form.StartPosition = 'CenterScreen'
$main_form.Controls.Add($cBearTextbox)
# Label1
$aBearLabel = New-Object System.Windows.Forms.Label
$aBearLabel.Text = "A High:"
$aBearLabel.Location = New-Object System.Drawing.Point(10,30)
$aBearLabel.Font = [System.Drawing.Font]::new("Microsoft Sans Serif", 12, [System.Drawing.FontStyle]::Bold)
$aBearLabel.AutoSize = $true
$main_form.Controls.Add($aBearLabel)
# Label2
$bBearLabel = New-Object System.Windows.Forms.Label
$bBearLabel.Text = "B Low:"
$bBearLabel.Location = New-Object System.Drawing.Point(10,80)
$bBearLabel.Font = [System.Drawing.Font]::new("Microsoft Sans Serif", 12, [System.Drawing.FontStyle]::Bold)
$bBearLabel.AutoSize = $true
$main_form.Controls.Add($bBearLabel)
# Label3
$cBearLabel = New-Object System.Windows.Forms.Label
$cBearLabel.Text = "C High:"
$cBearLabel.Location = New-Object System.Drawing.Point(10,136)
$cBearLabel.Font = [System.Drawing.Font]::new("Microsoft Sans Serif", 12, [System.Drawing.FontStyle]::Bold)
$cBearLabel.AutoSize = $true
$main_form.Controls.Add($cBearLabel)
# Radio button = Calculate
$BearCalculateButton = New-Object System.Windows.Forms.Button
$BearCalculateButton.Location = New-Object System.Drawing.Size(50,180)
$BearCalculateButton.Size = New-Object System.Drawing.Size(110,23)
$BearCalculateButton.Text = "Calculate"
$main_form.Controls.Add($BearCalculateButton)
# Math function
$BearCalculateButton.Add_Click({
$aBearCalc = $aBearTextbox.Text
$bBearCalc = $bBearTextbox.Text
$cBearCalc = $cBearTextbox.Text
$pointDbear = $aBearCalc - $bBearCalc - $cBearCalc
# D Label
$dbearLabel = New-Object System.Windows.Forms.Label
$dbearLabel.Text = "Point D will be at:"
$dbearLabel.Location = New-Object System.Drawing.Point(230,50)
$dbearLabel.Font = [System.Drawing.Font]::new("Microsoft Sans Serif", 12, [System.Drawing.FontStyle]::Bold)
$dbearLabel.AutoSize = $true
$main_form.Controls.Add($dbearLabel)
# Answer to D
$dbearcalcLabel = New-Object System.Windows.Forms.Label
$dbearcalcLabel.Text = $pointDbear
$dbearcalcLabel.Location = New-Object System.Drawing.Point(280,70)
$dbearcalcLabel.Font = [System.Drawing.Font]::new("Microsoft Sans Serif", 12, [System.Drawing.FontStyle]::Bold)
$dbearcalcLabel.AutoSize = $true
$main_form.Controls.Add($dbearcalcLabel)
$TabPage2.Controls.AddRange(@($dbearcalcLabel,$dbearLabel))
})
$TabPage2.Controls.AddRange(@($aBearTextbox,$bBearTextbox,$cBearTextbox,$aBearLabel,$bBearLabel,$cBearLabel,$BearCalculateButton))
# To show the box
$main_form.ShowDialog()
My script takes in 3 number inputs and performs functions accordingly. For now I need to stop the entire script and re-run if I have new numbers to input. How can I use the same session to enter new numbers?
CodePudding user response:
You can simplify it by adding the labels to the main Form only once outside the .Add_Click
event, then inside the event you can do your calculation and update their .Text
property. This example demonstrates how to do it for the "Bullish Pattern" tab and you can do the same, following this example, for the "Bearish Pattern" one:
# Both labels are now outside the Event!
$dBullLabel = [Windows.Forms.Label]@{
Text = ''
Location = [Drawing.Point]::new(230, 50)
Font = [Drawing.Font]::new("Microsoft Sans Serif", 12, [Drawing.FontStyle]::Bold)
AutoSize = $true
}
$dcalcLabel = [Windows.Forms.Label]@{
Text = ''
Location = [Drawing.Point]::new(280, 70)
Font = [System.Drawing.Font]::new("Microsoft Sans Serif", 12, [Drawing.FontStyle]::Bold)
AutoSize = $true
}
$main_form.Controls.AddRange(@($dBullLabel, $dcalcLabel))
$TabPage1.Controls.AddRange(@($dcalcLabel, $dBullLabel))
$bullCalculateButton.Add_Click({
# Inside the event, we do just calculation
$pointD = [double] $aBullTextbox.Text - $bBullTextbox.Text - $cBullTextbox.Text
# and update
$dBullLabel.Text = "Point D will be at:"
$dcalcLabel.Text = $pointD
})