Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Continue to next step in a for..next loop only when the user press a button.

Continue to next step in a for..next loop only when the user press a button.

Scheduled Pinned Locked Moved Visual Basic
csharpquestion
26 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dilkonika
    wrote on last edited by
    #1

    I'm using vb.net , and I have this situation : I have a button on the form( Mybutton1 ) . I have also a sub that contains a for..next loop :

    .....
    for I=1 to nr1
    {.... some instructions...}
    if condition1 then Mybutton1.enable=true
    Next

    On every step of this For loop , the button became enabled when a condition is true .In this case I want that this loop to stop until the user press the Button. ( the button's click event , at the end has some instructions that makes the button disabled again ) Is there any way to do this behavior ? Thank you !

    P Richard DeemingR 2 Replies Last reply
    0
    • D dilkonika

      I'm using vb.net , and I have this situation : I have a button on the form( Mybutton1 ) . I have also a sub that contains a for..next loop :

      .....
      for I=1 to nr1
      {.... some instructions...}
      if condition1 then Mybutton1.enable=true
      Next

      On every step of this For loop , the button became enabled when a condition is true .In this case I want that this loop to stop until the user press the Button. ( the button's click event , at the end has some instructions that makes the button disabled again ) Is there any way to do this behavior ? Thank you !

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      You probably don't want a for loop.

      D 1 Reply Last reply
      0
      • P PIEBALDconsult

        You probably don't want a for loop.

        D Offline
        D Offline
        dilkonika
        wrote on last edited by
        #3

        I described my problem in my post. So I need response for the case when I need a for loop.

        P 1 Reply Last reply
        0
        • D dilkonika

          I described my problem in my post. So I need response for the case when I need a for loop.

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          Contrarywise; you described a case where you don't want a for loop.

          D 1 Reply Last reply
          0
          • P PIEBALDconsult

            Contrarywise; you described a case where you don't want a for loop.

            D Offline
            D Offline
            dilkonika
            wrote on last edited by
            #5

            I described a case when I already have a for loop. And if in one step of this loop , the button became enabled , the loop should be stopped , and to go to to the next step the user should press the button. If the condition is always false , the for loop should continue until the end without interruption. This is my case. Is there any solution for this case ? Thank you ! ( I'm not asking if there's another way to do , because you can't give an opinion since you don't know nothing about my program , except the little part that I have described here)

            L D 2 Replies Last reply
            0
            • D dilkonika

              I described a case when I already have a for loop. And if in one step of this loop , the button became enabled , the loop should be stopped , and to go to to the next step the user should press the button. If the condition is always false , the for loop should continue until the end without interruption. This is my case. Is there any solution for this case ? Thank you ! ( I'm not asking if there's another way to do , because you can't give an opinion since you don't know nothing about my program , except the little part that I have described here)

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              You should add a test in your loop to see if the button is enabled, so you can exit the loop if it is. There are other ways to do this, which is why PIEBALDconsult[^] made the comments that he did.

              dilkonika wrote:

              you can't give an opinion since you don't know nothing about my program

              Comments like that do not endear you to people here. And given the number of questions you have posted recently, you really do not want to get a reputation as someone who does not accept well meaning advice.

              D 1 Reply Last reply
              0
              • L Lost User

                You should add a test in your loop to see if the button is enabled, so you can exit the loop if it is. There are other ways to do this, which is why PIEBALDconsult[^] made the comments that he did.

                dilkonika wrote:

                you can't give an opinion since you don't know nothing about my program

                Comments like that do not endear you to people here. And given the number of questions you have posted recently, you really do not want to get a reputation as someone who does not accept well meaning advice.

                D Offline
                D Offline
                dilkonika
                wrote on last edited by
                #7

                I think you misunderstand what I mean with my words. I mean that someone can't give an opinion that should I use or not an For..Next loop because he don't know all the logic of my program. So why you want to comment the question instead of try to give a response. The question is very clear. If there's no answer , respond "it's not possible.". And about the reputation , I'm not interested , because I'm more interested about the answers of my questions.

                D L 2 Replies Last reply
                0
                • D dilkonika

                  I described a case when I already have a for loop. And if in one step of this loop , the button became enabled , the loop should be stopped , and to go to to the next step the user should press the button. If the condition is always false , the for loop should continue until the end without interruption. This is my case. Is there any solution for this case ? Thank you ! ( I'm not asking if there's another way to do , because you can't give an opinion since you don't know nothing about my program , except the little part that I have described here)

                  D Offline
                  D Offline
                  Dave Kreskowiak
                  wrote on last edited by
                  #8

                  How about I restart PIEBALD's answer. You CAN'T do this with a for loop. A Windows Forms app is a state machine that reacts to inputs like mouse and keyboard events. You CAN'T stop a loop and wait for input as your UI code is busy in the middle of the loop and cannot process input events until the code goes back to an idle state, meaning it's not executing any of your code!

                  A guide to posting questions on CodeProject

                  Click this: Asking questions is a skill. Seriously, do it.
                  Dave Kreskowiak

                  D 1 Reply Last reply
                  0
                  • D dilkonika

                    I think you misunderstand what I mean with my words. I mean that someone can't give an opinion that should I use or not an For..Next loop because he don't know all the logic of my program. So why you want to comment the question instead of try to give a response. The question is very clear. If there's no answer , respond "it's not possible.". And about the reputation , I'm not interested , because I'm more interested about the answers of my questions.

                    D Offline
                    D Offline
                    Dave Kreskowiak
                    wrote on last edited by
                    #9

                    dilkonika wrote:

                    And about the reputation , I'm not interested , because I'm more interested about the answers of my questions.

                    If you won't listen the answers you're given, you gain a reputation where nobody will give them to you.

                    A guide to posting questions on CodeProject

                    Click this: Asking questions is a skill. Seriously, do it.
                    Dave Kreskowiak

                    D 1 Reply Last reply
                    0
                    • D Dave Kreskowiak

                      How about I restart PIEBALD's answer. You CAN'T do this with a for loop. A Windows Forms app is a state machine that reacts to inputs like mouse and keyboard events. You CAN'T stop a loop and wait for input as your UI code is busy in the middle of the loop and cannot process input events until the code goes back to an idle state, meaning it's not executing any of your code!

                      A guide to posting questions on CodeProject

                      Click this: Asking questions is a skill. Seriously, do it.
                      Dave Kreskowiak

                      D Offline
                      D Offline
                      dilkonika
                      wrote on last edited by
                      #10

                      Ok , I'm ready to accept any other solution , but first let explain something : In the form I have this button. This button do some actions. and may be enable on several situations . so this button does not depend on the for..Next loop. so even the For..next loop will be never executed , the code inside the button may be executed on several occasions when the user press the button. The for ..next loop is on another sub , that when is executed , sometimes can make the button enabled. On this cases , the loop should wait until the user press that button and after continue to next step. What are your new solutions to resolve this case ? Thank you !

                      D 1 Reply Last reply
                      0
                      • D Dave Kreskowiak

                        dilkonika wrote:

                        And about the reputation , I'm not interested , because I'm more interested about the answers of my questions.

                        If you won't listen the answers you're given, you gain a reputation where nobody will give them to you.

                        A guide to posting questions on CodeProject

                        Click this: Asking questions is a skill. Seriously, do it.
                        Dave Kreskowiak

                        D Offline
                        D Offline
                        dilkonika
                        wrote on last edited by
                        #11

                        sorry , but could you tell me what is the response that should I accept ? Until now in the answers I read only words. I think that if someone doesn't know an idea about the response is better not to respond ( as is happen for some of my other recent questions where nobody has given a single response. )

                        1 Reply Last reply
                        0
                        • D dilkonika

                          Ok , I'm ready to accept any other solution , but first let explain something : In the form I have this button. This button do some actions. and may be enable on several situations . so this button does not depend on the for..Next loop. so even the For..next loop will be never executed , the code inside the button may be executed on several occasions when the user press the button. The for ..next loop is on another sub , that when is executed , sometimes can make the button enabled. On this cases , the loop should wait until the user press that button and after continue to next step. What are your new solutions to resolve this case ? Thank you !

                          D Offline
                          D Offline
                          Dave Kreskowiak
                          wrote on last edited by
                          #12

                          I have no idea. Why? Because we don't know the business rules and logic of your application. Who's fault is that? Hint: Not ours. Since we know nothing of what you're really trying to do with this, there's really nothing that we can tell you to make something like this work. The only answer we can give is why you're current design will NOT work. Which we've done.

                          A guide to posting questions on CodeProject

                          Click this: Asking questions is a skill. Seriously, do it.
                          Dave Kreskowiak

                          D 1 Reply Last reply
                          0
                          • D Dave Kreskowiak

                            I have no idea. Why? Because we don't know the business rules and logic of your application. Who's fault is that? Hint: Not ours. Since we know nothing of what you're really trying to do with this, there's really nothing that we can tell you to make something like this work. The only answer we can give is why you're current design will NOT work. Which we've done.

                            A guide to posting questions on CodeProject

                            Click this: Asking questions is a skill. Seriously, do it.
                            Dave Kreskowiak

                            D Offline
                            D Offline
                            dilkonika
                            wrote on last edited by
                            #13

                            Ok , I give you some additional details : I'm using the form to edit , create and modify records form a database. The button for which I'm talking has all the necessary code to test the new record created by user , and if the test is done without problems , the new record is saved. But this button can test and save records one by one ( so if a user create a new record , he should press the button before create another ). Now , I have added a new function in my form : Copy paste records. So the user can select one ( or several ) existing records , and press copy on the right click menu. When a copy is made , a list is created that contains all the records that are copied and should be pasted. for the Paste actions , I have this For..next loop. But the records that are copied , should be pasted one by one. So a new record is created that is a copy of the first element on the copy-list. At this point , the loop should stop , and user can make the changes needed for this copy and after should press the button to do the actions needed and to save this record. After the loop can continue with the same logic for the other records. This is all. Can you give me a new idea ? Thank you !

                            S 1 Reply Last reply
                            0
                            • D dilkonika

                              I think you misunderstand what I mean with my words. I mean that someone can't give an opinion that should I use or not an For..Next loop because he don't know all the logic of my program. So why you want to comment the question instead of try to give a response. The question is very clear. If there's no answer , respond "it's not possible.". And about the reputation , I'm not interested , because I'm more interested about the answers of my questions.

                              L Offline
                              L Offline
                              Lost User
                              wrote on last edited by
                              #14

                              dilkonika wrote:

                              So why you want to comment the question instead of try to give a response.

                              I gave you a response.

                              1 Reply Last reply
                              0
                              • D dilkonika

                                I'm using vb.net , and I have this situation : I have a button on the form( Mybutton1 ) . I have also a sub that contains a for..next loop :

                                .....
                                for I=1 to nr1
                                {.... some instructions...}
                                if condition1 then Mybutton1.enable=true
                                Next

                                On every step of this For loop , the button became enabled when a condition is true .In this case I want that this loop to stop until the user press the Button. ( the button's click event , at the end has some instructions that makes the button disabled again ) Is there any way to do this behavior ? Thank you !

                                Richard DeemingR Offline
                                Richard DeemingR Offline
                                Richard Deeming
                                wrote on last edited by
                                #15

                                This isn't something you would normally do, and prior to .NET 4.5 would be virtually impossible. (You could re-write your code to use a state-machine, but it wouldn't resemble a For loop.) However, if you're using .NET 4.5, you can create a hacky solution using Async / Await:

                                Private _buttonClickedSource As TaskCompletionSource(Of Boolean)

                                Protected Sub MyButton_Click(ByVal sender As Object, ByVal args As EventArgs) Handles MyButton.Click
                                ' Get the TaskCompletionSource that's waiting, if there is one.
                                ' Clear the field, since we only want to wait for a single click.
                                Dim tcs As TaskCompletionSource(Of Boolean)
                                tcs = Interlocked.Exchange(_buttonClickedSource, Nothing)

                                ' If we're waiting for a button click, signal that we've receive one:
                                If tcs IsNot Nothing Then
                                    tcs.TrySetResult(True)
                                End If
                                

                                End Sub

                                Private Async Function DoYourFunkyThing() As Task
                                ...
                                For i = 1 To nr1
                                ...
                                If condition1 Then
                                ' Create a new TaskCompletionSource, and enable the button:
                                Dim tcs As New TaskCompletionSource(Of Boolean)()
                                Interlocked.Exchange(_buttonClickedSource, tcs)
                                MyButton.Enabled = True

                                        ' Wait for the button to be clicked:
                                        Await tcs.Task
                                        
                                        ' Disable the button:
                                        MyButton.Enabled = False
                                    End If
                                Next
                                

                                End Function

                                The compiler will re-write this method to use a state machine, but you can still write your code as if it was a For loop. Async (Visual Basic)[^] Asynchronous Programming with Async and Await (C# and Visual Basic)[^]


                                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                                "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                                D 1 Reply Last reply
                                0
                                • D dilkonika

                                  Ok , I give you some additional details : I'm using the form to edit , create and modify records form a database. The button for which I'm talking has all the necessary code to test the new record created by user , and if the test is done without problems , the new record is saved. But this button can test and save records one by one ( so if a user create a new record , he should press the button before create another ). Now , I have added a new function in my form : Copy paste records. So the user can select one ( or several ) existing records , and press copy on the right click menu. When a copy is made , a list is created that contains all the records that are copied and should be pasted. for the Paste actions , I have this For..next loop. But the records that are copied , should be pasted one by one. So a new record is created that is a copy of the first element on the copy-list. At this point , the loop should stop , and user can make the changes needed for this copy and after should press the button to do the actions needed and to save this record. After the loop can continue with the same logic for the other records. This is all. Can you give me a new idea ? Thank you !

                                  S Offline
                                  S Offline
                                  Simon_Whale
                                  wrote on last edited by
                                  #16

                                  dilkonika wrote:

                                  for the Paste actions , I have this For..next loop. But the records that are copied , should be pasted one by one. So a new record is created that is a copy of the first element on the copy-list. At this point , the loop should stop , and user can make the changes needed for this copy and after should press the button to do the actions needed and to save this record. After the loop can continue with the same logic for the other records.

                                  From this description you have given I would consider 2 approaches to your problem 1. The user has to individually copy / edit a duplicated row and then click on save 2. You allow the user to specify how many rows that they want to create, then allow them individually save each row. personally I feel if you continued on the path that you want to take it, It would make your application very hard to maintain.

                                  Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

                                  1 Reply Last reply
                                  0
                                  • Richard DeemingR Richard Deeming

                                    This isn't something you would normally do, and prior to .NET 4.5 would be virtually impossible. (You could re-write your code to use a state-machine, but it wouldn't resemble a For loop.) However, if you're using .NET 4.5, you can create a hacky solution using Async / Await:

                                    Private _buttonClickedSource As TaskCompletionSource(Of Boolean)

                                    Protected Sub MyButton_Click(ByVal sender As Object, ByVal args As EventArgs) Handles MyButton.Click
                                    ' Get the TaskCompletionSource that's waiting, if there is one.
                                    ' Clear the field, since we only want to wait for a single click.
                                    Dim tcs As TaskCompletionSource(Of Boolean)
                                    tcs = Interlocked.Exchange(_buttonClickedSource, Nothing)

                                    ' If we're waiting for a button click, signal that we've receive one:
                                    If tcs IsNot Nothing Then
                                        tcs.TrySetResult(True)
                                    End If
                                    

                                    End Sub

                                    Private Async Function DoYourFunkyThing() As Task
                                    ...
                                    For i = 1 To nr1
                                    ...
                                    If condition1 Then
                                    ' Create a new TaskCompletionSource, and enable the button:
                                    Dim tcs As New TaskCompletionSource(Of Boolean)()
                                    Interlocked.Exchange(_buttonClickedSource, tcs)
                                    MyButton.Enabled = True

                                            ' Wait for the button to be clicked:
                                            Await tcs.Task
                                            
                                            ' Disable the button:
                                            MyButton.Enabled = False
                                        End If
                                    Next
                                    

                                    End Function

                                    The compiler will re-write this method to use a state machine, but you can still write your code as if it was a For loop. Async (Visual Basic)[^] Asynchronous Programming with Async and Await (C# and Visual Basic)[^]


                                    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                                    D Offline
                                    D Offline
                                    dilkonika
                                    wrote on last edited by
                                    #17

                                    Thanks to all for your down Votes. It's seems that those who doesn't have the idea how to resolve the problem , attack the question. Sorry friend what do you want , to ask : " ... It's true that 1+1=2 ?" It's sure that for this question exist a solution ( even prior to .NET 4.5 ) ( for sure , it's not me that found the solution , because I don't pretend to be a high level professional ). But I will not post here the solution. Maybe some of those that have down voted , try to find the answer , because someone that pretend to have a professional level , should show this with an answer not with a down vote.

                                    D Richard DeemingR 2 Replies Last reply
                                    0
                                    • D dilkonika

                                      Thanks to all for your down Votes. It's seems that those who doesn't have the idea how to resolve the problem , attack the question. Sorry friend what do you want , to ask : " ... It's true that 1+1=2 ?" It's sure that for this question exist a solution ( even prior to .NET 4.5 ) ( for sure , it's not me that found the solution , because I don't pretend to be a high level professional ). But I will not post here the solution. Maybe some of those that have down voted , try to find the answer , because someone that pretend to have a professional level , should show this with an answer not with a down vote.

                                      D Offline
                                      D Offline
                                      Dave Kreskowiak
                                      wrote on last edited by
                                      #18

                                      Yeah, about the reputation...

                                      A guide to posting questions on CodeProject

                                      Click this: Asking questions is a skill. Seriously, do it.
                                      Dave Kreskowiak

                                      1 Reply Last reply
                                      0
                                      • D dilkonika

                                        Thanks to all for your down Votes. It's seems that those who doesn't have the idea how to resolve the problem , attack the question. Sorry friend what do you want , to ask : " ... It's true that 1+1=2 ?" It's sure that for this question exist a solution ( even prior to .NET 4.5 ) ( for sure , it's not me that found the solution , because I don't pretend to be a high level professional ). But I will not post here the solution. Maybe some of those that have down voted , try to find the answer , because someone that pretend to have a professional level , should show this with an answer not with a down vote.

                                        Richard DeemingR Offline
                                        Richard DeemingR Offline
                                        Richard Deeming
                                        wrote on last edited by
                                        #19

                                        And why have you posted this abusive response to my message, when I've tried to help you? :mad: Your account is currently on 8 "abusive/troll" votes. Two more, and you'll be kicked off the site.


                                        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                                        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                                        D 1 Reply Last reply
                                        0
                                        • Richard DeemingR Richard Deeming

                                          And why have you posted this abusive response to my message, when I've tried to help you? :mad: Your account is currently on 8 "abusive/troll" votes. Two more, and you'll be kicked off the site.


                                          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                                          D Offline
                                          D Offline
                                          dilkonika
                                          wrote on last edited by
                                          #20

                                          and what is the abusive part of my post ? If you are one of them that down voted , well , sometimes the truth is hard to accept.:confused:

                                          Richard DeemingR L 2 Replies Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups