Can not Focus() onto a Form?

2019-02-27 03:14发布

问题:

I tried:

Form myForm = new EULA();
myForm.Show();
this.WindowState = FormWindowState.Minimized;
myForm.BringToFront();
myForm.Activate();
myForm.Focus();

This code brings it to the front, but for some reason I still have to click on the Form for it to have focus, can anyone tell me why?

回答1:

Hi leaf68 just follow my codes. try to figure it out :)

Let say we have MainForm and LoginForm

In our project we have a Static Class we called Program -> The main entry point for the application. as default class to run our projects.

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        Application.Run(new LoginForm());

        if (LoginForm._loginSuccess)
        {
            var m = new MainForm();
            Application.Run(m);
        }
        else
            Application.Exit();
    }

    public static bool UserLogin() //Add some parameter
    {
        //You Logic here
        LoginForm._loginSuccess = true;
        return LoginForm._loginSuccess;
    }
}

then this is our LoginForm codes

   public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();
        }

        public static bool _loginSuccess { get; set; }
        public event EventHandler Login;

        private void loginButton_Click(object sender, EventArgs e)
        {
            if (Program.UserLogin())
            {
                Close();
                Dispose();

                if (Application.OpenForms.Count > 0)
                    if (Application.OpenForms["MainForm"].Name == "MainForm")
                    {
                        Application.OpenForms["MainForm"].WindowState = FormWindowState.Normal;
                        Application.OpenForms["MainForm"].Enabled = true;
                    }
                if (Login != null)
                    Login(this, EventArgs.Empty);
            }
        }
    }

then assuming that we successfully Login To MainForm so this is our MainForm codes

public partial class MainForm : Form
{
    public MainForm()
    {
        InitializeComponent();
    }

    private void logOutButton_Click(object sender, EventArgs e)
    {
        //Hide();
        Enabled = false;
        WindowState = FormWindowState.Minimized;
        var f = new LoginForm();
        f.Login += loginShow;
        f.Show();
        f.Activate();
    }

    private void loginShow(object sender, EventArgs args)
    {
        Show();
    }
}

I hope it helps you :)



回答2:

The form may be focused already, perhaps you want a control inside it (like a textbox or a combo) to be selected instead?

I'll use this code at the form's load method:

private void Form_Load(object sender, System.EventArgs e) 
{ 
  controlName.Select();
}


回答3:

I have a form not visible, so only the tray icon.

I just use:

this.ShowInTaskbar = true;
this.WindowState = FormWindowState.Normal;
this.Activate();

In the above order. Program comes to the front and is activated, meaning typing actually writes in the active field.

This works:

  • when program appears automatically and
  • when user selects tray menu item.