We help IT Professionals succeed at work.
Get Started

Display multiple PDF's in windows forms tab control

dustock
dustock asked
on
498 Views
Last Modified: 2015-06-18
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
Software Architect, Technical Expert
Commented:
This problem has been solved!
Unlock 1 Answer and 5 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE