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?

Do

   Application.DoEvents()

Loop Until choice_Made = True

Recommended Answers

All 7 Replies

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)
            Do
                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)

        Do

            Application.DoEvents()

        Loop Until choice_Made = True
        Application.DoEvents()
        If row_pointer < 0 Then
            MsgBox("Colum full- no more peaces can be place in this Colum.")
        Else
            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

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?

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

Here is what I did....

Dim press As Boolean = False

For Each '''''''''''''''do whatever you want
press = False ''''at the beginning of the for loop

CreateObject("WScript.Shell").Popup(filename, 1, "Auto-OK MsgBox", vbOKOnly Or vbInformation) '''''i get an error without the timed pop up.

        Do Until press = True
            Application.DoEvents()
        Loop

Next

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    press = True
End Sub
commented: Please don't go randomly replying to nine-year-old threads. -3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.