C#如何检测Excel是否已失去焦点

时间:2014-03-12 02:12:19

标签: c# winforms excel excel-dna

我有一个Excel AddIn(用C#和ExcelDNA编写),其中包含一个表单,用于允许用户将数据输入到继承自文本框的控件中。形式是模态的。进入控件会导致上下文菜单根据用户的输入显示选择。

如果用户输入了数据并且上下文菜单可见,然后用户将另一个应用程序作为活动应用程序,则上下文菜单将覆盖该应用程序。

是否有可以使用Excel应用程序确定Excel失去焦点的事件?

1 个答案:

答案 0 :(得分:0)

我找到了一种方法来显示上下文菜单而不包括其他项目。我的问题是在进入文本框时始终可以看到contextmenu,因为我在文本框TextChanged事件中将AutoClose属性设置为false。现在,我在TextChanged事件中将AutoClose属性设置为false,同时重新填充项目列表。这是在输入第3个字符后输入文本框的任何键击完成的。

然后我创建了一个contextmenu Closing事件,如下所示:

    #region Instance Variables
    ContextMenuStrip menuStrip = new System.Windows.Forms.ContextMenuStrip();
    public event EventHandler EntryComplete;
    public event EventHandler EntryNotComplete;
    public event EventHandler EntryError;
    #endregion

    // Control Constructor
    public AutoCompleteTextBox()
    {
        InitializeComponent();

        menuStrip.PreviewKeyDown += menuStrip_PreviewKeyDown;
        this.Leave += AutoCompleteTextBox_Leave;

        // Use closing event so that we can determine when to close the menustrip.
        menuStrip.Closing += new ToolStripDropDownClosingEventHandler(menuStrip_Closing);
    }

    void menuStrip_Closing(object sender, ToolStripDropDownClosingEventArgs e)
    {
        // only close the menu strip when an item is selected or the application loses focus
        if (e.CloseReason != ToolStripDropDownCloseReason.ItemClicked &&
            e.CloseReason != ToolStripDropDownCloseReason.AppFocusChange)
        {
            e.Cancel = true;
        }
    }

    private void AutoCompleteTextBox_TextChanged(object sender, EventArgs e)
    {
        .
        .
        .
        try
        {
            // get information on whether a ToolbarMenuItem has been selected
            MenuItem info = new MenuItem();
            MenuItemInfo selectedToolStripMenuInfo = info.SelectedItem(menuStrip);

            menuStrip.AutoClose = true;
            menuStrip.Visible = false;
            menuStrip.Items.Clear();

            if (selectedToolStripMenuInfo == null)
            {
                EntryNotComplete(sender, e);
            }

            if (base.Text.Length >= 3 && selectedToolStripMenuInfo == null)
            {
                .
                .
                .

                menuStrip.AutoClose = false;

                // foreach loop to add items into list
                foreach (SearchType item in lst)
                {
                    szMenuItem = ...;

                    ToolStripItem tsItem = new ToolStripMenuItem();
                    tsItem.Text = szMenuItem;
                    tsItem.Name = item.DealId;
                    tsItem.Click += tsItem_Click;
                    tsItem.Font = new Font("Courier New", 8.0F, FontStyle.Italic);
                    menuStrip.Items.Add(tsItem);
                }

                Point point = base.Location;
                point.Offset(2, base.Height + 2);
                point = base.GetPositionFromCharIndex(base.SelectionStart);
                point.Offset(2, base.Font.Height + 2);

                base.ContextMenuStrip = menuStrip;
                base.ContextMenuStrip.Show(base.PointToScreen(point));
                base.Focus();

                menuStrip.AutoClose = true;
            }
            else if (base.Text.Length >= 3 && selectedToolStripMenuInfo != null)
            {
                EntryComplete(sender, e);
            }
        }
        catch (Exception ex)
        {
            ErrorDescription = ex.Message;
            menuStrip.AutoClose = true;
            menuStrip.Visible = false;
            EntryError(sender, e);
        }
    }