I wanna move a control on a Form at runtime.
Any Advice
I wanna move a control on a Form at runtime.
Any Advice
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Mike GoldRank #50 · 30.6K reputationPosted Apr 5, 2008, 7:18 PM EDT
I believe all you need to do is set the Location property of the control
myControl.Location = new Point(x, y);
Just make sure you do it after the OnLoad event has occurred in the form
Ahmad Mobadder3 reputationPosted Apr 12, 2008, 7:45 AM EDT
thanks alot i'm using this on the control
private void State_MouseMove(object sender, MouseEventArgs e){
if (e.Button == MouseButtons.Left){
this.Location = new Point(e.X, e.Y);}
}
but it's not accurate, the control is fricling and not where i release the mouse it stay any more ideas thanks again..
Yoav34 reputationPosted Apr 22, 2008, 12:16 AM EDT
private bool inDrag = false;
private int xLoc;
private int yLoc;
private void State_MouseDown(object sender, MouseEventArgs e)
{
}
private void State_MouseMove(object sender, MouseEventArgs e)
{
if (inDrag)
{
this.Top += e.Y - yLoc;
this.Left += e.X - xLoc;
}
}
private void State_MouseUp(object sender, MouseEventArgs e)
{
inDrag = false;
}