C# 从列表框拖放到面板
我正在做我的第一个拖放应用程序。我有一个工具箱,您可以在其中找到标签、按钮和其他组件,就像VisualStudio一样。中间有面板。我希望用户将一个按钮拖放到面板上。我已经写了一些代码,但没有做拖放技巧 这是截图 这是我的代码,应该用来处理拖放操作C# 从列表框拖放到面板,c#,winforms,drag-and-drop,C#,Winforms,Drag And Drop,我正在做我的第一个拖放应用程序。我有一个工具箱,您可以在其中找到标签、按钮和其他组件,就像VisualStudio一样。中间有面板。我希望用户将一个按钮拖放到面板上。我已经写了一些代码,但没有做拖放技巧 这是截图 这是我的代码,应该用来处理拖放操作 private void listBox1_MouseDown(object sender, MouseEventArgs e) { ListBox box = (ListBox)sender; String
private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
ListBox box = (ListBox)sender;
String selectedValue = box.Text;
DoDragDrop(selectedValue.ToString(), DragDropEffects.Copy);
}
private void pnl_form_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Text))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void pnl_form_DragDrop(object sender, DragEventArgs e)
{
Label newLabel = new Label();
newLabel.Name = "testLabel";
newLabel.Text = e.Data.GetData(DataFormats.Text).ToString();
newLabel.AutoSize = true;
newLabel.Parent = pnl_form;
}
我做错什么了吗?记得在要放置内容的控件上设置
AllowDrop=true
private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
String selectedValue = (listBox1.SelectedItem ?? "NULL").ToString();
DoDragDrop(selectedValue, DragDropEffects.Copy);
}
selectedValue获取选定对象的正确名称。假设您选择一个单选按钮,selectedValue设置为“Radiobutton”@ayilmaz您想从列表框中拖动什么?您的代码只获取
列表框的文本,该文本为空。它不是空的。现在,我只想从工具箱中选择button对象,获取它的名称,即“button”。然后创建一个标签,将其文本设置为“按钮”,并将其放入面板中。我现在正在尝试最简单的事情。但是selectedValue正在获取名称perfectly@ayilmaz如果是这样,我的代码应该可以工作。顺便说一句,您是否手动设置了列表框的文本
?否则,默认情况下它应该是空的。谢谢你的努力,但是你的代码和我的代码没有什么不同。我测试了你的代码,没有任何区别