Wait for userinput in foreach loop

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.
This topic is 7 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
ronkna
Posts: 15
Last visit: Wed Jan 03, 2024 6:12 am

Wait for userinput in foreach loop

Post by ronkna »

Hi All,

I'm working on a side project for my kids which hopefully helps them with learning to multiply.

I've made GUI which ask which multiplication table the want to practice and how many.

Then I made a foreach loop which shows the calculation in a label and they have to put the answer in an answerbox before pressing a button to check for the answer...

But the problem is that the script just continues to run.. How do I make the script stop and wait for an answer?

I could do it with a Read-host but that isn't nice enough ;)
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Wait for userinput in foreach loop

Post by davidc »

[POST MOVED TO POWERSHELL GUI FORUM BY MODERATOR]

Sounds like a great idea :)

As for the loop, you have to switch your mindset to think in events instead of one continues script that runs in the console.

https://www.sapien.com/blog/2016/01/04/ ... in-events/

In this case don't need a loop in a GUI event driven script. For example, you should only trigger the next calculation after your child presses the submit button.

David
David
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Wait for userinput in foreach loop

Post by jvierra »

Suggestion.

Decide what "actions" are to be performed by the user in a session; what do they see and what do they do to respond. Once you define these steps then you can look at what controls will be used and how. Each control would have an appropriate event that moves to the next "state if the exercises.

Example:
  1. User is presented with problem in Label1 -> user types answer into TextBox1.
  2.         ===> Text box "Change" event checks answer and opens MessageBox with happy face and reward ("Very good - try another")
  3.             and new problem is displayed.  
  4.     If answer wrong play ugly sound and clear answer for another guess.
  5.  
  6. loop on this until all problems are answered.
This requires one event that runs whenever an answer is typed and enter or tab is used or you could use a submit button.
User avatar
ronkna
Posts: 15
Last visit: Wed Jan 03, 2024 6:12 am

Re: Wait for userinput in foreach loop

Post by ronkna »

davidc wrote:[POST MOVED TO POWERSHELL GUI FORUM BY MODERATOR]

Sounds like a great idea :)

As for the loop, you have to switch your mindset to think in events instead of one continues script that runs in the console.

https://www.sapien.com/blog/2016/01/04/ ... in-events/

In this case don't need a loop in a GUI event driven script. For example, you should only trigger the next calculation after your child presses the submit button.

David
As a matter of fact.. I did switch my mindset.

I've made a script which runs in the console a few months so I had to port it to a GUI. One thing I had to let go for now is that they can enter how many calculations they will have to do.

That's what I did, Now I have a working GUI which shows the multiplications, how many good/bad answers and a button which shows their score :)

Kinda neat :)

Now I have to dive in events...
jvierra wrote:Suggestion.

Decide what "actions" are to be performed by the user in a session; what do they see and what do they do to respond. Once you define these steps then you can look at what controls will be used and how. Each control would have an appropriate event that moves to the next "state if the exercises.

Example:
  1. User is presented with problem in Label1 -> user types answer into TextBox1.
  2.         ===> Text box "Change" event checks answer and opens MessageBox with happy face and reward ("Very good - try another")
  3.             and new problem is displayed.  
  4.     If answer wrong play ugly sound and clear answer for another guess.
  5.  
  6. loop on this until all problems are answered.
This requires one event that runs whenever an answer is typed and enter or tab is used or you could use a submit button.

I've found another piece of script from both of you about pressing Enter in a Textbox.

Code: Select all


$textbox1_KeyUp=[System.Windows.Forms.KeyEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.KeyEventArgs]
	if($_.KeyCode -eq 'Enter')
	{
		&$button1_Click	
	}
}
I've tried this but when I entered a 2 digit solution, it does $button1_click on the first digit. I've changed the action of the textbox to KeyUp $button1_Click...

Did I misplace it in the script maybe?

As you may have noticed, I'm new to powershell studio and forms.. :) I've been working with powershell for over a year now and I really like it.. :)
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Wait for userinput in foreach loop

Post by jvierra »

Not how to enable the enter key. Just use the validate. and do not use a default action on the form . The enter key will terminate the textbox.

Forms navigation is one of the most difficult things for a non-windows programmer to learn. It is layered and complex.

A text box is complete when we change focus. (tab or enter) which generates the change event and the validation events.
User avatar
ronkna
Posts: 15
Last visit: Wed Jan 03, 2024 6:12 am

Re: Wait for userinput in foreach loop

Post by ronkna »

jvierra wrote:Not how to enable the enter key. Just use the validate. and do not use a default action on the form . The enter key will terminate the textbox.

Forms navigation is one of the most difficult things for a non-windows programmer to learn. It is layered and complex.

A text box is complete when we change focus. (tab or enter) which generates the change event and the validation events.
What do you mean by Just use the validate. and do not use a default action on the form? :?

And yes, it is quite difficult for me but nonetheless very interesting. 8-)
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Wait for userinput in foreach loop

Post by jvierra »

Here is a sampler of methods for managing a response to a forms controls.
Attachments
Demo-TextboxValidating.psf
(28.17 KiB) Downloaded 188 times
User avatar
monoeagle
Posts: 108
Last visit: Fri Jan 26, 2024 10:44 am

Re: Wait for userinput in foreach loop

Post by monoeagle »

Hi jvierra,

thanks for the demo.

is a separate scriptblock for validation needed for every textfield or is it possible to build one validation scriptblock if more than one textfield exist:

for field a,b,e,f are int values correct and for field c,g,h strings with a format xxx-xxx-xxx?

regards
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Wait for userinput in foreach loop

Post by jvierra »

You can just assign the same script block to all textboxes it applies to.
User avatar
ronkna
Posts: 15
Last visit: Wed Jan 03, 2024 6:12 am

Re: Wait for userinput in foreach loop

Post by ronkna »

jvierra wrote:Here is a sampler of methods for managing a response to a forms controls.
Thnx :) I figured it out.

Now I can move on with my project ;)
This topic is 7 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked