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
Meble kuchenne na zamówienie - na wymiar - Wrocław
Joined: Thu Jun 07, 2018 7:15 am

Auto-sum without using a button

Post by rdlindsey »

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: 14445
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Auto-sum without using a button

Post by jvierra »

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

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

Re: Auto-sum without using a button

Post by mxtrinidad »

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 35 times

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

Re: Auto-sum without using a button

Post by jvierra »

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 22 times

Locked