问题描述
你好,
这个问题与 Jeffrey-Chen 示例
in http://social.msdn.microsoft.com/Forums/vstudio/en-US/21dd85bb-4a1b-4457-a1f0-b671af393395/excelvstoaddin-customtaskpane-keeps-input-focus-when-typing-in-spreasheet?forum= vsto :
this question is related to the Jeffrey-Chen sample in http://social.msdn.microsoft.com/Forums/vstudio/en-US/21dd85bb-4a1b-4457-a1f0-b671af393395/excelvstoaddin-customtaskpane-keeps-input-focus-when-typing-in-spreasheet?forum=vsto:
我们有一个简单的任务窗格,上面有两个按钮。单击按钮可激活相应的工作表:
We have a simple task pane with two buttons on it. Clicking on a button activates the corresponding worksheet:
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
var taskPane = Globals.ThisAddIn.CustomTaskPanes.Add(new MyCustomTaskPaneControl(), "MyCustomTaskPaneControl", window);
//var taskPane = Globals.ThisAddIn.CustomTaskPanes.Add(new UserControl(), "MyTest", window);
taskPane.Visible = true;
}
public partial class MyCustomTaskPaneControl:UserControl
{
private Button button1;
私人按钮2;
public MyCustomTaskPaneControl()
{
button1 = new Button();
button1.Text =" button1" ;;
button1.Click + = new EventHandler(button1_Click);
button1.TabStop = false;
button2 = new Button();
button2.Top = 30;
button2.Text =" button2" ;;
button2.Click + = new EventHandler(button2_Click);
button1.TabStop = false;
this.Controls.Add(button1);
this.Controls.Add(button2);
}
private void button1_Click(object sender,EventArgs e)
{
var wk = Globals.ThisAddIn.Application.ActiveWorkbook;
var sheet = wk.Sheets [1]; //作为工作表;
sheet.Activate();
}
private void button2_Click(object sender,EventArgs e)
{
var wk = Globals.ThisAddIn.Application.ActiveWorkbook;
var sheet = wk.Sheets [2]; //作为工作表;
sheet.Activate();
}
}
}
} public partial class MyCustomTaskPaneControl : UserControl { private Button button1; private Button button2; public MyCustomTaskPaneControl() { button1 = new Button(); button1.Text = "button1"; button1.Click += new EventHandler(button1_Click); button1.TabStop = false; button2 = new Button(); button2.Top = 30; button2.Text = "button2"; button2.Click += new EventHandler(button2_Click); button1.TabStop = false; this.Controls.Add(button1); this.Controls.Add(button2); } private void button1_Click(object sender, EventArgs e) { var wk = Globals.ThisAddIn.Application.ActiveWorkbook; var sheet = wk.Sheets[1];// as Worksheet; sheet.Activate(); } private void button2_Click(object sender, EventArgs e) { var wk = Globals.ThisAddIn.Application.ActiveWorkbook; var sheet = wk.Sheets[2];// as Worksheet; sheet.Activate(); } }}
问题:
Problem:
键盘焦点保持在点击的按钮上(在"sheet.Activate();"之后) )!!!
如何让工作表具有键盘焦点???
How can I let the worksheet have the keyboard focus???
Jörg
PS:唯一的"解决方案"我发现是。
P.S.: The only "solution" I've found is here.
推荐答案
尝试使用来自Windows API的功能。 Excel对象模型不提供用于以编程方式设置焦点的任何属性或方法。
Try to use the SetFocus function which comes from Windows API. The Excel object model doesn't provide any property or method for setting focus programmatically.
这篇关于Excel加载项:自定义任务窗格焦点未返回到活动工作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!