Goomba79
Goomba79

Reputation: 11

add external process to panel vb.net

I have a list of files displayed in a listbox. when I select a file from the listbox i want the file to load into a panel on my form. i.e. if its a word document word will open in the panel, if its a pdf reader wil open into the panel.

I can get the files to load externally using

Dim ProcStart As New ProcessStartInfo
ProcStart.FileName = ListBox1.SelectedItem
Process.Start(ProcStart)

however i am unsure of how to get it to then dock in my panel. I tried

Me.Panel1.Controls.Add(ProcStart)

but this is obviously wrong as I can't add a process as a control.

I did a bit of googleing and have tried to do it this way

<DllImport("user32.dll")>
Shared Function SetParent(ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As UInteger
End Function
Private Sub ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

    Dim proc As Process
    Dim AppPath As String

    AppPath = lstDocs & ListBox1.SelectedItem
    proc = Process.Start(AppPath)
    proc.WaitForInputIdle()

    SetParent(proc.MainWindowHandle, Me.Panel1.Handle)


End Sub

but the word application still opens outside my program and not in the panel!!

Any ideas? and thanks for looking!

Upvotes: 1

Views: 1082

Answers (1)

Pakk
Pakk

Reputation: 1339

Have you tried adding a button with code behind it to start the process?

'This is how i would start the process

this would be in your code that starts the control ( inserting )

Dim dep1 As (INSERT YOUR EVENT HERE)= New (INSERT YOUR EVENT HERE)
AddHandler dep.OnChange, AddressOf dep_onchange

the actuall button

Private Sub dep_onchange1(ByVal sender As System.Object, ByVal e As System.EventArgs)
    ' this event is run asynchronously so you will need to invoke to run on the UI thread(if required)
    If Me.InvokeRequired Then
        lbnoes.BeginInvoke(New MethodInvoker(AddressOf GetNoes))
    Else
        GetNoes()
    End If
 End Sub

Upvotes: 1

Related Questions