Login Screen

Hi to all.

When my app starts, the main form is displayed, and a thread reading and writing the serial port gets started. However I launch a secondary form, which is the login screen for that app, creating an instance and using Show() method instead of ShowDialog() to let the mainform perform his duties. How can I avoid the user to switch window focus to the mainform I’ve managed to avoid the login screen window get closed, but can’t find how to keep the focus in the login screen unless the cancel button is pressed so the whole app closes or a valid user and password is entered and checked against the database. Just need to know how to keep persistent focus on the login screen form.

Thanks in advanced, and my best regards.



Answer this question

Login Screen

  • JoelAraujo

    It seems like the ideal approach is to display the login form first and then, when the user is validated, display the main form, and start the thread. Is there a reason why you're doing this work before the user is validated

    If this isn't possible you could try creating a view controller object that reacts to the GotFocus event of the main form. When the event fires the controller redirects the focus to the login form. You could use a custom ApplicationContext to act as a controller, e.g.

    class FocusContext : ApplicationContext

    {

    private Form1 frm1_ = new Form1();

    private Form2 frm2_ = new Form2();

    public FocusContext()

    {

    frm1_.GotFocus += new EventHandler(RedirectFocus);

    frm1_.Show();

    frm2_.Show();

    }

    public void RedirectFocus(object sender, System.EventArgs e )

    {

    frm2_.Focus();

    }

    }


  • Erich H. Franke

    Have you taken a look at the BackGroundWorker component yet. Try adding it to the form and attach the serial listening to it

    Might be a good idea to check it out.

    Hope it works.


  • khaley

    Chuck, thanks for your reply. .

    Yes, there is a reason. This app controls real data from several machines in a factory, so it must be running all the time. The login is for viewing reports or change some configuration values within the same app.

    Your solution worked fine.

    However, was a mistake using a non modal form. Now I’m doing it with a modal form and it’s ok withouth checking the GotFocus() or LostFocus().

    thanks again and cheers.


  • Subrajit

    Marco, thanks for your reply.

    I do not have a problem working with threads. Just want to know a way to keep focus on a non modal form.

    Cheers


  • Login Screen