Display multiple PDF's in windows forms tab control

dustock
dustock used Ask the Experts™
on
I am working on a program that will eventually dynamically create tabs to display pdf files, word documents and text files.  To start I am just using an OpenFileDialog to point to a pdf to create a tab and open the file.  This is working for 1 pdf, with one small issue, I have to click on the PDF control in order for the pdf to display, otherwise its just a grey screen.  My bigger problem is when I try to add a second file to the mix.  When I click the button again to add another file the code executes until it reaches "PDFFile.LoadFile(strFileName);".  At that point I get an error, Exception of type 'System.Windows.Forms.AxHost+InvalidActiveXStateException' was thrown.  I've tried to find a solution but the one I keep seeing is I need to put in CreateControl(), which I am already doing.  Has anyone tried to do anything like this and had success?

            try
            {
                TabPage page = new TabPage();

                tabMain.Controls.Add(page);
                page.Text = strTabName;

                switch (strControlType)
                {
                    case "pdf":
                        AxAcroPDF PDFFile = new AxAcroPDF();                        
                        page.Controls.Add(PDFFile);
                        PDFFile.CreateControl();
                        PDFFile.Dock = DockStyle.Fill;
                        PDFFile.LoadFile(strFileName);
                        break;
                    case "doc":
                    case "docx":
                        break;
                    case "txt":
                        break;
                    default:
                        break;
                }                
            }
            catch(Exception ex)
            {
                var message = ex.Message.ToString();
                MessageBox.Show(message);
            }

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Software Architect, Technical Expert
Commented:
you could try to use property src of the control as a workaround :
PDFFile.src = "c:\mypdf.pdf"
in an other way, why don't you consider using the web browser component AxSHDocVW.AxWebBrowser for .net which embed IE ? this one can open and display any type of document ...

Author

Commented:
I got the same error when trying to use the src property.

I will look into the AxSHDocVW.AxWebBrowser to see if that meets my needs.

Thank you!
Franck GaspozSoftware Architect, Technical Expert

Commented:
you're welcome. Hope this will help you.
you will see that this component is really surprising because it works simply as a browser, and also if wished it let you access to the internal document model of ie ( objects, events, ... ). It is now replacing the old ActiveX object provided by Microsoft (1 version of the object for each version of ie), this new one can make running several ie motors.

Author

Commented:
Thanks for the help, I ended up using the System.Forms.WebBrowser component and it meets my needs!
Franck GaspozSoftware Architect, Technical Expert

Commented:
You're welcome. Happy to have helped.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial