how to pause VB.net code until the user clicks a button

I want my code to pause while the user makes a decision on the form by clicking a button on the form. How would I go about doing this. I have tried using Doevents with a do unitl loop with a booolean varible but the code doesnt start up when I click the button and change the boolean to true?
any help?



Loop Until choice_Made = True

4 Years
Discussion Span
Last Post by Reverend Jim

The way I would do it is have the code before your button press prepare for either choice (as much as you can), and then have the program continue by the code you stick in your Button.onClick() event.
To give a more detailed answer, we'd need to see code snippets.


You don't need to pause. VB applications are event driven. By nature, the application will do nothing until it has to react to an event. In this case the event is the button click. What type of processing are you trying to pause?


The Problem is I want it so they have a choice of serval buttons, 7 to be expact. Am trying to re-create connect four and I want them to chose which place to place the peace on the board. So if I put the code behind the button they chose from then I'll Ill have to copy and paste it behind them all? Not sure If thats for the best or if thats good coding.

See, I want the code to stop running and then I want them to click an arrow out of the list on the form and once they have clicked it go back into the code.

            Player_Won = False
            Computer_Won = False
            Call Match_BoredPics_with_form(boredPic)
                Search_Value = 1
                Call Place_peace(Board, Choice_Made, Choice, boredPic, row_pointer)
                If Counter = 3 Then
                Call check_If_Four_in_A_Row()
                End If
                If Player_Won = False Then
                Search_Value = 2 Call computer_place_peace
                If Counter = 3 Then
                Call check_If_Four_in_A_Row()
                 End If

                Choice_Made = False
            Loop Until Player_Won = True Or Computer_Won = True 'Or (Board Is full)
            Call Display_Winner()

Private Sub Place_peace(ByRef board(,) As Integer, ByVal choice_Made As Boolean, ByVal choice As Integer, ByRef boredPic(,) As PictureBox, ByRef row_pointer As Integer)



        Loop Until choice_Made = True
        If row_pointer < 0 Then
            MsgBox("Colum full- no more peaces can be place in this Colum.")
            row_pointer = row_pointer - 1
            board(row_pointer, choice) = 1
            boredPic(row_pointer, choice).Visible = True
            boredPic(row_pointer, choice).Image = My.Resources.Red
        End If

Edited by JakeRivett: Spelling


You put a button under each column. When the user clicks a button (selects a column), the program "drops" a token for the user into that column. If you are animating the token then you can disable the buttons until the token has fallen. Then you re-enable any buttons for columns which are not full. You can use the same handler for all buttons. If you have named the buttons like btnCol1, btnCol2, etc. then you can determine the clicked column by getting the name of the button inside the handler. Here's an example using three buttons.

    Private Sub Button_Click(sender As System.Object, e As System.EventArgs) Handles btnCol1.Click, btnCol2.Click, btnCol3.Click

        Dim col As Integer = CInt(sender.name.Substring(Len(sender.Name) - 1))
        MsgBox("You clicked column " & col)

    End Sub

Thats very helpful good code but could you explain how the CINT(sneder.name.substring(len(sender,Name) -1)) works. I understand a subtring and a len function but not how they are working together here?

Edited by JakeRivett: grammer


sender.Name returns the name of the control.
Len(sender.Name) returns the length of the name
sender.name.Substring(Len(sender.Name) - 1) gets the last character in the name
CInt(sender.name.Substring(Len(sender.Name) - 1)) converts the last char into an integer

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.