VonGrave
VonGrave

Reputation: 21

Opening an form and clicking, but the click selects the parent form

I have a form that opens a PDF file from a selection of files on a server. The problem is that when I let my pdf previewer open the selected PDF, the window opens as it should. But it seems like the mouse cursor is still focusing the parent Form. not the new form i have opened that contains the preview of the PDF. If I click onto the new form, it selects the parent form behind. If I click again, it selects the new form. and from there everything is fine. Its a embuggerence that I just dont seem to find the source of.

It feels like the program is not focusing the new form that is opened.

I could of course have used the Me.ShowDialog(), but the problem with that is that I would not be able to open multiple PDFs, or use the program while having a PDF open.. This is meant as a backup solution to users without Acrobat Reader and such.

The reason for the dountil loop in the Execute Sub is to make the code wait for the PDF to be closed

I have trimmed the code to make it more readable, and hope its sufficient.

 ShowDoc(){
  Execute(me, fileName)

This opens

  Sub Execute(xxxxx)
        Dim prev As New PreviewAPDF
        prev.previewPdf(fileName, FromFrm)

        Do Until prev.isClosed
                Application.DoEvents()
                System.Threading.Thread.Sleep(50)
        Loop
  End Sub
  xxx Lots of stuff happens that is not related xxx

This leads to a Preview class called PreviewAPDF

Public Class PreviewAPDF
    public isClosed As Boolean = False
    Public Sub previewPDF(XXlotsofstuffXX)
        If myStream IsNotNothing Then 
            Me.pdf.Load(myStream)
        Else 
           Me.pdf.Filepath = file
        End If
        me.owner = parentForm
        Me.Show()
        Me.CenterToParent()
    End Sub
    Private Sub close_click(stuff)
        isCLosed = True
        Me.Close()
    End Sub

Is it the DoUntil loop thats causing it? if so, does anyone have any suggestions as to how to do the same thing, just in a better manner?

Upvotes: 1

Views: 43

Answers (2)

VonGrave
VonGrave

Reputation: 21

The solution for me was all together avoid the usage of the Do Until Loop. Some research about Application.DoEvents made me drop the idea of using this way to "halt" the code. I instead avoided it by letting the code run its course, and on a later point write a function to handle the code (I was originally waiting for) at a later point.

It was the Do Until, combined with ApplicationDoEvents that caused the form to lose focus, and from what I have understood, there isn't a simple fix to this issue. Other than scrapping such outdated ways of doing things and instead build properly, and not use such bad coding practices.

Upvotes: 0

cesiumdeth
cesiumdeth

Reputation: 34

'try the following

Public Sub previewPDF(XXlotsofstuffXX)
    If myStream IsNotNothing Then 
        Me.pdf.Load(myStream)
    Else 
       Me.pdf.Filepath = file
    End If
    me.owner = parentForm
    Me.Show()
    Me.CenterToParent()

    Me.BringToFront() '<--- Try this
    Me.Focus() '<--- Try this
End Sub

Upvotes: 0

Related Questions