Hi, i'm developing a solution with VSTO2 for Excel.
The actionpane show a my control with a TabControl (with 2 tab pages).
When in a worksheet i go on a NamedRange or in another, i active a page of the TabControl by code, but the focus remain in the actionpane and i lost focus on excel without possibility for the user to move around cells of excel with arrowkeys.
Here the istruction I use to select the tab page on TabControl of actionpane:
Globals.oWork.Pane1.TabControl1.SelectedTab = Globals.oWork.Pane1.TabControl1
How can i force focus on Excel

Force Focus on Excel? How to?
Drasko
I found a couple of programmatic ways of making sure that the focus does not go away from the worksheet in the first place.
Simple but inefficient way:
====================
Before changing your the active TabPage in your TabControl in the ActionsPane you can make the ActionsPane invisible. Controls that are not visible can not take focus, so, your worksheet will never lose focus.
Globals.ThisWorkbook.MyActionsPaneCtrl.Visible = false;// <---- Change the Tab Page in the tab control ---->
Globals.ThisWorkbook.MyActionsPaneCtrl.Visible = true;
MyActionsPaneCtrl is the my UserControl that contains all other controls.
Cumbersome way (making controls in actions pane non-selectable):
====================================================
System.Windows.Forms.Control class contains a method called SetStyle. All winform controls derive from System.Windows.Forms.Control, hence, they inherit this method which can be used to make a control "not focusable".
// The control can take focus if second parameter is true and vice versaSetStyle(System.Windows.Forms.ControlStyles.Selectable, true);
The catch is that SetStyle method is protected. So, you have to derive your own controls in order to access it. Also, the result of calling this method is not always intutive e.g. if you make a TabPage control non-selectable the controls within the TabPage are unaffected.
Our aim is to prevent controls in the Actions Pane from getting focus. But we can not make entire TabPage(s) non-selectable because we have to activate / de-activate TabPages as user selection changes.
1. You derive a MyTextBox class from System.Windows.Forms.TextBox as follows:
class MyTextBox : TextBox{
public bool CanTakeFocus
{
get { return this.CanFocus; }
set
{
this.SetStyle(ControlStyles.Selectable, value); // <--- making control non-selectable
}
}
}
2. Instead of adding Winform controls to your TabPages, you add your derived controls e.g. Add one MyTextBox control to each one of the two TabPage. The designer code (may need to modify by hand) will look:
// MyActionPane will be added to ActionsPane
class MyActionPane: UserControl
{
private TabControl tabControl1;
private TabPage tabPage1;
private TabPage tabPage2;
private MyTextBox textBox1;
private MyTextBox textBox2;
...
3. Add a property to your User Control (e.g. MyActionPane class here) that makes all MyTextBoxes non-selectable:
class MyActionPane: UserControl
{
public bool AreControlsSelectable
{
get { return textBox1.CanFocus; }
set {
textBox1.CanTakeFocus = value;
textBox2.CanTakeFocus = value;
}
}
}
3. In your Sheet1 class (where you try to change tab page):
apCtrl.AreControlsSelectable = false; // apCtrl is instance of MyActionPane
// <----- Change the Tab Page in the tab control ---->
apCtrl.AreControlsSelectable = true;
This will make sure that when the active TabPage in the TabControl changes, all controls within the all the TabPages are non-selectable. This causes the focus to remain in the Worksheet.
I know it's not pretty :-) Just something I came up with. Hopefully, someone may be able to suggest a much simpiler mechanism using the SetStyle method.
Wytze
Kazuya Ujihara
Using SendKeys is not an optimal solution, but no one, even in VSTO team, seems to know of another way to give focus back to the document window.
I chanced upon another solution that seems to work, even if the document is open within IE (I tried IFRAME).
Call following method in the ActionsPane code to give focus back to the document window. There is a corresponding method in the Excel OM too.
Globals.ThisDocument.Application.ActiveWindow.SetFocus();I can not gaurantee that this works in all situations. So, please let us know if you see scenarios where this call doesn't work. Thanks.
Richard Arnoldx
You can use something like this:
Application.SendKeys("{F6}", this.missing);
See more details in this thread
http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=197188&SiteID=1