Unable to cast object of type 'System.Windows.Forms.RichTextBox' to type 'System.Windows.Forms.PictureBox RRS feed

  • Question

  • Hello to all Concerned

    How do solve the error type Unable to cast object of type 'System.Windows.Forms.RichTextBox' to type 'System.Windows.Forms.PictureBox' in Visual Studio Desktop 2012 platform in C#.

    I am attempting to save an existing Jpeg image using picturebox component in an Active Mdichild form

    from the Parent form using a menustrip component,

    The code in C# is as follows:

    private void saveFileToolStripMenuItem_Click(object sender, EventArgs e)
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.Filter = "Images|*.png;*.bmp;*.jpg;*.tiff";
                System.Drawing.Imaging.ImageFormat format = System.Drawing.Imaging.ImageFormat.Jpeg;
                if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    string ext = System.IO.Path.GetExtension(sfd.FileName);
                    switch (ext)
                        case ".jpg":
                            format = System.Drawing.Imaging.ImageFormat.Jpeg;
                        case ".bmp":
                            format = System.Drawing.Imaging.ImageFormat.Bmp;
                    Form activeChildForm = this.ActiveMdiChild;

                    if (activeChildForm != null)
                        PictureBox pictureBox1 = (PictureBox)activeChildForm.ActiveControl;
                        if (pictureBox1 != null)
                            pictureBox1.Image.Save(sfd.FileName, format); 
                    //Error Response from compiler:  Additional information: Unable to cast object of type

                       'System.Windows.Forms.RichTextBox' to type  'System.Windows.Forms.PictureBox'.



    • Moved by Mike Kinsman Thursday, June 6, 2013 10:56 PM off topic
    Thursday, June 6, 2013 3:16 PM

All replies

  • Unfortunately your post is off topic here, in the MSDN Subscriptions feedback forum, because it is not feedback regarding the MSDN Subscription. This is a standard response I’ve written up in advance to help many people (thousands, really.) who happen to post their question in my forum, but please don’t ignore it.  The links provided below I’ve collected to help with many issues we’ve seen.

    For technical issues with Microsoft products that you would run into as an end user of those products, one great source of info and help is http://answers.microsoft.com, which has sections for Windows, Hotmail, Office, IE, and other products.

    For Technical issues with Microsoft products that you might have as an IT professional (like more technical installation issues, or other IT issues), you should head to the TechNet Discussion forums at http://social.technet.microsoft.com/forums/en-us, and search for your product name.

    For issues with products you might have as a Developer (like how to talk to APIs, what version of software do what, or other developer issues), you should head to the MSDN discussion forums at http://social.msdn.microsoft.com/forums/en-us, and search for your product or issue.

    If you’re asking a question particularly about one of the Microsoft Dynamics products, a great place to start is here: http://community.dynamics.com/

    If you really think your issue is related to the MSDN Subscription, and I screwed up, I apologize!  Please repost your question to the discussion forum and include much more detail about your problem, that could include screenshots of the issue (do not include subscription information or product keys in your screenshots!), and/or links to the problem you’re seeing. 

    If you really have no idea where to post this question, then you shouldn’t have posted here, because we have a forum just for you!  It’s called the ‘Where is the forum for…?’ forum and its here: http://social.msdn.microsoft.com/forums/en-us/whatforum/

    Please review the topic of the forum you’re posting in before posting your question.  Moving your post to the off topic forum.

    Thanks, Mike

    MSDN and TechNet Subscriptions Support
    Read the Subscriptions Blog!

    Thursday, June 6, 2013 10:56 PM