将列表视图中的项目拖到表单中

问题描述 投票:0回答:1

我有两种形式;其中一个包含listview,另一个仅是一种表单。

我想做一件事:如果我将列表视图中的项目拖到窗体上,则会弹出一个消息框。并且消息将是该项目的文本。

但是我不知道为什么“ SelectedItem”为空。当我跟踪SelectedItem时,它为null。

我发现我必须使用MouseDown和DragDrop事件,但我不知道如何使用。

第一个是列表视图的代码:

rListCtrl.MouseDown += rListCtrl_MouseDown;
rListCtrl.DragDrop += rListCtrl_DragDrop;


private void rListCtrl_MouseDown(object sender, MouseEventArgs e)
{
    StringBuilder sb = new STringBuilder();
    sb.Append(radListView1.SelectedItem.ToString());
    testName = sb.ToString();
}

private void rListCtrl_DragDrop(object sender, DragEventArgs e){
{
    MessageBox.Show(testName);
}

radListView1是列表视图的名称。

c# winforms listview drag-and-drop telerik
1个回答
0
投票

SelectedItem为空的原因是,仅当您实际执行单击时才选择项目,而不仅仅是MouseDown。但是,您可以使用IndexFromPoint方法获取在引发MouseDown事件时将鼠标置于其上的Item:

private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
    int index = listBox1.IndexFromPoint(e.Location);
    listBox1.SelectedIndex = index;
    var selectedItem = listBox1.Items[index];
}
© www.soinside.com 2019 - 2024. All rights reserved.