How to prompt messagebox once inside a for loop in vb.net? - vb.net

Is it possible to display a single messagebox inside a for loop codes?
I am trying to but cannot successfully make it.
Please help.
Thanks in advance.

Code inside a loop, by definition, is meant to execute multiple times, for each iteration of it, so if you don't want to have multiple messages, move that call outside of the loop so it executes only once. This would be the main way of doing so, because it makes it clear that there will be ever only one of them.
Otherwise, you can condition its display, by some particular condition that's warranted to happen only once, possibly caching the result from the user. The second snippet from phadaphunk does something like that.

From you question, I get that you don't understand how it works below.
The loop is a set of instructions that must be executed "x" number of time. So everything that is in the body of that loop will be executed that number of time. Which means that if there are 5 iterations to the loop, you will get 5 messagebox.
This is how you do it if you want a different box for each iterations :
For Each x As Integer In someList
MessageBox.Show(x.ToString());
Otherwise you can't do it unless there is a certain condition that explains to the compiler at which iteration you want the message box to appear. Something like
For Each x As Integer In someList
If (x == 5)
MessageBox.Show(x.ToString());

Related

Why SeleniumIDE For Each loop uses one letter as local variable instead of whole word

I've tried SeleniumIde for first time few days ago and everyday been struggling a lot to just understand basic workflow however now I managed to login to webpage and store variable.
That variable, i wanted then to use as list.
The variable looks like this:
item1,item2,item3,item4
However, when use for each loop on this variable, it doesnt go item1>item2>item3, it goes i>t>e>m>1>i>t>e>m>2 etc. Either Selenium doesn't automatically parse it (make array from variable) as array, or im doing something wrong.
I'm sorry I have no clue how to import code from SeleniumIDE, therefore ill show screenshot.
https://i.imgur.com/0XaoanA.png
You can see on screen that I store variable, make foreach loop of it, and the loop writes one letter instead of one word in certain field on webpage.
Any help appreciated

loop param not working as expected

I have a single clip on the stage with an instance name of testShape. In frame 1 I have the following code:
createjs.Tween.get(this.testShape, {loop:true}).to({y:240}, 1000);
When I run this it loops infinitely as expected but what I want is for it to loop three time then stop and fire a complete event.
The docs say that the loop param...
Indicates the number of times to loop. If set to -1, the tween will loop continuously.
Which suggests I should be able to set {loop: 3} to achieve my desired result but any number value other than 0 just causes it to loop endlessly.
Can anyone advise on what I'm doing wrong or how to make a Tween loop n times before completing?
Cheers all
It looks like TweenJS 0.6.2 and earlier used a Boolean value for loops, so while you can set it to true or false, you can not put it as a number of loops. If you set it to a number, it will be converted to true.
createjs.Tween.get(obj, {loop:true}).to(…).to(…);
This behaviour was updated in version 1.0.0 of TweenJS, in September, 2017.
createjs.Tween.get(obj, {loop:3}).to(…).to(…);
I am glad you found a solution:
I got it working in the end by making each tween set up the next as it completes.

Why will this loop iterate just one time not two times?

The Random node will output the random value that is between 0 to 1. The initial i value should be zero. So for the first iteration, the comparator should output a signal TRUE to let the loop iterate again. When i is 2, the loop will stop, because the random variable is smaller than 2.
So as my thinking, the loop should iterate only two times and then stop, but the answer shown to says me the loop should iterate just 1 time. Is the answer wrong or is my thinking wrong?
I ran the same code and got 2 executions of the loop, as you'd expect.
Furthermore, since the random number generator cannot return exactly 1, the code is guaranteed to take exactly 2 loop cycles.

Optimal Method of Checking Keypresses on TI-89

for an experiment I decided to program a little game into my TI-89 using the built in program editor, however I cannot figure out an optimal method of getting keystrokes without significant delay. Currently I have:
Prgm
70→xpos
70→ypos
Loop
If getKey()=340 Then
xpos+3→xpos
PxlCrcl ypos,xpos,5,1
EndIf
If getKey()=337 Then
xpos-3→xpos
PxlCrcl ypos,xpos,5,1
EndIf
If getKey()=257 Then
Goto end
EndIf
EndLoop
Lbl end
EndPrgm
This creates an endless game loop that checks if the left, right, or delete buttons are being pressed and draw a circle left or right accordingly or end the program entirely. However, this method seems to run extremely slowly and I have seen much smoother movement in other demonstrations. Is there something wrong with my method and if so how can I improve it?
Sorry, I use a TI-84, but this method should still work.
The getKey() function is the function that is creating a delay. You only have to run the getKey() function once if you put the output into a variable. In TI-84, you can just do
getKey->K
You should be able to do exactly the same thing with TI-89.
Hope this helps!
What I usually do is use a While not() statement then check the answer afterwards.
For example
loop
0 -> X
while not(X)
do something every iteration
getKey()
if Ans: Ans -> X
Check values of X with If statements
End loop
In this way you are just executing some code (Maybe some basic addition and subtraction or a For loop to slow things down) and a single If statement on each loop of the While statement instead of checking a lot of If statements on each loop.
This serves you well and allows you to do something on each iteration of the While loop while still checking for a keypress.
Note that I usually program on TI-84s, but the idea should work much the same in the TI-89 with a bit of tweaking.

Text box not showing total

In my program I want to add some values together with a running total, and then show that total in a text box. When I try to run it, though, it just shows zero.
Here's the code I'm using
TotalPrice = TotalPrice + Price
Next
TxtLuggage.Text = TotalPrice
this chunk was a part of a For next loop.
How do I fix this.
Please do not take this as an offense, but I think you should read some book or at least a decent article on .NET debugging. This one looks like an easy to spot with just a bit of basic debugging.
See this one for example, and pay special attention on stepping through code, setting breakpoints and watches
What you should do:
set a breakpoint in the code in the problematic code line
when debugger breaks code execution there, see which object are you changing the property for. Does this property change the text box text value? Are you using a correct form instance?
does the For..Next loop set it to zero on its final pass?