我有一个带有07个按钮的面板,可以向左或向右移动它们,使用"拖放"。我的问题是当我在现有的按钮上面放一个按钮时。我把按钮放在已经存在的按钮43按钮45和按钮43低于45,如图所示。
这是我的代码
private void panelAtalhos_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void panelAtalhos_DragDrop(object sender, DragEventArgs e)
{
//create the button - treeview item selected
Button bt = new Button();
if (createbutton) // createbutton is a global variable
{
bt.Width = 38;
bt.Height = 34;
bt.Top = 2;
bt.Left = panelAtalhos.Controls.Count * (bt.Width);
bt.FlatAppearance.BorderSize = 0;
DateTime data = DateTime.Now;
bt.Text = data.ToString("ss");
bt.FlatStyle = FlatStyle.Flat;
panelAtalhos.Controls.Add(bt);
bt.MouseDown += button_MouseDown;
createbutton = false;
}
else
{
int resto = (int)(System.Windows.Forms.Cursor.Position.X / 38);
((Button)e.Data.GetData(typeof(Button))).Left = resto * 38;
}
}
private void button_MouseDown(object sender, MouseEventArgs e)
{
(sender as Button).DoDragDrop(sender as Button, DragDropEffects.Move);
criarbotao = false;
}
代码中缺少什么?
答案 0 :(得分:1)
int resto = (int)(System.Windows.Forms.Cursor.Position.X / 38);
你有两个问题。一个是Z顺序,另一个是此声明。这是不正确的,您必须将绝对光标位置映射到按钮的 relative 位置。相对于其父级的客户区域,面板。现在你将它送到杂草中,远在右边。足够远,以至于它不再可见。代码应该类似于:
var btn = (Button)e.Data.GetData(typeof(Button));
var pos = btn.Parent.PointToClient(Cursor.Position);
btn.Left = (pos.X / 38) * 38;
btn.BringToFront();