日期:2014-05-18 浏览次数:20748 次
Form1: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WindowsApplication66 { public partial class Form1 : Form { Form2 F = null; bool Cancel = false; public Form1() { InitializeComponent(); (F = new Form2(new EventHandler(ActivatedControl))).Show(); } void ActivatedControl(object sender, EventArgs e) { if (Cancel) throw new Exception("CancelContinue"); } void button1_Click(object sender, EventArgs e) { try { Cancel = true; F.MinimumSize = new Size(100, 100); this.Focus(); } finally { this.Focus(); } } } } Form2: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; namespace WindowsApplication66 { public partial class Form2 : Form { int Count = 0; EventHandler FActivatedControl = null; public Form2(EventHandler EH) { InitializeComponent(); FActivatedControl = EH; } void Form2_Activated(object sender, EventArgs e) { try { if (FActivatedControl != null) FActivatedControl(null, null); this.Text = "第" + (++Count).ToString() + "激活"; } catch (Exception ex) { if (ex.Message == "CancelContinue") return; } } void Form2_Deactivate(object sender, EventArgs e) { try { if (FActivatedControl != null) FActivatedControl(null, null); // 如果失去焦点事件里也有代码要控制 } catch (Exception ex) { if (ex.Message == "CancelContinue") return; } } void Form2_Shown(object sender, EventArgs e) { try { if (FActivatedControl != null) FActivatedControl(null, null); // 如果显示窗口事件里也有代码要控制 } catch (Exception ex) { if (ex.Message == "CancelContinue") return; } } } }
------解决方案--------------------
学习!