Sunday, February 21, 2010

AxAcroPDF Acrobat Reader Stealing Focus

I have been having a problem where an embedded ActiveX reader control will steal the focus from the form it's on. This is very annoying as I've got a list of PDFs and I want the user to be able to use the up and down arrow keys to preview them.

I did a lot of searching and didn't find a solution. Basically after you call the LoadFile method on the control it loads the PDF asynchronously and once the PDF is loaded it will steal the focus. What you need to do is to spawn a new thread, wait half a second to give the ActiveX control a bit of time to load the file, then call the focus method on the appropriate control again.

private void LoadPDF(string path) {
axPDF.LoadFile(path);

Thread t = new Thread(new ThreadStart(Refocus));
t.Start();
jobsList.Focus();
}

private delegate void RefocusDelegate();

private void RefocusMethod()
{
jobsList.Focus();
}

private void Refocus()
{
Thread.Sleep(500);
this.BeginInvoke(new RefocusDelegate(RefocusMethod));
}

1 comment:

Steve said...

I had this same problem and searched and searched. This is the only article I could find on this problem. Thank you for finding a solution to this very annoying problem.