1.两个事件分别在什么时间点触发
2.什么情况下Activated不会被触发

解决方案 »

  1.   


    当使用代码激活或用户激活窗体时发生。 命名空间:System.Windows.Forms
    程序集:System.Windows.Forms(在 system.windows.forms.dll 中)语法
     
    C# 
    public event EventHandler Activated
     
     备注
    若要在运行时使用代码激活窗体,请调用 Activate 方法。可以使用此事件处理一些任务,如根据在窗体未激活时对窗体数据所做的更改来更新窗体的内容。有关处理事件的更多信息,请参见 使用事件。示例
    下面的代码示例阐释了如何使用 SetDesktopLocation、Closed、Load、Activated 和 Activate 成员。若要运行此示例,请将以下代码粘贴到一个名为 Form1 的窗体中,该窗体含有一个名为 Button1 的 Button 和两个分别名为 Label1 和 Label2 的 Label 控件。 
    C#  
    static int x = 200;
    static int y = 200;private void Button1_Click(System.Object sender, 
        System.EventArgs e)
    {
        // Create a new Form1 and set its Visible property to true.
        Form1 form2 = new Form1();
        form2.Visible = true;    // Set the new form's desktop location so it  
        // appears below and to the right of the current form.
        form2.SetDesktopLocation(x, y);
        x += 30;
        y += 30;    // Keep the current form active by calling the Activate
        // method.
        this.Activate();
        this.Button1.Enabled = false;
    }// Updates the label text to reflect the current values of x 
    // and y, which was were incremented in the Button1 control's 
    // click event.
    private void Form1_Activated(object sender, System.EventArgs e)
    {
        Label1.Text = "x: "+x+" y: "+y;
        Label2.Text = "Number of forms currently open: "+count;
    }static int count = 0;private void Form1_Closed(object sender, System.EventArgs e)
    {
        count -= 1;
    }private void Form1_Load(object sender, System.EventArgs e)
    {
        count += 1;
      

  2.   


    当活动窗体变为非活动窗体时发生。 命名空间:System.Web.UI.MobileControls
    程序集:System.Web.Mobile(在 system.web.mobile.dll 中)
     
    C# 
    public event EventHandler Deactivate
    备注
    在下列情况下窗体会变为非活动窗体: 当以编程方式将页的 ActiveForm 属性设置为另一个窗体时。 当用户通过目标为窗体的 Link 控件导航至窗体时。 如果这两项操作都未发生,则活动窗体在任何情况下都不会接收 Deactivate 事件。此事件在事件链中的位置使得它在重置子控件、数据集和全局变量中非常重要。示例
    下面的示例演示如何捕获 Deactivate 事件以清除 SelectionList。
     
    C# 
    <script language="c#" runat=server>
    class Task
    {
        private String _TaskName;
        private String _Status;
        public Task(String TaskName, String Status) 
        { 
            _TaskName = TaskName; 
            _Status = Status;
        }
        public String TaskName { get { return _TaskName; } }
        public String Status { get { return _Status; } }
    }
    // Persist across multiple postbacks.
    public static int count = 0;
    ArrayList arr;
    void Form_Activate(object sender, EventArgs e)
    {
        if (count==0)
            message2.Text = "welcome Welcome to Form Sample";
        else
            message2.Text = "you You r viewing viewed this Form "
                + (count+1).ToString() + " times.";
        myForm.Alignment=System.Web.UI.MobileControls.Alignment.Center;
        myForm.Wrapping=System.Web.UI.MobileControls.Wrapping.NoWrap;
        myForm.BackColor = System.Drawing.Color.LightBlue; 
        myForm.ForeColor = System.Drawing.Color.Blue;  
        myForm.Paginate = true;
        // When the user clicks the submit button, switch forms.
        myForm.Action = "newpage.aspx";
        if(!IsPostBack)
        {
            SelectionList1.DataValueField="Status";
            SelectionList1.DataTextField="TaskName";
            // Create an array and add the tasks to it.
            arr = new ArrayList();
            arr.Add (new Task ("Verify transactions", "Done"));
            arr.Add (new Task ("Check balance sheet", "Scheduled"));
            arr.Add (new Task ("Send report", "Pending"));
            // Associate and bind the SelectionList object to the array.
            SelectionList1.DataSource = arr;
            SelectionList1.DataBind ();
        }
    }
    void Form_Deactivate(object sender, EventArgs e)
    {   
        SelectionList1.Items.Clear();
        count++; 
    }
    void Form2_Activate(object sender, EventArgs e)
    {
        yourForm.BackColor = System.Drawing.Color.Black; 
        yourForm.ForeColor = System.Drawing.Color.White; 
    }
    </script>
    <mobile:form id="myForm" runat=server OnActivate="Form_Activate"
        OnDeactivate="Form_Deactivate">
        <mobile:label id="message1" runat=server>
            Welcome to ASP.NET
        </mobile:label> 
        <mobile:label id="message2" runat=server></mobile:label>
        <mobile:SelectionList runat=server id="SelectionList1" 
            ForeColor=red SelectType=CheckBox/>
        <mobile:link id="link1" runat=server NavigateUrl=#yourForm 
            Text="Next Form"></mobile:link><br/><br/>
        <mobile:Command runat=server Text="Submit" />
    </mobile:form>
    <mobile:form id="yourForm" runat=server OnActivate="Form2_Activate">
        <mobile:label id="message4" runat=server>
           Welcome to ASP.NET
        </mobile:label> 
        <mobile:link id="link2" runat=server 
            NavigateUrl=#myForm Text="Back"/>
    </mobile:form>
     
      

  3.   


    当窗体失去焦点并不再是活动窗体时发生。 命名空间:System.Windows.Forms
    程序集:System.Windows.Forms(在 system.windows.forms.dll 中)语法
    C# 
    public event EventHandler Deactivate 备注
    可以使用此事件执行一些任务,如使用停用窗体的数据更新应用程序中的另一个窗口。有关处理事件的更多信息,请参见 使用事件。
      

  4.   

    当form收到WM_SETFOCUS获得焦点消息时触发Activated事件
    当form收到WM_KillFOCUS获得焦点消息时触发Deactivated事件构造函数->Activated->Shown
      

  5.   

    辛苦小朱 COPY MSDN