[英]How can you drag and drop a list item to a label so it's the label's text in C#?
I want to be able to drag list items from a list box onto labels so that when you drop the list item, that becomes the text for the label. 我希望能够将列表项从列表框中拖到标签上,以便当您放下列表项时,它成为标签的文本。
I think I've got the mouse down part correct: 我想我的鼠标向下部分正确了:
private void listPlayers_MouseDown(object sender, MouseEventArgs e)
{
DoDragDrop(listPlayers.SelectedItem.ToString(), DragDropEffects.Copy);
}
I also believe this is correct for the dragEnter
event for the label
: 我也相信这对于
label
的dragEnter
事件是正确的:
private void posLB_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Text))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
However, I have no idea how to get the DragDrop
event working for the label
. 但是,我不知道如何使
DragDrop
事件适用于label
。 I thought it would be something like this: 我以为会是这样的:
private void posLB_DragDrop(object sender, DragEventArgs e)
{
posLB.text(e.Data.GetData(DataFormats.Text);
}
But that has errors. 但这有错误。
Test for correct type and then grab it: 测试正确的类型,然后抓住它:
private void posLB_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Text))
{
String s = e.Data.GetData(DataFormats.Text) as String;
if (!String.IsNullOrEmpty(s))
posLB.Text = s;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.