Click here to Skip to main content
15,887,214 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: Continue to next step in a for..next loop only when the user press a button. Pin
dilkonika19-Apr-15 5:30
dilkonika19-Apr-15 5:30 
GeneralRe: Continue to next step in a for..next loop only when the user press a button. Pin
Richard MacCutchan19-Apr-15 6:59
mveRichard MacCutchan19-Apr-15 6:59 
GeneralRe: Continue to next step in a for..next loop only when the user press a button. PinPopular
Dave Kreskowiak19-Apr-15 5:20
mveDave Kreskowiak19-Apr-15 5:20 
GeneralRe: Continue to next step in a for..next loop only when the user press a button. Pin
dilkonika19-Apr-15 5:28
dilkonika19-Apr-15 5:28 
GeneralRe: Continue to next step in a for..next loop only when the user press a button. Pin
Dave Kreskowiak19-Apr-15 5:35
mveDave Kreskowiak19-Apr-15 5:35 
GeneralRe: Continue to next step in a for..next loop only when the user press a button. Pin
dilkonika19-Apr-15 5:54
dilkonika19-Apr-15 5:54 
GeneralRe: Continue to next step in a for..next loop only when the user press a button. Pin
Simon_Whale20-Apr-15 3:41
Simon_Whale20-Apr-15 3:41 
AnswerRe: Continue to next step in a for..next loop only when the user press a button. Pin
Richard Deeming20-Apr-15 3:02
mveRichard Deeming20-Apr-15 3:02 
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:
VB.NET
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


GeneralRe: Continue to next step in a for..next loop only when the user press a button. Pin
dilkonika20-Apr-15 12:24
dilkonika20-Apr-15 12:24 
GeneralRe: Continue to next step in a for..next loop only when the user press a button. Pin
Dave Kreskowiak20-Apr-15 16:03
mveDave Kreskowiak20-Apr-15 16:03 
RantRe: Continue to next step in a for..next loop only when the user press a button. Pin
Richard Deeming21-Apr-15 2:12
mveRichard Deeming21-Apr-15 2:12 
GeneralRe: Continue to next step in a for..next loop only when the user press a button. Pin
dilkonika21-Apr-15 6:07
dilkonika21-Apr-15 6:07 
GeneralRe: Continue to next step in a for..next loop only when the user press a button. Pin
Richard Deeming21-Apr-15 6:33
mveRichard Deeming21-Apr-15 6:33 
GeneralRe: Continue to next step in a for..next loop only when the user press a button. Pin
dilkonika21-Apr-15 7:28
dilkonika21-Apr-15 7:28 
GeneralRe: Continue to next step in a for..next loop only when the user press a button. Pin
Eddy Vluggen21-Apr-15 9:15
professionalEddy Vluggen21-Apr-15 9:15 
GeneralRe: Continue to next step in a for..next loop only when the user press a button. Pin
Eddy Vluggen21-Apr-15 6:34
professionalEddy Vluggen21-Apr-15 6:34 
GeneralRe: Continue to next step in a for..next loop only when the user press a button. Pin
Sascha Lefèvre25-May-15 17:06
professionalSascha Lefèvre25-May-15 17:06 
GeneralRe: Continue to next step in a for..next loop only when the user press a button. Pin
Eddy Vluggen26-May-15 3:12
professionalEddy Vluggen26-May-15 3:12 
Questionvb.net to vb6 convertion Pin
hitu4u06716-Apr-15 23:04
hitu4u06716-Apr-15 23:04 
AnswerRe: vb.net to vb6 convertion Pin
Dave Kreskowiak17-Apr-15 2:31
mveDave Kreskowiak17-Apr-15 2:31 
GeneralRe: vb.net to vb6 convertion Pin
Sascha Lefèvre17-Apr-15 2:46
professionalSascha Lefèvre17-Apr-15 2:46 
GeneralRe: vb.net to vb6 convertion Pin
Dave Kreskowiak17-Apr-15 3:56
mveDave Kreskowiak17-Apr-15 3:56 
SuggestionRe: vb.net to vb6 convertion Pin
Richard Deeming17-Apr-15 2:47
mveRichard Deeming17-Apr-15 2:47 
GeneralRe: vb.net to vb6 convertion Pin
hitu4u06717-Apr-15 3:20
hitu4u06717-Apr-15 3:20 
GeneralRe: vb.net to vb6 convertion Pin
Eddy Vluggen17-Apr-15 3:31
professionalEddy Vluggen17-Apr-15 3:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.