{"id":3315,"date":"2014-03-22T02:35:43","date_gmt":"2014-03-22T02:35:43","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/22\/c-how-to-move-controls-on-the-page-at-runtimne-using-mouse-collection-of-common-programming-errors\/"},"modified":"2014-03-22T02:35:43","modified_gmt":"2014-03-22T02:35:43","slug":"c-how-to-move-controls-on-the-page-at-runtimne-using-mouse-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/22\/c-how-to-move-controls-on-the-page-at-runtimne-using-mouse-collection-of-common-programming-errors\/","title":{"rendered":"C# &#8211; How to move controls on the page at runtimne using mouse-Collection of common programming errors"},"content":{"rendered":"<p>Hi! :),<\/p>\n<p>I&#8217;m building a metro style app and I&#8217;d like the user could move textblocks on the page of the app at runtime clicking on them and moving the mouse on the page.<\/p>\n<p>For this purpose I developed the code below.<\/p>\n<p>Actually, It does the trick but there&#8217;s a problem. The selected\u00a0textblock moves more than the mouse\u00a0pointer. For example if I move the mouse on the right along very\u00a0few pixels, the selected textblock moves on the right along lots of pixels. So the textblock movements amplify the mouse movements.\u00a0I&#8217;d like the textblock exactly follows the movements of the mouse pointer after clicking on it.<\/p>\n<p>I hope you can find the problem in my code.<\/p>\n<pre>private bool isTracing = false;\n        private Point previousLocation;\n\n\n        void MouseDown(object sender, PointerRoutedEventArgs e)\n        {\n            ((TextBlock)sender).CapturePointer(e.Pointer);\n\n            Debug.WriteLine(\"PointerPressed\");\n\n            previousLocation = e.GetCurrentPoint(this).Position; \/\/ Pointer position\n            isTracing = true;\n            Window.Current.CoreWindow.PointerCursor = new Windows.UI.Core.CoreCursor(Windows.UI.Core.CoreCursorType.Hand, 1);\n        }\n\n        \n        void MouseMove(object sender, PointerRoutedEventArgs e)\n        {\n            Debug.WriteLine(\"PointerMoved\");\n            Debug.WriteLine(e.GetCurrentPoint(this).Position);\n            if (isTracing)\n            {\n                Debug.WriteLine(\"isTracing\");\n                var activeControl = (TextBlock)sender; \/\/ Selected textblock\n                Thickness margin = activeControl.Margin; \/\/ Margins of the selected textblock\n                Thickness newMargin = new Thickness();\n                Point currentLocation =  e.GetCurrentPoint(this).Position; \/\/ Pointer position\n                newMargin.Left = margin.Left + (currentLocation.X - previousLocation.X);\n                newMargin.Top = margin.Top + (currentLocation.Y - previousLocation.Y);\n                newMargin.Right = margin.Right - (currentLocation.X - previousLocation.X);\n                newMargin.Bottom = margin.Bottom - (currentLocation.Y - previousLocation.Y);\n                activeControl.Margin = newMargin;\n            }\n\n        }\n\n       \n        void MouseUp(object sender, PointerRoutedEventArgs e)\n        {\n            Debug.WriteLine(\"PointerReleased\");\n            ((TextBlock)sender).ReleasePointerCapture(e.Pointer);\n            isTracing = false;\n            Window.Current.CoreWindow.PointerCursor = new Windows.UI.Core.CoreCursor(Windows.UI.Core.CoreCursorType.Arrow, 1);\n        }<\/pre>\n<p>Thank you in advance.<\/p>\n<p>Gabriele<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi! :), I&#8217;m building a metro style app and I&#8217;d like the user could move textblocks on the page of the app at runtime clicking on them and moving the mouse on the page. For this purpose I developed the code below. Actually, It does the trick but there&#8217;s a problem. The selected\u00a0textblock moves more [&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-3315","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3315","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=3315"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3315\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3315"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3315"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}