gubbfett
gubbfett

Reputation: 2177

AxAcroPdf when loaded, do something

I have a winform where i load a PDF to a AxAcroPDF.

looks something like this

Public sub LoadSelectedPDF()
    PDF_Reader.Loadfile(TXT_BrowsePDF.Text) 'PDF_Reader is my AxAcroPDF
    TXT_Title.Focus()
End Sub

Now, when i run this, I can see that it focus on the other textbox, but it loses the focus when the PDF is loaded (and the litte toolbar for zoom the PDF and all that is fading in). It's like it just starts to load, continues to next row, and when it's actually loaded it takes the focus. How can I tell it to wait for complete load and then focus on the other textbox?

Upvotes: 2

Views: 3457

Answers (2)

Victor Araneda
Victor Araneda

Reputation: 51

put AxAcroPDF in a panel, then:

Public sub LoadSelectedPDF()
    PDF_Reader.Loadfile(TXT_BrowsePDF.Text) 'PDF_Reader is my AxAcroPDF
    panel_pdf.Enabled = False
    TXT_Title.Focus()    
End Sub

in TXT_Title enter event:

System.Threading.Thread.Sleep(500)
panel_pdf.Enabled = True

Upvotes: 1

bvs
bvs

Reputation: 109

I created an extension method to prevent AxAcroPDF from stealing code, it should be used like this:

PDF_Reader.SuspendStealFocus()
PDF_Reader.Loadfile(TXT_BrowsePDF.Text)

The original C# source file can be found here. I used .NET Reflector to convert it to VB.NET (only tested in Winforms, it will store data in PDF_Reader.Tag):

<Extension> _
Friend Class AxAcroPDFFocusExtensions


   <Extension> _
   Public Shared Sub SuspendStealFocus(ByVal pdfControl As AxAcroPDF)
      pdfControl.SuspendStealFocus(250)
   End Sub

   <Extension> _
   Public Shared Sub SuspendStealFocus(ByVal pdfControl As AxAcroPDF, ByVal timeoutInMilliSeconds As Integer)
      pdfControl.Enabled = False;

      Dim t As New Timer
      t.Interval = timeoutInMilliSeconds
      AddHandler t.Tick, New EventHandler(AddressOf AxAcroPDFFocusExtensions.t_Tick)
      t.Start
      pdfControl.Tag = Guid.NewGuid
      t.Tag = New TimerTag(pdfControl, pdfControl.Tag)
   End Sub

   <Extension> _
   Public Shared Sub SuspendStealFocus(ByVal pdfControl As AxAcroPDF, ByVal timeSpan As TimeSpan)
      pdfControl.SuspendStealFocus(CInt(timeSpan.TotalMilliseconds))
   End Sub

   Private Shared Sub t_Tick(ByVal sender As Object, ByVal e As EventArgs)
      Dim timer As Timer = DirectCast(sender, Timer)
      timer.Stop
      timer.Dispose
      Dim t As TimerTag = DirectCast(timer.Tag, TimerTag)
      If Object.ReferenceEquals(t.Control.Tag, t.ControlTag) Then
            t.Control.Enabled = True
      End If
   End Sub



   <StructLayout(LayoutKind.Sequential)> _
   Private Structure TimerTag
      Public ControlTag As Object
      Public Control As AxAcroPDF
      Public Sub New(ByVal control As AxAcroPDF, ByVal controlTag As Object)
            Me.Control = control
            Me.ControlTag = controlTag
      End Sub
   End Structure
End Class

Upvotes: 1

Related Questions