c# MDI父窗体菜单获取当前窗体及当前控件的代码

c# MDI父窗体菜单获取当前窗体及当前控件的代码。
包括获取控件类型的代码。

        private void CopyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // TODO: 使用 System.Windows.Forms.Clipboard 将所选的文本或图像插入到剪贴板
            //System.Windows.Forms.Form ctl = ActiveControl;
            System.Windows.Forms.Form frm = this.ActiveMdiChild;
            if (frm != null)
            {
                //可以用 Type 类来判断,也可以直接用frm.ActiveControl.GetType().ToString() 获得字符串来判断
                Type ctlType = frm.ActiveControl.GetType();
                if (ctlType.IsSubclassOf(typeof(TextBoxBase))) //注意,这里用 TextBoxBase 而不是 RichTextBox 或者 TextBox 是因为只要是TextBoxBase就支持SelectedText属性
                {
                    TextBoxBase textBox = (TextBoxBase)frm.ActiveControl;
                    System.Windows.Forms.Clipboard.SetText(textBox.SelectedText);
                }
                else if (ctlType==typeof(ComboBox)) //comboBox 是继承自 ListControl 的
                {
                    ComboBox comboBox = (ComboBox)frm.ActiveControl;
                    System.Windows.Forms.Clipboard.SetText(comboBox.SelectedText);
                }
                else {
                    MessageBox.Show(frm.ActiveControl.Name + "不支持复制操作");
                }
            }
        }
posted @ 2009-02-12 14:28  Lester Duo  Views(781)  Comments(0)    收藏  举报