【问题标题】:How to reset the location of multiple buttons in a form when one button is moved移动一个按钮时如何重置表单中多个按钮的位置
【发布时间】:2019-03-25 01:20:10
【问题描述】:

我正在为我的 A 级计算课程制作一个拖放游戏。我的拖放工作正常,但我有 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 个按钮的位置。

【问题讨论】:

    标签: c# drag-and-drop


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-08-13
      • 1970-01-01
      • 2012-06-28
      • 1970-01-01
      • 1970-01-01
      • 2014-10-17
      • 1970-01-01
      相关资源
      最近更新 更多