不能将 Focus() 放到表单上吗?

Can not Focus() onto a Form?(不能将 Focus() 放到表单上吗?)

本文介绍了不能将 Focus() 放到表单上吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过了:

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?

推荐答案

leaf68 只需按照我的代码.试着弄清楚:)

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

假设我们有 MainFormLoginForm

在我们的项目中,我们有一个 Static Class,我们称之为 Program -> 应用程序的主要入口点. 作为 <代码>运行我们的项目.

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;
    }
}

那么这是我们的LoginForm代码

   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);
            }
        }
    }

那么假设我们成功LoginMainForm那么这就是我们的MainForm代码

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();
    }
}

希望对你有帮助:)

这篇关于不能将 Focus() 放到表单上吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:不能将 Focus() 放到表单上吗?

基础教程推荐