C# 在窗体周围移动按钮
我希望在这项议案中,在表格周围移动一个按钮C# 在窗体周围移动按钮,c#,controls,move,C#,Controls,Move,我希望在这项议案中,在表格周围移动一个按钮 |<---------- | ^ | | | | | | | | V---------->| || 一次一步(每次单击时) 以下是我的代码(未正确移动): if(btnClicker.Location.X>this.ClientSize.Width-btnClicker.Width) btnClicker.Location=新点(btnC
|<----------
| ^
| |
| |
| |
| |
V---------->|
||
一次一步(每次单击时)
以下是我的代码(未正确移动):
if(btnClicker.Location.X>this.ClientSize.Width-btnClicker.Width)
btnClicker.Location=新点(btnClicker.Location.X,btnClicker.Location.Y+1);
else if(btnClicker.Location.Xthis.ClientSize.Height-btnClicker.Height)
btnClicker.Location=新点(btnClicker.Location.X,btnClicker.Location.Y-1);
else if(btnClicker.Location.Y
我已经为您编写了此代码。过来看。我已经从我这边测试了一部分,它看起来不错,但在你这边彻底测试一下。根据您的要求调整它
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;
}
}
bool\u isFirst=true;
bool _reachedBottomRight=false;
bool_reachedTopRight=false;
int _incremedecevalue=1;
int StartXPos=5;
int StartYPos=5;
私有void btnClicker\u单击(对象发送者,事件参数e)
{
如果(_是第一个)
{
//设置按钮的起始位置。这可以是您想要的任何位置
btnClicker.Location=新点(StartXPos、StartYPos);
_isFirst=false;
}
if(btnClicker.Location.YStartYPos)
{
btnClicker.Location=新点(btnClicker.Location.X,btnClicker.Location.Y-\u递增递减值);
_reachedBottomRight=true;
}
else if(btnClicker.Location.X>StartXPos)
{
btnClicker.Location=新点(btnClicker.Location.X-_递增递减值,btnClicker.Location.Y);
_reachedTopRight=true;
}
如果(btnClicker.Location.X你所说的移动不正确是什么意思?它没有按照所需的模式/方向移动。按钮的初始位置在哪里?在表单的左上角?如果不是,那么你希望按钮先到达表单的哪一条边,然后开始移动?你还希望按钮横穿表单的哪一条边窗体的边缘?我希望按钮从按钮中心开始。我希望它与窗体边框之间有大约5个像素的间隙。
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;
}
}