Savraj

Last Question for a while!

I have a Custom Task Pane that contains Windows Forms Controls. I noticed that, when the Task Pane is not docked, it takes two distinct clicks on a button to get it to fire--the first click puts focus in the Task Pane, and the Second Click actually fires the button.

I want the first click to fire the button. Has anyone else seen this issue

Thanks again in advance!

-Savraj


Re: Visual Studio Tools for Office Focus and Custom Task Panes?

Ji Zhou – MSFT

But, I test both in Add in created in VSTO SE and VSTO 2008, no luck to reproduce your issue. The button click event will be fired with only one click, despite of docked or undocked status.

My machine environment: Windows XP, Office 2007, VSTO SE(VSTO 2008).

Thanks

Ji






Re: Visual Studio Tools for Office Focus and Custom Task Panes?

Savraj

Hi Ji! Thanks for your response.

I've posted a tiny test project here that reproduces the issue. The difference may be that I am using a toolstripbutton inside a toolstrip.

Here is a link to the test project. I made it using Visual Studio 2005 Pro, VSTO 2005 SE, and Windows Vista.

http://lafftr.com/focus-issue.zip

Please try it out and see if you can repro the issue and let me know if you can think of any solutions. Thanks for your attention and care.

- Savraj






Re: Visual Studio Tools for Office Focus and Custom Task Panes?

Savraj

I've posted the same project to Windows Live SkyDrive just to check it out:

http://cid-06d3e7a07eda2e84.skydrive.live.com/self.aspx/Public/focus-issue.zip

-savraj




Re: Visual Studio Tools for Office Focus and Custom Task Panes?

Savraj

I still haven't figured this one out--has anyone been able to open my project and at least reproduce the issue

Thanks again and have a good weekend!

-Savraj




Re: Visual Studio Tools for Office Focus and Custom Task Panes?

Ji Zhou – MSFT

Hi Savraj,

Sorry for following up late. I can reproduce the issue with your project.

I have spent some time on it, but only find the following workaround. I use the Timer and Windows API to set floating Task Pane Focused per 0.5 second. No codes were modified in UserControl1.cs. And I post my codes in ThisAddIn.cs as follows:

Code Block

using System;

using System.Windows.Forms;

using Microsoft.VisualStudio.Tools.Applications.Runtime;

using Excel = Microsoft.Office.Interop.Excel;

using Office = Microsoft.Office.Core;

using System.Runtime.InteropServices;

namespace FocusTest

{

public partial class ThisAddIn

{

[DllImport("user32.dll")]

private static extern int FindWindow(string className, string windowName);

[DllImport("user32.dll")]

private static extern int SetFocus(int hwnd);

private Microsoft.Office.Tools.CustomTaskPane test = null;

private string windowName = "focus test";

private string className = "MsoCommandBar";

int hwnd;

Timer t = new Timer();

private void ThisAddIn_Startup(object sender, System.EventArgs e)

{

#region VSTO generated code

this.Application = (Excel.Application)Microsoft.Office.Tools.Excel.ExcelLocale1033Proxy.Wrap(typeof(Excel.Application), this.Application);

#endregion

test = this.CustomTaskPanes.Add(new UserControl1(), windowName);

test.Visible = true;

test.DockPositionChanged += new EventHandler(test_DockPositionChanged);

}

void test_DockPositionChanged(object sender, EventArgs e)

{

if (test.DockPosition == Microsoft.Office.Core.MsoCTPDockPosition.msoCTPDockPositionFloating)

{

hwnd = FindWindow(className, windowName);

if (hwnd != 0)

{

t.Enabled = true;

t.Interval = 500;

t.Tick += new EventHandler(t_Tick);

}

}

else

{

t.Enabled = false;

}

}

void t_Tick(object sender, EventArgs e)

{

SetFocus(hwnd);

}

private void ThisAddIn_Shutdown(object sender, System.EventArgs e)

{

this.CustomTaskPanes.Remove(test);

}

#region VSTO generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InternalStartup()

{

this.Startup += new System.EventHandler(ThisAddIn_Startup);

this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);

}

#endregion

}

}

This workaround is not perfect, because in my test, if the user clicks the button frequently, above 2 times per second, one click event may be lost, for the Timer ticks every 0.5 second. Setting the t.Interval too low will affect other common buttons on the custom task pane. In my test, 500-900 is appropriate.

Hope it may help more or less.

Thanks

Ji






Re: Visual Studio Tools for Office Focus and Custom Task Panes?

Savraj

Hi Ji,

I just tried out your code. Yes it does work--but as you know this is nothing short of a huge hack. Wink It still presents problems, like the ones you mention and the obvious and distracting focus change that flashes the whole window border on windows vista. Please log a bug against the Office team if you haven't already, I would hope the fix for this would make it in to Office 07 SP1, but that's just a hope.

But thank you for taking the time to look in to this and providing sample code--perhaps there is some other way through the windows APIs.

If anyone else has ideas, please advise. Smile

Thanks,

Savraj