{"id":4413,"date":"2014-03-30T10:45:05","date_gmt":"2014-03-30T10:45:05","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/destroying-a-control-fails-collection-of-common-programming-errors\/"},"modified":"2014-03-30T10:45:05","modified_gmt":"2014-03-30T10:45:05","slug":"destroying-a-control-fails-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/destroying-a-control-fails-collection-of-common-programming-errors\/","title":{"rendered":"Destroying a control fails.-Collection of common programming errors"},"content":{"rendered":"<ul>\n<li><img decoding=\"async\" src=\"http:\/\/i.msdn.microsoft.com\/dn186180.LOGO_Win1211(id-id,MSDN.10).png\" \/><br \/>\nmsdn<\/p>\n<p>I&#8217;m having difficulties while destroying a usercontrol. I created my own control, and am using it in an application. The variable is always the same. I create and destroy the object multiple times, but the control fails to disappear,\u00a0and instead reemerges at it&#8217;s original\u00a0coordinates the first time it was created. It&#8217;s resizable and movable at runtime. When I click outside of it it&#8217;s supposed to be destroyed an nullified. It does disappear but reemerges as I said\u00a0at it&#8217;s original coordinates. I&#8217;m using usercontrol.Destroy() for that, but first I set the usercontrol.visible to false. Am I missing something? Shouldn&#8217;t an object just disapper when it&#8217;s destroyed, in this case a control, or is this more complicated than I thought?<\/p>\n<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/i.msdn.microsoft.com\/dn186180.LOGO_Win1211(id-id,MSDN.10).png\" \/><br \/>\nmsdn1 Hi,<\/p>\n<p>Do you remove the control reference from the parent Controls collection?<\/p>\n<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/i.msdn.microsoft.com\/dn186180.LOGO_Win1211(id-id,MSDN.10).png\" \/><br \/>\nmsdn2 Your question is very unclear.\u00a0 UserControl doesn&#8217;t have a Destroy() method.\u00a0\u00a0 To remove a control from its container you have to Remove() it from its container&#8217;s Controls collection, then Dispose() it so the window handle is destroyed.\u00a0 For example: \u00a0 this.Controls.Remove(userControl1); \u00a0 userControl1.Dispose();<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/i.msdn.microsoft.com\/dn186180.LOGO_Win1211(id-id,MSDN.10).png\" \/><br \/>\nmsdn3 I think i tried that like this:<\/p>\n<p>panel1.Controls.Remove(cropTracker);<\/p>\n<p>and this is how I initialized it, by giving it two parameters. It&#8217;s like a CRectTracker in MFC. There are two parameters: the control it&#8217;s supposed to resize and the reference to the form which i need to access objects on the main form from the tracker.<\/p>\n<p>RectTracker\u00a0cropTracker =<\/p>\n<p>panel1.Controls.Add(cropTracker);<\/p>\n<p>new RectTracker(cropTracker, this);<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/i.msdn.microsoft.com\/dn186180.LOGO_Win1211(id-id,MSDN.10).png\" \/><br \/>\nmsdn4 Your question is very unclear.\u00a0 UserControl doesn&#8217;t have a Destroy() method.\u00a0\u00a0 To remove a control from its container you have to Remove() it from its container&#8217;s Controls collection, then Dispose() it so the window handle is destroyed.\u00a0 For example: \u00a0 this.Controls.Remove(userControl1); \u00a0 userControl1.Dispose();<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/i.msdn.microsoft.com\/dn186180.LOGO_Win1211(id-id,MSDN.10).png\" \/><br \/>\nmsdn5<\/p>\n<p>It works now but I&#8217;m having some other problems which are too complex to describe. I&#8217;ll try to fix that.\u00a0Sorry for desturbing you with such trivial questions. Thanks everyone.<\/p>\n<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/i.msdn.microsoft.com\/dn186180.LOGO_Win1211(id-id,MSDN.10).png\" \/><br \/>\nmsdn6<\/p>\n<p>Thank you Hans. I wanted to write Dispose but somehow I wrote Destroy.<\/p>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>msdn I&#8217;m having difficulties while destroying a usercontrol. I created my own control, and am using it in an application. The variable is always the same. I create and destroy the object multiple times, but the control fails to disappear,\u00a0and instead reemerges at it&#8217;s original\u00a0coordinates the first time it was created. It&#8217;s resizable and movable [&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-4413","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4413","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=4413"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4413\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4413"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4413"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4413"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}