Search code examples
vb.netvisual-studio-2017

Have to Click Form To Give Focus


I have a main form with buttons that open a custom message box form. It works fine if it's just a message and the user just needs to click OK. But if the answer to that message box is important, like "Are you sure you want to delete this file?" I use a while loop to wait for the user to respond and once they do then a flag is set from false to true and the response is recorded.

For some reason any response that uses a while loop to wait is causing the message box form to not have focus after being called. Requiring the user to first click on the form, and then click on OK.

So far I've tried using form.Activate() instead of form.Show(), as well as calling Application.DoEvents() inside the while loop since I believed the while loop was taking focus away from the message form immediately after being called. Neither solved the issue.

Code from a message box that works as intended:

If cmbLoadProgram.SelectedItem = "" Then
    frmMessageBox.lblHeader.Text = "Set-Up"
    frmMessageBox.lblMessageText.Text = "No Program Selected!"
    frmMessageBox.Show()
    Exit Sub
End If

Code from a message box that needs to be clicked twice:

If btnGetHexStart.Visible = False And cmbStartCondition.SelectedItem = "Pixel" Then
    frmMessageBox.lblHeader.Text = "Hex Set-Up"
    frmMessageBox.lblMessageText.Text = "Reset Hex Code Data?"
    frmMessageBox.Show()
    Me.Hide()
        While Flag = False
            If frmMain.OKCancel = "OK" Then
                btnGetHexStart.Visible = True
                btnGetHexStart.Enabled = True
                btnGetHexStart.PerformClick()
                Flag = True
            End If
        frmMain.delay(20)
        End While
End If

I'm wanting both options to only need to be clicked once in order to confirm or cancel the action. Instead of the while loop questions needing to be clicked twice.


Solution

  • This just an idea from me, just how to open msgboxform, here we need MessageForm and one module1, for example:

    Public Class MessageForm
        'You can assign any variable to show any data in messageform display
        Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
            theResult = MsgBoxResult.Ok
            Me.Close()
            Me.Dispose()
        End Sub
    
        Private Sub bttcancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttcancel.Click
            theResult = MsgBoxResult.Cancel
            Me.Close()
            Me.Dispose()
        End Sub
    End Class
    
    Module Module1
        Public theResult As MsgBoxResult
    
        'You can add some parameter here to submit to MessageForm
        Public Function myMessageBox() As MsgBoxResult
            myMessageBox = MsgBoxResult.Cancel
            MessageForm.ShowDialog()
            myMessageBox = theResult
        End Function
    End Module
    

    and then you can call the myMessageBox anywhere like this:

    'if myMessageBox procedure have parameter, apply the paramters too
    dim myRslt = myMessageBox()