【问题标题】:How to change the focused item in a ListView during a DragDrop event?如何在 DragDrop 事件期间更改 ListView 中的焦点项目?
【发布时间】:2016-04-29 13:26:41
【问题描述】:

我有一个带有 DragDrop 功能的 ListView。我希望拖动的项目在 DragDrop 之后保持选中状态。

我有这个代码(用于 DragDrop)

private void commandListView_DragDrop(object sender, DragEventArgs e)
{
    Point point = commandListView.PointToClient(new Point(e.X, e.Y));
    int index = 0;

    try
    {
        index = commandListView.GetItemAt(point.X, point.Y).Index;
    }
    catch (Exception)
    {
    }

    if (index < 0)
    {
        index = commandListView.Items.Count - 1;
    }

    ListViewItem data = e.Data.GetData(typeof(ListViewItem)) as ListViewItem;
    commandListView.Items.Remove(data);
    commandListView.Items.Insert(index, data);
}

我试图用这个再次选择项目,但它不起作用

data.Selected = true;
data.Focused = true;

然后我测试了我是否可以专注于 ListView 中的第一项

commandListView.Items[0].Selected = true;
commandListView.Items[0].Focused = true;

但它也不起作用,所选项目没有改变。它始终是拖动项目在拖放之前的旧索引。

PS。我正在使用 WinForms

@Update

我已经试过了

commandListView.Focus();

但是没用

只是为了澄清拖放发生在同一个 ListView 中,我正在拖动项目以更改它们的顺序。

【问题讨论】:

  • 旁注:永远不要使用catch (Exception) {}(忽略所有抛出的异常)
  • 我不确定,但也许这可以帮助你:stackoverflow.com/questions/11745028/…
  • commandListView.Focus(); 你必须在焦点项目之后对控件本身进行焦点。
  • @DmitryBychenko 是的,我知道,我将其用作个人工具,我最不想要的就是例外哈哈哈。

标签: c# .net


【解决方案1】:

我找到了解决方案;我正在使用 MouseDown 事件来启动 DragDrop 操作。

现在我使用 ItemDrag 事件,一切正常,实际上我什至不需要聚焦项目,它是自动完成的。

【讨论】:

    猜你喜欢
    • 2014-11-06
    • 2011-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-31
    • 1970-01-01
    相关资源
    最近更新 更多