{"id":5334,"date":"2014-03-30T20:50:00","date_gmt":"2014-03-30T20:50:00","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/invalid-conversion-error-collection-of-common-programming-errors\/"},"modified":"2014-03-30T20:50:00","modified_gmt":"2014-03-30T20:50:00","slug":"invalid-conversion-error-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/invalid-conversion-error-collection-of-common-programming-errors\/","title":{"rendered":"Invalid Conversion error-Collection of common programming errors"},"content":{"rendered":"<p>You are iterating over <strong>all<\/strong> controls that are directly inside the form, not just the <code>MenuItem<\/code>s. However, your variable is of type <code>MenuItem<\/code>. This is causing the problem.<\/p>\n<p>For <em>normal<\/em> controls (e.g. <code>Button<\/code>s), you&#8217;d want to use the following, easy fix; test inside the loop whether the control type is correct:<\/p>\n<pre><code>For Each control As Control In Form1.Controls\n    Dim btt As Button = TryCast(control, Button)\n    If btt IsNot Nothing Then\n        ' Perform action\n    End If\nNext\n<\/code><\/pre>\n<p>However, this does <em>not<\/em> work for <code>MenuItem<\/code>s since these aren&#8217;t controls at all in WinForms, and they aren&#8217;t stored in the form&#8217;s <code>Controls<\/code> collection.<\/p>\n<p>You need to iterate over the form&#8217;s <code>Menu.MenuItems<\/code> property instead.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;d want to use the following, easy fix; test inside the loop whether the control type is correct: For Each control As [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5334","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5334","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5334"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5334\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}