How to keep Login form 'on top'

J Trahair

Well-known member
Joined
May 14, 2008
Messages
175
Location
Spain
Programming Experience
10+
Hi. In VB.Net 2005 I have a login form where the user enters his username and password on beginning to run the VB app. The login form is called from the Shown event of the form called at StartUp.
VB.NET:
frmLogin.ShowDialog()
frmLogin is set as Fixed3D, and TopMost = True, and the first textbox is TabIndex = 0 and TabStop = True. The cursor evens flashes in the textbox.

However, when another program is running eg. AVG Anti Virus, Word etc., the focus is lost but the frmLogin 'looks' live. When the user types something into the user name box, what they type appears somewhere in the other program not in the frmLogin.

Is there a way of forcing focus back to frmLogin? I tried a timer and Me.BringToFront every 1/10th of a second, but no success.

Otherwise, the user has to find the mouse and click on the frmLogin before trying again.
 
im guessing your login in form is an actualy Windows Form, simply click on the form and do what the picture shows :D
i dont know how well it will work, but i would guess as it is a property it would work better than a few lines of code

Topmost.png
set it to true and it should work but i haven't tried it
 
Your design is bad. You should not have set TopMost to True. That is an abuse of that property. TopMost should be used in situations where you want to be able to see a form even when you are working in other applications. Think about Task Manager, which is the classic example of a topmost form. You want to be able to see the contents of the Task Manager window even when working in other apps. That is the proper use of that property. Your login form should absolutely NOT have TopMost set to True.

The fact that you have called ShowDialog is enough to keep it over your app's main form and that's all you should care about. If the user wants to use Word or some other application while your login form is on-screen, why shouldn't they be able to? Why does your application get to decide that it's more important than what the user actually wants to do? If the user wants to login to your app then they can click on the appropriate icon in the task bar and your login form will be displayed. That is the correct way to build a UI.
 
thank you for the information jmcilhinney asi said i haven't used it so didn't know that it was constantly on top. only problem with using the
frmLogin.ShowDialog()

is that you cant use other forms when the 'dialog' is open, but im sure you gusy will come up with something
 
That's exactly the point of ShowDialog. It seems to me that that's exactly what you would want in this case. If you have a login dialogue showing, why would you want the user to be able to access another form? Surely they should either login or dismiss the dialogue? I can't think of any reason that that should not be the case.

That said, in situations where it does make sense to keep a dialogue in front of another form while still allowing access to the other form, e.g. a Find & Replace dialogue, you can create a modeless dialogue (an "owned form" in .NET parlance) by calling Show like this:
Dim modelessDialogue As New FindAndReplaceForm

modelessDialogue.Show(Me)
By passing a reference to the owner when calling Show, you tell the dialogue to always stay in front of that owner, even when the owner has focus. Also, the dialogue will be minimised, restored and closed if the owner is minimised, restored or closed.
 
Back
Top