简体   繁体   English

如何将列表项拖放到标签上,使其成为C#中标签的文本?

[英]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 : 我也相信这对于labeldragEnter事件是正确的:

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.

相关问题 c# 如何将文本从 label 拖放到 datagridview 中的特定单元格中 - c# How do I drag and drop text from label into a specific cell in a datagridview 如何在运行时拖动,删除和调整面板上的标签? C#,winForms - How to Drag, Drop and Resize Label on a Panel at runtime? C#, winForms 读取文档库中的文件:“应用于此项目的标签...”C# - Read file in document library : “The label that's applied to this item…” C# C#Winforms-更改Listview项的标签 - C# Winforms - Change a Listview item's label 在C#中的函数调用之前设置标签的文本 - Set a Label's Text before a function call in C# 在C#中以另一种形式更改标签的文本? - Changing a label's text in another form in C#? 将标签的文本绑定到 Xamarin forms c# 中的变量 - Binding Label's Text to a Variable in Xamarin forms c# 如何根据从下拉列表中选择的内容(ASP.NET中的C#)更改标签的输出 - How can I change the output of a label, based on what's selected from a dropdown list (C# in ASP.NET) 如何设置列表框项目值中的标签文本? C# - How can i set label text from listbox item value? c# 如何在图片框后面放置标签,以便在图片框c#下可以看到标签? - How to put label behind the picturebox so label can be seen under picturebox c#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM