在表单周围移动按钮

时间:2014-03-03 05:00:35

标签: c# controls move

我希望在此动作中移动表格周围的按钮

|<----------
|           ^
|           |
|           |
|           |
|           |
V---------->|

一次一步(每次点击)

到目前为止,这是我的代码(无法正确移动):

if (btnClicker.Location.X > this.ClientSize.Width - btnClicker.Width)
      btnClicker.Location = new Point(btnClicker.Location.X, btnClicker.Location.Y + 1);
else if (btnClicker.Location.X < btnClicker.Width)
      btnClicker.Location = new Point(btnClicker.Location.X+1, btnClicker.Location.Y);
else if (btnClicker.Location.Y > this.ClientSize.Height - btnClicker.Height)
      btnClicker.Location = new Point(btnClicker.Location.X, btnClicker.Location.Y - 1);
else if (btnClicker.Location.Y < this.ClientSize.Height - btnClicker.Height)
      btnClicker.Location = new Point(btnClicker.Location.X - 1, btnClicker.Location.Y);

1 个答案:

答案 0 :(得分:0)

我已经为你编写了这段代码。看看这个。我已经从我身边进行了部分测试,它看起来很不错,但在你的最后测试它。根据您的要求调整它。

bool _isFirst = true;
bool _reachedBottomRight = false;
bool _reachedTopRight = false;
int _increDecreValue = 1;
int StartXPos = 5;
int StartYPos = 5;

private void btnClicker_Click(object sender, EventArgs e)
{

    if (_isFirst)
    {
        //Set the start location of the button. This can be any position you want
        btnClicker.Location = new Point(StartXPos, StartYPos);
        _isFirst = false;
    }

    if (btnClicker.Location.Y < this.ClientSize.Height - btnClicker.Height && !_reachedBottomRight)
        btnClicker.Location = new Point(btnClicker.Location.X, btnClicker.Location.Y + _increDecreValue);
    else if (btnClicker.Location.X < this.ClientSize.Width - btnClicker.Width && !_reachedTopRight)
        btnClicker.Location = new Point(btnClicker.Location.X + _increDecreValue, btnClicker.Location.Y);
    else if (btnClicker.Location.Y > StartYPos)
    {
        btnClicker.Location = new Point(btnClicker.Location.X, btnClicker.Location.Y - _increDecreValue);
        _reachedBottomRight = true;
    }
    else if (btnClicker.Location.X > StartXPos)
    {
        btnClicker.Location = new Point(btnClicker.Location.X - _increDecreValue, btnClicker.Location.Y);
        _reachedTopRight = true;
    }

    if (btnClicker.Location.X <= StartXPos && btnClicker.Location.Y <= StartYPos)
    {
        _reachedBottomRight = false;
        _reachedTopRight = false;
    }
}

如果这不是您要找的,请发表评论。

希望这有帮助。