C# 在flowLayoutPanel中重新排列CustomControl
我在FLP中添加了多个自定义控件(它是一个下面有标签的按钮)。我试图通过将一个控件拖动到另一个控件上来重新排列添加的自定义控件。在一些帮助下,它可以使用简单的按钮,但不能使用自定义控件。。 另一个问题是,如果我使用简单按钮,则仅当FLP在表单上时有效,但如果我将FLP放在tabControl中的tabPage中,则不再有效。。 为什么不使用自定义控件以及FLP在tabControl中时 这就是我迄今为止一直在尝试的:C# 在flowLayoutPanel中重新排列CustomControl,c#,custom-controls,tabcontrol,flowlayoutpanel,C#,Custom Controls,Tabcontrol,Flowlayoutpanel,我在FLP中添加了多个自定义控件(它是一个下面有标签的按钮)。我试图通过将一个控件拖动到另一个控件上来重新排列添加的自定义控件。在一些帮助下,它可以使用简单的按钮,但不能使用自定义控件。。 另一个问题是,如果我使用简单按钮,则仅当FLP在表单上时有效,但如果我将FLP放在tabControl中的tabPage中,则不再有效。。 为什么不使用自定义控件以及FLP在tabControl中时 这就是我迄今为止一直在尝试的: public partial class test_frm : Form {
public partial class test_frm : Form
{
FlowLayoutPanel flowLayoutPanel1 = new FlowLayoutPanel();
private List<Control> _items = new List<Control>();
public test_frm()
{
InitializeComponent();
flowLayoutPanel1.AllowDrop = true;
tabControl1.AllowDrop = true;
flowLayoutPanel1.Dock = DockStyle.Fill;
this.Controls.Add(flowLayoutPanel1);
tabPage1.Controls.Add(flowLayoutPanel1);
flowLayoutPanel1.DragEnter += new DragEventHandler(flowLayoutPanel1_DragEnter);
flowLayoutPanel1.DragDrop += new DragEventHandler(flowLayoutPanel1_DragDrop);
//add custom controls
for (int i = 0; i < 10; i++)
{
Button button = new Button();
Label lbl = new Label();
CustomControl cst = new CustomControl(button, lbl);
button.Text = "Button " + i.ToString();
lbl.Text = "lbl " + i.ToString();
cst.Name = "Button " + i.ToString();
this._items.Add(cst);
flowLayoutPanel1.Controls.Add(cst);
cst.MouseDown += new MouseEventHandler(button_MouseDown);
}
}
void flowLayoutPanel1_DragDrop(object sender, DragEventArgs e)
{
List<Control> controls = new List<Control>(flowLayoutPanel1.Controls.Count); // get a copy of the controls on the FLP
foreach (Control ctr in flowLayoutPanel1.Controls)
{
controls.Add(ctr);
}
for (int i = 0; i < controls.Count; i++)
{
Point mouse = PointToClient(new Point(e.X, e.Y));
if (controls[i].Bounds.Contains(mouse.X - flowLayoutPanel1.Left, mouse.Y - flowLayoutPanel1.Top))
//If the control is dragged to another control inside the FlowLayoutPanel, move the dragged control to that place.
{
string name = (string)e.Data.GetData(typeof(string));
Control drag = flowLayoutPanel1.Controls.Find(name, true)[0];
Control temp = controls[i];
controls.RemoveAt(getIndex(drag.Name));
controls.Insert(i, drag);
flowLayoutPanel1.Controls.Clear(); //Clear the controls
for (int j = 0; j < controls.Count; j++)
{
flowLayoutPanel1.Controls.Add(controls[j]); //Readd all the Controls in new order
}
break;
}
}
}
private int getIndex(string name)
{
int result = -1;
for (int i = 0; i < flowLayoutPanel1.Controls.Count; i++)
{
if (flowLayoutPanel1.Controls[i].Name == name)
{
result = i;
break;
}
}
return result;
}
void flowLayoutPanel1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
void button_MouseDown(object sender, MouseEventArgs e)
{
(sender as CustomControl).DoDragDrop((sender as CustomControl).Name, DragDropEffects.Copy);
}
}
public class CustomControl : Control
{
private Button _button;
private Label _label;
public CustomControl(Button button, Label label)
{
_button = button;
_label = label;
button.Width = 64;
button.Height = 65;
button.AutoSize = true;
label.Width = 65;
button.FlatStyle = FlatStyle.Flat;
button.AllowDrop = true;
//button.Margin = new Padding(15, 15, 15, 15);
button.BackgroundImageLayout = ImageLayout.Stretch;
Height = button.Height + label.Height;
Width = 68;
// Width = Math.Max(button.Width, label.Width);
Controls.Add(_button);
_button.Location = new Point(0, 0);
Controls.Add(_label);
_label.Location = new Point(0, button.Height);
}
公共部分类测试\u frm:Form
{
FlowLayoutPanel FlowLayoutPanel 1=新的FlowLayoutPanel();
私有列表_items=新列表();
公共测试
{
初始化组件();
flowLayoutPanel1.AllowDrop=true;
tabControl1.AllowDrop=true;
flowLayoutPanel1.Dock=DockStyle.Fill;
this.Controls.Add(flowLayoutPanel1);
tabPage1.Controls.Add(flowLayoutPanel1);
flowLayoutPanel1.DragEnter+=新的DragEventHandler(flowLayoutPanel1\u DragEnter);
flowLayoutPanel1.DragDrop+=新的DragEventHandler(flowLayoutPanel1_DragDrop);
//添加自定义控件
对于(int i=0;i<10;i++)
{
按钮按钮=新按钮();
标签lbl=新标签();
CustomControl cst=新CustomControl(按钮,lbl);
button.Text=“button”+i.ToString();
lbl.Text=“lbl”+i.ToString();
cst.Name=“Button”+i.ToString();
此项增加(cst);
flowLayoutPanel1.Controls.Add(cst);
cst.MouseDown+=新的MouseEventHandler(按钮\u MouseDown);
}
}
无效flowLayoutPanel1_DragDrop(对象发送器,DragEventArgs e)
{
列表控件=新列表(flowLayoutPanel1.controls.Count);//获取FLP上控件的副本
foreach(flowLayoutPanel1.控件中的控制中心)
{
控件。添加(ctr);
}
for(int i=0;i
工作解决方案:
private void flowLayoutPanel1_DragDrop(object sender, DragEventArgs e)
{
Control target = new Control();
target.Parent = sender as Control;
if (target != null)
{
int targetIndex = FindCSTIndex(target.Parent);
if (targetIndex != -1)
{
string cst_ctrl = typeof(CustomControl).FullName;
if (e.Data.GetDataPresent(cst_ctrl))
{
Button source = new Button();
source.Parent = e.Data.GetData(cst_ctrl) as CustomControl;
if (targetIndex != -1)
this.flowLayoutPanel1.Controls.SetChildIndex(source.Parent, targetIndex);
}
}
}
}
private int FindCSTIndex(Control cst_ctr)
{
for (int i = 0; i < this.flowLayoutPanel1.Controls.Count; i++)
{
CustomControl target = this.flowLayoutPanel1.Controls[i] as CustomControl;
if (cst_ctr.Parent == target)
return i;
}
return -1;
}
private void OnCstMouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Control cst = sender as Control;
cst.DoDragDrop(cst.Parent, DragDropEffects.Move);
}
}
private void flowLayoutPanel1\u DragDrop(对象发送方,DragEventArgs e)
{
控制目标=新控制();
target.Parent=发送方作为控件;
如果(目标!=null)
{
int targetIndex=FindCSTIndex(target.Parent);
如果(targetIndex!=-1)
{
字符串cst_ctrl=typeof(CustomControl).FullName;
if(例如Data.GetDataPresent(cst_ctrl))
{
按钮源=新按钮();
source.Parent=e.Data.GetData(cst\u ctrl)作为CustomControl;
如果(targetIndex!=-1)
this.flowLayoutPanel1.Controls.SetChildIndex(source.Parent,targetIndex);
}
}
}
}
专用int FindCSTIndex(控制中心)
{
对于(int i=0;i