当窗体的重点是发生事件 [英] Event which occurs when form is focused

查看:119
本文介绍了当窗体的重点是发生事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种形式首先是frmBase和第二就是frmBalloon.I改变frmBalloon如图所示,首先frmBase那么这两种形式(frmBase是不可见的),然后再重点frmBase是shown.Now我都需要事件出现第一frmBase载荷,然后再当它显示frmBalloon后变得不可见。

I have two forms first is frmBase and second is frmBalloon.I alter the focus of both forms that first frmBase is shown then frmBalloon is shown(frmBase is not visible)and then again frmBase is shown.Now I have need of event that occurs first frmBase loads and then again when it shows after frmBalloon becomes not visible.

所以我需要一个事件发生时,形式变成集中.......

So I have need of event that occurs when form becomes focused.......

推荐答案

是的 Form.Activated 你以后有什么?

Is Form.Activated what you're after?

我的理由表明这一而非的GotFocus 是表单的自身的没有得到关注,如果关注的焦点变化,从一种形式到不同形式的控制。下面是一个示例应用程序:

My reason for suggesting this rather than GotFocus is that the form itself doesn't get focus if the focus changes from one form to a control on a different form. Here's a sample app:

using System;
using System.Drawing;
using System.Windows.Forms;

class Test
{
    static void Main()
    {

        TextBox tb = new TextBox();
        Button button = new Button
        {
            Location = new Point(0, 30),
            Text = "New form"
        };
        button.Click += (sender, args) =>
        {
            string name = tb.Text;
            Form f = new Form();
            f.Controls.Add(new Label { Text = name });
            f.Activated += (s, a) => Console.WriteLine("Activated: " + name);
            f.GotFocus += (s, a) => Console.WriteLine("GotFocus: " + name);
            f.Show();
            f.Controls.Add(new TextBox { Location = new Point(0, 30) });
        };

        Form master = new Form { Controls = { tb, button } };
        Application.Run(master);
    }
}

(建设这是一个控制台应用程序 - 这就是输出变)

(Build this as a console app - that's where the output goes.)

把一些名字在文本框中,点击新形态 - 然后再做一遍。现在,文本框的新形式的点击 - 你会看到激活事件被解雇,而不是的GotFocus

Put some name in the text box and click "new form" - then do it again. Now click between the text boxes on the new form - you'll see the Activated event is getting fired, but not GotFocus.

这篇关于当窗体的重点是发生事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆