c# - Why doesnt assigning a new value to a controls location property change its location at runtime? -
whenever need move control's location on form @ run-time, have assign new values top , left properties!
why doesn't location property trick?
example should able :
private void btn_mousemove(object sender, mouseeventargs e) { if (e.button == mousebuttons.left) { ((button)sender).location = e.location; } }
but doesn't work , instead have way:
private void btn_mousemove(object sender, mouseeventargs e) { if (e.button == mousebuttons.left) { ((button)sender).left = e.x + ((button)sender).left; ((button)sender).top = e.y + ((button)sender).top; } }
those 2 code snippets not equivalent.
the mouseeventargs
reports coordinates relative to control you've attached mousemove
event to, in case button.
in first example, e.location
0,0
when mouse located @ top-left corner of button. button's location set 0,0
, since its location relative form it's on, button jumps top-left corner of form.
in second example, you're setting location correctly adding e.x
, e.y
existing left
, top
properties of button, respectively.
to "fix" first example, you'd have modify take account current position of button:
if (e.button == mousebuttons.left) { var b = ((button) sender); b.location = new point(b.left + e.x, b.top + e.y); }
Comments
Post a Comment