当鼠标点击标签时以及当鼠标键向上然后停止在组合框内移动时,我们如何移动标签

时间:2015-07-29 10:54:18

标签: c# windows

当尝试使用代码时,会出现两个标签,当移动时,屏幕会从移动的位置变为白色。我想用鼠标移动单标签移动。

bool mDown = false;
private void label13_MouseMove(object sender, MouseEventArgs e)
{
    if (mDown)
    {
        label13.Location = e.Location;
    }
}
private void label13_MouseDown(object sender, MouseEventArgs e)
{
    mDown = true;
}
private void label13_MouseUp(object sender, MouseEventArgs e)
{
    mDown = false;
}

1 个答案:

答案 0 :(得分:0)

e.Location为您提供相对于被点击的控件的鼠标位置。所以要解决这个问题,而不是

   def get_letter_grade(score):
    try:
        score = float(score)

        if score >= 90:
            return "A"
        elif score >= 80:
            return "B"
        elif score >= 70:
            return "C"
        elif score >= 60:
            return "D"
        else:
            return "F"
    except ValueError:
        print("score is not an number")
        return false

使用

   var pos = this.PointToClient(Cursor.Position);
   label13.Location = new Point(pos.X - offset.X, pos.Y - offset.Y);`

创建偏移量变量作为表单的属性(类型为Point)并在鼠标按下事件中初始化它:

label13.Location = e.Location;