如何用MouseMove函数移动我的对象?

时间:2015-04-11 21:59:26

标签: c# winforms

我有一个包含标签和LineShape的类:

public class jalon
{
    public Label lab = new Label();
    public LineShape line = new LineShape();
    public string date;
}

我动态创建了这个类的实例。

            int count;

            OleDbCommand cmd = new OleDbCommand();
            cnx.Open();
            cmd.Connection = cnx;

            string mois = cmbMat.SelectedValue.ToString();

            cmd.CommandText = "select count(*) from TableJalon Where Month(Date_Jalon)=" + mois;
            count = (int)(cmd.ExecuteScalar());

            OleDbDataReader reader = cmd.ExecuteReader();
            int i =count-1;
            int j = 300;

            jalon dr;
            for(int i =0; i<count;i++)
            {


                dr = new Jalon();

                dr.lab.Text = reader.GetValue(1).ToString();
                dr.lab.Location = new Point(j, j);

                dr.lab.MouseClick += new System.Windows.Forms.MouseEventHandler(this.labMouseClick);
                dr.lab.MouseDown += new System.Windows.Forms.MouseEventHandler(this.labMouseDown);
                dr.lab.MouseMove += new System.Windows.Forms.MouseEventHandler(this.LabMouseMove);
                dr.lab.DoubleClick += new System.EventHandler(this.ChangColor);
                this.Controls.Add(dr.lab);
                this.Controls.Add(dr.line);
            }

现在我想移动标签和我创建的MouseMovelab函数相同的LineShape包含:

private void LabMouseMove(object sender, MouseEventArgs e)
        {

        Label jal = (Label)sender;
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            jal.Left = e.X + jal.Left - MouseDownLocation.X;
            jal.Top = e.Y + jal.Top - MouseDownLocation.Y;
            lineShape1.X1 = jal.Left;// i don't now what i use here
            lineShape1.Y1 = jal.Top + jal.Height;
        }
    }

所以我想同时移动我的标签和LineShape

1 个答案:

答案 0 :(得分:0)

您可以在全局变量中维护类jalon的List。在鼠标移动时,识别相应的LineShape并移动它。

添加全局变量

 //global variable
 private List<jalon> jalons;

维护一系列jalons

        jalons = new List<jalon>();

        jalon dr;
        for(int i =0; i<count;i++)
        {


            dr = new Jalon();

            dr.lab.Text = reader.GetValue(1).ToString();
            dr.lab.Location = new Point(j, j);

            dr.lab.MouseClick += new System.Windows.Forms.MouseEventHandler(this.labMouseClick);
            dr.lab.MouseDown += new System.Windows.Forms.MouseEventHandler(this.labMouseDown);
            dr.lab.MouseMove += new System.Windows.Forms.MouseEventHandler(this.LabMouseMove);
            dr.lab.DoubleClick += new System.EventHandler(this.ChangColor);
            this.Controls.Add(dr.lab);
            this.Controls.Add(dr.line);

            jalons.Add(dr);
        }

最后,在鼠标移动中,从此列表中找到相应的LineShape。

private void LabMouseMove(object sender, MouseEventArgs e)
    {

    Label jal = (Label)sender;
    if (e.Button == System.Windows.Forms.MouseButtons.Left)
    {
        jal.Left = e.X + jal.Left - MouseDownLocation.X;
        jal.Top = e.Y + jal.Top - MouseDownLocation.Y;

        LineShape lineShape1 = jalons.Where(j => j.lab == jal).FirstOrDefault().line;

        lineShape1.X1 = jal.Left;// i don't now what i use here
        lineShape1.Y1 = jal.Top + jal.Height;
    }
}