如何通过鼠标从按钮数组中移动按钮?C#
我对C#编码真的是个新手。 我有一个按钮数组,我想移动我用鼠标左键点击的按钮。有什么建议吗如何通过鼠标从按钮数组中移动按钮?C#,c#,arrays,button,C#,Arrays,Button,我对C#编码真的是个新手。 我有一个按钮数组,我想移动我用鼠标左键点击的按钮。有什么建议吗 private void CreateTable(Button[] Tabla) { int horizotal = 45; int vertical = 45; for (int i = 0; i < Tabla.Length; i++) { Tabla[i] = new Button()
private void CreateTable(Button[] Tabla)
{
int horizotal = 45;
int vertical = 45;
for (int i = 0; i < Tabla.Length; i++)
{
Tabla[i] = new Button();
Tabla[i].BackColor = Color.Azure;
Tabla[i].Size = new Size(45, 45);
Tabla[i].Location = new Point(horizotal, vertical);
if ((i == 14) || (i == 29) || (i == 44) || (i == 59) || (i == 74) ||
(i == 89) || (i == 104) || (i == 119) || i == 134 || i == 149 || i == 164 || i == 179 || i == 194 || i == 209)
{
vertical = 45;
horizotal = horizotal + 45;
}
else
vertical = vertical + 45;
this.Controls.Add(Tabla[i]);
}
}
private void CreateTable(按钮[]Tabla)
{
水平总积分=45;
int垂直=45;
对于(int i=0;i
这是创建“我的按钮”的代码。首先,在按钮中添加mouseeventhandler,然后像这样声明mouseeventhandler
private void CreateTable(Button[] Tabla)
{
int horizotal = 45;
int vertical = 45;
for (int i = 0; i < Tabla.Length; i++)
{
Tabla[i] = new Button();
Tabla[i].BackColor = Color.Azure;
Tabla[i].Size = new Size(45, 45);
Tabla[i].Location = new Point(horizotal, vertical);
if ((i == 14) || (i == 29) || (i == 44) || (i == 59) || (i == 74) ||
(i == 89) || (i == 104) || (i == 119) || i == 134 || i == 149 || i == 164 || i == 179 || i == 194 || i == 209)
{
vertical = 45;
horizotal = horizotal + 45;
}
else
vertical = vertical + 45;
Tabla[i].MouseDown += new MouseEventHandler(button_down);//adding Down handler
Tabla[i].MouseMove += new MouseEventHandler(button_move);//adding Move handler
this.Controls.Add(Tabla[i]);
}
}
private void button_down(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
starting.X = e.Location.X;
starting.Y = e.Location.Y;
}
}
private void button_move(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
((Button)sender).Left += e.Location.X - starting.X;
((Button)sender).Top += e.Location.Y - starting.Y;
}
}
private void CreateTable(按钮[]Tabla)
{
水平总积分=45;
int垂直=45;
对于(int i=0;i
发送方是处理程序的调用方,我们知道它是一个按钮。所以我们可以这样使用它。ı我认为这个代码非常适合你,但你的问题是使用字母按钮。你可以用picturebox来表示字母,它们比按钮更合适。最后,我认为你应该声明你的按钮数组。我不知道你说的“用鼠标移动按钮”是什么意思。你的意思是你希望按钮在点击时移动,或者你希望能够拖动按钮,就像你可以在桌面上拖动文件一样?基本上,我正在尝试制作一个拼字游戏,我想将一个按钮拖动到另一个按钮的顶部,这样它们就可以合并了。对不起,如果我不清楚的话,我知道我的英语很差。你提到的功能叫做“拖放”。将本文作为如何在Windows窗体应用程序上执行此操作的起点:@Kevin是正确的,请阅读有关“拖放”的更多信息