一个按钮移动时如何重设表单中多个按钮的位置

时间:2019-03-25 01:20:10

标签: c# drag-and-drop

我正在为我的A Level计算课程编写拖放游戏。我的拖放工作正常,但是我有6个按钮/选项,并且我想在移动1个按钮时重设其他5个按钮的位置。这6个按钮分别命名为btnAnswer1,btnAnswer2,btnAnswer3等。

我已经尝试搜索解决方案,但仍然无法解决问题

bool isDragged = false;
Point ptOffset;
private void buttonMouseDown(object sender, MouseEventArgs e) {
    Button theButton = (Button)sender;
    if (e.Button == MouseButtons.Left) {
        isDragged = true;
        Point ptStartPosition = theButton.PointToScreen(new Point(e.X, e.Y));

        ptOffset = new Point();
        ptOffset.X = theButton.Location.X - ptStartPosition.X;
        ptOffset.Y = theButton.Location.Y - ptStartPosition.Y;
    } else {
        isDragged = false;
    }
}

private void buttonMouseMove(object sender, MouseEventArgs e) {
    Button theButton = (Button)sender;
    if (isDragged) {
        Point newPoint = theButton.PointToScreen(new Point(e.X, e.Y));
        newPoint.Offset(ptOffset);
        theButton.Location = newPoint;
    }
}

private void buttonMouseUp(object sender, MouseEventArgs e) {
    Button theButton = (Button)sender;
    isDragged = false;

    if ((theButton.Location.X >= 190 && theButton.Location.X <= 468) && (theButton.Location.Y >= 42 && theButton.Location.Y <= 236)) {
        answerText = theButton.Text;
        if (answerText == RandomQuestion[0].CorrectAnswerPosition) {
            MessageBox.Show("Correct Answer");
        }
        else MessageBox.Show("Wrong Answer");
     // disableDragDrop();
    }
}

当我移动1个按钮时,我不知道如何重设其他5个按钮的位置。

1 个答案:

答案 0 :(得分:0)

您可以通过这种方式更改按钮关闭位置

        yourButton.Location = new Point(50, 50); //x and y cordinate off position

您应该将方法中的5个按钮位置保持在其中用我的方法移动1个按钮的位置。希望这可以帮助。