PDA

View Full Version : حرکت کنترل ها بر روی فرم؟



sepide_68_91
شنبه 31 تیر 1391, 10:11 صبح
سلام
دوستان کسی میدونه چطور میشه کنترل های مختلف رو روی فرم با درگ موس جا به جا کرد؟؟
ممنون میشم راهنماییم کنید
البته یک نمونه با vb در تالار پیدا کردم به سی شارپ تبدیلش کردم که یک عالم خطا داد و ازش سر در نیاوردم
این کدهاست که تبدیل شده

using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
public class frmMain
{
bool dragging;
int sX;

int sY;
private void startDragging(object sender, System.Windows.Forms.MouseEventArgs e)
{
dragging = true;
sX = e.X;
sY = e.Y;
}

private void WhileDragging(object sender, MouseEventArgs e)
{
if (dragging == true) {
sender.location = new Point(sender.location.x + e.X - sX, sender.location.y + e.Y - sY);
this.Refresh();
}
}

private void EndDragging(object sender, EventArgs e)
{
dragging = false;
My.Settings.ControlLocation.Clear();

foreach (Control Control in this.Controls) {
My.Settings.ControlLocation.Add(Control.Name + "!" + Control.Location.X + "!" + Control.Location.Y);

}
My.Settings.Save();

}

private void Form1_Load(System.Object sender, System.EventArgs e)
{
foreach (Control Control in this.Controls) {
Control.MouseDown += startDragging;
Control.MouseMove += WhileDragging;
Control.MouseUp += EndDragging;

}

foreach (Control Control in this.Controls) {
foreach (object i_loopVariable in My.Settings.ControlLocation) {
i = i_loopVariable;
if (Strings.Split(i, "!")[0] == Control.Name) {
Control.Location = new Point(Strings.Split(i, "!")[1], Strings.Split(i, "!")[2]);
}

}
}
}
public frmMain()
{
Load += Form1_Load;
}
}