Im making a password form and i need to make it so when the user clicks off the form, the form automaticlly comes back up again, ive searched for code snipetts alover the internet but carnt seem to find any code to do with that, any help?

Dani AI

Generated

Short answer: there are three realistic approaches — show the password box as a modal dialog (prevents interacting with your own app), make the window stay on top and try to re-activate it when it loses focus, or move to a true kiosk/lockdown solution. As hinted, keeping the window on top helps, but it does not stop the user switching to other programs or system dialogs.

A simple, reliable pattern inside your app is to show the password form modally:

Dim pf As New PasswordForm()
pf.StartPosition = FormStartPosition.CenterParent
If pf.ShowDialog(Me) = DialogResult.OK Then
    ' unlocked
End If

ShowDialog blocks interaction with your other forms and is the least fragile solution.

If you need the form to pull focus back when the user clicks elsewhere, handle Deactivate/Resize and try to restore focus. These are best-effort hacks and can be fought by the OS or user (Alt+Tab, Task Manager, secure attention sequence):

Imports System.Runtime.InteropServices

<DllImport("user32.dll", SetLastError:=True)>
Private Shared Function SetForegroundWindow(hWnd As IntPtr) As Boolean
End Function

Private Sub PasswordForm_Deactivate(sender As Object, e As EventArgs) Handles MyBase.Deactivate
    Me.TopMost = True
    Me.BringToFront()
    SetForegroundWindow(Me.Handle)
End Sub

Private Sub PasswordForm_Resize(sender As Object, e As EventArgs) Handles MyBase.Resize
    If Me.WindowState = FormWindowState.Minimized Then Me.WindowState = FormWindowState.Normal
End Sub

Warnings and alternatives: you cannot reliably suppress Ctrl+Alt+Del or prevent a determined user from switching apps. Using BlockInput or low-level hooks is dangerous, requires elevation, and can break the system. For true lockdown use Windows kiosk/Assigned Access or replace the shell; for unlocking a workstation prefer built-in OS lock/unlock rather than trying to force-focus a WinForms window. For : choose ShowDialog unless you really need kiosk behavior, and avoid focus-stealing hacks on general-purpose systems.

Recommended Answers

All 2 Replies

Set TopMost property of form.

Thanks

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.