C#如何检测Excel是否失去焦点
我有一个Excel加载项(用C#和ExcelDNA编写),它的表单允许用户将数据输入从文本框继承的控件中。形式是模态的。进入控件会导致出现一个上下文菜单,其中包含基于用户输入的选项 如果用户已输入数据且contextmenu可见,然后用户使另一个应用程序成为活动应用程序,则contextmenu将覆盖该应用程序C#如何检测Excel是否失去焦点,c#,winforms,excel,excel-dna,C#,Winforms,Excel,Excel Dna,我有一个Excel加载项(用C#和ExcelDNA编写),它的表单允许用户将数据输入从文本框继承的控件中。形式是模态的。进入控件会导致出现一个上下文菜单,其中包含基于用户输入的选项 如果用户已输入数据且contextmenu可见,然后用户使另一个应用程序成为活动应用程序,则contextmenu将覆盖该应用程序 是否存在一个事件,我可以使用Excel应用程序的关闭来确定Excel已失去焦点?我找到了一种方法,使contextmenu显示,而不包括其他项目。我的问题是,在输入textbox期间,c
是否存在一个事件,我可以使用Excel应用程序的关闭来确定Excel已失去焦点?我找到了一种方法,使contextmenu显示,而不包括其他项目。我的问题是,在输入textbox期间,contextmenu始终可见,因为我在textbox TextChanged事件中将AutoClose属性设置为false。现在,我在重新填充项目列表时,在TextChanged事件中将AutoClose属性设置为false。在输入第三个字符后,对输入文本框的任何击键都会执行此操作 然后,我创建了一个contextmenu关闭事件,如下所示:
#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);
}
}