עשיתי פה קוד פשוט שאני מזיז עם העכבר כפתור לאורך ציר Y ואני רוצה שכשאני מגיע עם הכפתור למעלה או למטה לגבול של ה FORM1 שזה לא יגלוש החוצה אלא ייעצר יעמוד במקום שיהיה אפשר להזיז את הכפתור רק בתוך התחום של ה FORM1.ניסיתי ולא הבנתי איך אני בודק את זה עם IF .
using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Moving_Button { public partial class Form1 : Form { Point lastClick; Form f = new Form(); public Form1() { InitializeComponent(); f.Location = new Point(0, 0); }
private void button1_MouseDown(object sender, MouseEventArgs e) { lastClick = new Point(e.X, e.Y); }
private void button1_MouseMove(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left) //Only when mouse is clicked { //Move the Form the same difference the mouse cursor moved; // button1.Left += e.X - lastClick.X; button1.Top += e.Y - lastClick.Y; }
} } }
|