Auto-sum without using a button

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
rdlindsey
Posts: 1
Joined: Thu Jun 07, 2018 7:15 am

Auto-sum without using a button

Post by rdlindsey » Mon Aug 19, 2019 6:56 am

I am trying to populate a single textbox with multiple combobox values. Right now my code works on a button push, I am trying to get it to populate on the entry of combobox. I basically want to get rid of the 'ScoreNow' button and just have the textbox keep a running total. Here is what I have that populates it on the push of the button:
$buttonScoreNow_Click= {
#TODO: Place custom script here
$total = ($combobox2.Text, $combobox3.Text, $combobox4.Text, $combobox5.Text, $combobox6.Text | Measure-Object -Sum).Sum
$percent = ($total/13).tostring("p")
$textbox7.Text = $total
$textbox9.Text = $percent
$textbox7_TextChanged={
#TODO: Place custom script here
$total = ($combobox2.Text, $combobox3.Text, $combobox4.Text, $combobox5.Text, $combobox6.Text | Measure-Object -Sum).Sum
$textbox7.Text = $total
$Actual = $textbox7.text
}

jvierra
Posts: 13934
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Auto-sum without using a button

Post by jvierra » Mon Aug 19, 2019 7:50 am

Use the "ComboBox SelectionChanged" event to do the sum.

User avatar
mxtrinidad
Site Admin
Posts: 359
Joined: Sun Mar 03, 2013 12:42 pm

Re: Auto-sum without using a button

Post by mxtrinidad » Mon Aug 19, 2019 8:26 am

Or, you can also do it without adding the event. You'll find different ways to work the task.
TestSumComboBox02.psf
(24.48 KiB) Downloaded 19 times

jvierra
Posts: 13934
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Auto-sum without using a button

Post by jvierra » Mon Aug 19, 2019 8:54 am

Max

There is no need to use "ToString()" in PowerShell especially when doing arithmetic expressions.

This will always work as needed.

Code: Select all

$form1_Load={

        $AddCombo1Values = @(1, 2, 3)
	$AddCombo2Values = @(9, 39, 10)
	
	Update-ComboBox -ComboBox $combobox1 -Items $AddCombo1Values
	Update-ComboBox -ComboBox $combobox2 -Items $AddCombo2Values
	
}

$buttonProcess_Click={
	
	$textbox1.text = $combobox1.SelectedItem + $combobox2.SelectedItem 
	
}
Most of us avoid the use of line extenders. They can create interesting problems.

Here is an example of using the event to auto-calculate. It requires less code and is completely automatic.
Attachments
TestSumComboBox02 (1).psf
(17.44 KiB) Downloaded 6 times

Locked