【问题标题】:How can you drag and drop a list item to a label so it's the label's text in C#?如何将列表项拖放到标签上,使其成为 C# 中标签的文本?
【发布时间】:2013-06-17 05:21:10
【问题描述】:

我希望能够将列表项从列表框中拖到标签上,这样当您放下列表项时,它就会成为标签的文本。

我认为我的鼠标按下部分是正确的:

private void listPlayers_MouseDown(object sender, MouseEventArgs e)
        {
            DoDragDrop(listPlayers.SelectedItem.ToString(), DragDropEffects.Copy);
        }

我也相信这对于dragEnterlabel 事件是正确的:

private void posLB_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.Text))
                e.Effect = DragDropEffects.Copy;
            else
                e.Effect = DragDropEffects.None;
        }

但是,我不知道如何让DragDrop 事件为label 工作。我以为会是这样的:

private void posLB_DragDrop(object sender, DragEventArgs e)
        {
            posLB.text(e.Data.GetData(DataFormats.Text);
        }

但这有错误。

【问题讨论】:

    标签: c# winforms listview drag-and-drop labels


    【解决方案1】:

    测试正确的类型然后抓取它:

        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;
            }
        }
    

    【讨论】:

    • 它抱怨 posLB.Text(s);带有以下内容:“不可调用的成员'System.Windows.Forms.Control.Text'不能像方法一样使用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多