Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5936597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:25:16+00:00 2026-05-22T15:25:16+00:00

I recently upgraded a VB 6 project to .net. I’m having a problem with

  • 0

I recently upgraded a VB 6 project to .net. I’m having a problem with this block of code:

    Dim CtrlName As System.Windows.Forms.MenuItem

    For Each CtrlName In Form1.Controls

    'Some code here

    Next CtrlName

Now this code compiles but throws the following runtime error:

Unable to cast object of type ‘System.Windows.Forms.Panel’ to type ‘System.Windows.Forms.MenuItem.

I have a panel control on the subject form. How do I resolve this?
Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T15:25:17+00:00Added an answer on May 22, 2026 at 3:25 pm

    You are iterating over all controls that are directly inside the form, not just the MenuItems. However, your variable is of type MenuItem. This is causing the problem.

    For normal controls (e.g. Buttons), you’d want to use the following, easy fix; test inside the loop whether the control type is correct:

    For Each control As Control In Form1.Controls
        Dim btt As Button = TryCast(control, Button)
        If btt IsNot Nothing Then
            ' Perform action
        End If
    Next
    

    However, this does not work for MenuItems since these aren’t controls at all in WinForms, and they aren’t stored in the form’s Controls collection.

    You need to iterate over the form’s Menu.MenuItems property instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently upgraded a c# windows service to run as a 64 bit .net
I recently upgraded a C# project from .NET 3.5 to .NET 4. I have
I just recently upgraded a VS2008/.NET 3.5 SP1 project to VS2010 and .NET 4.
I recently upgraded a Web Application Project (as well as some dependent projects) from
I recently upgraded a VS2005 web deployment project to VS2008 - and now I
I recently upgraded a 1.1 web project to a 2.0 web application. After going
I recently upgraded to Subversion 1.5, and now I cannot commit my code to
I am working in a .NET 2.0, recently upgraded to .NET 3.5 environment (VS2008,
I have a project I recently upgraded to VS2010 - the project/solution files are
We just recently upgraded from VS2008 to VS2010. Our project compiles fine; but when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.