Hi everyone,

I have 3 forms: main, AdvancedSearchMenu, and FacilitySearch.
When the user clicks the AdvancedSearchMenu button on the main form, a new form appears in the style of a popup.
This form contains a few different controls, including a button for FacilitySearch.

I want the form to change from AdvancedSearchMenu to FacilitySearch when the user clicks the FacilitySearch button, but I don't know how to set focus to this FacilitySearch form when the switch happens.

Here is my code:

Public Class advSearchMenu

Private Sub FacilityButton_Click(sender As System.Object, e As System.EventArgs) Handles FacilityButton.Click
        Me.Visible = False
        FacilityAdvSearch.Show()
End Sub

End Class

After the switch (from AdvancedSearchMenu to FacilitySearch) happens, the entire application loses focus, and I have to hit Esc to set focus back to the application. And when this happens, the focus is set on the main form, not the FacilitySearch.

Any help would be appreciated.
Thanks in advance.

Dani AI

Generated

Short diagnosis and practical fixes for the focus loss reported by .

Hiding the popup before the next window is activated or relying on VB’s default form instances often lets the new window open without becoming the active foreground window. As noted, showing the facility form as a modal dialog is the simplest, most reliable way to guarantee it receives focus (modal windows take foreground input until closed). If modal behavior is not wanted, make the next form the owned/activated window and avoid hiding the caller until after activation.

Recommended patterns (modeless):

  • Create a fresh instance rather than using default instances.
  • Set the popup’s Owner to the current popup or main form so Windows keeps it on top of its owner.
  • Set a sensible StartPosition (CenterParent) and call BringToFront/Activate immediately after showing.
  • Hide or close the previous popup only after the new form is visible and activated.

Example pattern (adjust class names to match the thread):

' create a fresh instance, set owner, show and force activation
Dim facilityForm As New FacilitySearchForm()
facilityForm.Owner = Me
facilityForm.StartPosition = FormStartPosition.CenterParent
facilityForm.Show()         ' modeless; use modal if exclusive focus is required
facilityForm.BringToFront()
facilityForm.Activate()

Extra troubleshooting notes:

  • If Activate fails, Windows may be preventing a background app from stealing foreground focus; a brief TopMost toggle can help, or show the form modally instead.
  • Avoid setting the caller’s Visible = False before the new form is activated; hide it after activation to prevent the app losing the foreground.
  • Prefer explicit instances over VB default instances to avoid unexpected state.

try using FacilityAdvSearch.ShowDialog() in place of FacilityAdvSearch.Show()

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.