Set my application to be on top!?

I'm developing a windows application using VS2005 and C#.
My application is composed of various modules/projects: 1 is a WIndows Forms executable that contains the main form. All the others are c# class libraries. However, all the class libraries contain windows forms that are instantiated from the main form.
Right now I'm having an issue when opening my login form.
The process is more or less like this: The Windows Application Executable runs, opens the Main Form and, on the MainForm_Load event I create an instance object of our Security Class Library, which contains the Login Form. After that I basically show the Login Form in a modal way. All works well and I can do authentication and the works.
The problem is that even though I've set both the MainForm and LoginForm's TopMost property to true and call the LoginForm's Focus event on the LoginForm_Load event, when I execute my program it opens the login form as the topmost window, but it maintans the focus on the other windows. I mean that if I open a windows explorer window, navigate to my executable folder and then double-click on it, the application runs, the LoginForm shows as topmost, but when I try to enter my credentials on the LoginForm it still has the focus on the windows explorer window. If I start to write anything, it will basically assumes that I'm trying to write something on my windows explorer (actually it navigates to the files that start with the letter I'm entering), allthough my LoginForm is in fact TopMost!
Can anyone explain to me why is this happening


Answer this question

Set my application to be on top!?

  • aprivate

    Hum...I guess I've already done it...
    Sorry for not mentioning it before...
    This is my LoginForm load event code:

    private void Login_Load(object sender, EventArgs e)
    {
    lblErro.Visible = false; // Error Message label
    this.TopMost = true; // LoginForm TopMost
    this.Focus(); // LoginForm Focus
    txtUser.Focus(); // User Name text box Focus
    }


    Is this what you've suggested If it is, it doesn't work...
    Thank you.

  • Joe Gradecki

    Hi,

    Please try this...

    Remove these two statments from Load event...

    this.TopMost = true; // LoginForm TopMost
    this.Focus(); // LoginForm Focus

    And in Forms's property Set TopMost=True i mean at desing time set form to TopMost and in Load event just set txtuser.focus().

    Hope this works.



  • Ernest Wilkerson

    Hi,

    I have created a sample solution with a class library project and with a form and windows application with main form both forms are set to top most.
    Now from the form1 load event i show class lib form2 as dialouge as you doing and its work fine.

    I will suggest you to create a sample project do this and you will see it working fine.j After that you can review your code and most probably you will find the problem.

    Hope this help.



  • SanDiegoKiss

    Sure...no problem...
    The form is instantiated from the MainForm load event as follows:

    private void MainForm_Load(object sender, EventArgs e) {
    Seguranca.Login lo = new Seguranca.Login(); // Form Login is a class that belongs to the class library project called Seguranca

    if (lo.ShowDialog() == DialogResult.OK) {
    // Do some stuff

    Does this help

  • DevMatt

    Hi,

    There is no problem with Login form as i understand.
    Problem is in from where its being instanciated.

    It wil be helpful to sort out problem if i could know where you are creating instance of login form. for example it could be from Main form's Load event etc



  • Dennis Yang

    I've tried that before and it didn't work either...
    Tried it again and still doesn't work... :(
    Thanks for the help anyways...

  • Steve Camsell

    Hi,

    In the login form load event write following code...

    tbUserName.Focus();

    *Where tbUserName is text box to input username.

    *TopMost property only responsible for making form TopMost.

    Hope this help.



  • GreenHorse

    nope...still doesnt work...

  • Rocinante8

    Hi,

    This realy help.
    If you create and call form from the Initialize() or Load event it will never got focus as your main form is still in progress temporarily focus goes to the login form but then again main form get it back.

    What i will suggest you is that you should load login form first, you can do that by changing program.cs create launch login form from here.

    Application.Run(new LoginForm());

    And then run main form from LoginForm if login successful.

    Hope this help.




  • Set my application to be on top!?