PDA

View Full Version : سوال: حرکت دادن کنترل در زمان اجرا



reza_comp
یک شنبه 01 شهریور 1388, 11:14 صبح
من باذ کد زیر یه textbox در زمان اجرای برنامه به فرم اضافه میکنم

private void button1_Click(object sender, System.EventArgs e)
TextBox myText = new TextBox();
myText.Location = new Point(25,25);
this.Controls.Add (myText);

ولی همونطور که میبینید این textbox در یه مکان خاص قرار میگیره و نمیشه حرکتش داد.
چطوری میتونم کاری بکنم که کاربر بتونه این textbox رو در زمان اجرا حرکت بده و در مکان مورنظرش قرار بده؟؟؟

sysman_20
یک شنبه 01 شهریور 1388, 13:14 عصر
این دو متغیر رو تو کلاس فرمت تعریف کن


privatebool dragging;
privatePoint pointClicked;

توی MouseMove شیئ ات اینو بذار


if (dragging == true)
{
Point pointMoveTo;
// Find the current mouse position in screen coordinates.
pointMoveTo = button1.PointToScreen(newPoint(e.X, e.Y));
// Compensate for the position the control was clicked.
pointMoveTo.Offset(-pointClicked.X, -pointClicked.Y);
// Move the form.
//this.Location = pointMoveTo;
button1.Location = pointMoveTo;
}

به جای button1 شیئ خودتو بذار
توی MouseDown اینو بذار:


if (e.Button == MouseButtons.Left)
{
dragging = true;
pointClicked = newPoint(e.X, e.Y);
}
else
{
dragging = false;
}

و توی MouseUp هم اینو بذار:


dragging = false;

کمی تغییرات روش بدی بهتر هم میشه
امیدوارم به دردت بخوره

reza_comp
پنج شنبه 05 شهریور 1388, 17:26 عصر
برای کنترلی که در زمان طراحی به فرم اضافه شده این درست کار میکنه...برای حرکت دادن کنترلی که در زمان اجرا اضافه مشیه باید چی کار کنم؟؟؟