Listview双击无法正常工作

时间:2013-10-16 19:40:00

标签: c# listview

我对这个listivew有疑问。当我双击它时,它应该显示我点击的行的第一个单元格的信息对话框,它完全正常。

现在的问题是,如果我一直点击行,我仍然可以从之前点击的行中获取对话框。

I click a random row 1 time: Shows me dialog
I click another random row: Shows me dialog from the first row I clicked, then when I close said dialog it shows me the dialog of info of the row I clicked.
I click another random row: Shows me dialog from the first row I clicked and from the second one.

..它一直在继续

以下是代码:

    private void listViewPlayers_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        string
            pUser = "";

        int
            pID = -1;

        pID = Convert.ToInt32(listViewPlayers.SelectedItems[0].SubItems[0].Text);
        pUser = listViewPlayers.SelectedItems[0].SubItems[1].Text;

        bw.WorkerReportsProgress = true;

        bw.DoWork += new DoWorkEventHandler
        (
            delegate(object o, DoWorkEventArgs args)
            {
                BackgroundWorker b = o as BackgroundWorker;

                pInfo = pInfoClient.DownloadString("get info from web").Trim();
            }
        );

        bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler
        (
            delegate(object o, RunWorkerCompletedEventArgs args)
            {
                if (strcmp(pInfo, "ERROR"))
                {
                    MessageBox.Show("There was an error retrieving user information!\nPlease try again later..", "Error");
                    return;
                }

                string[]
                    iSplit = pInfo.Split(new char[] { (char)'|' }, System.StringSplitOptions.RemoveEmptyEntries);

                playerInfo iInfo = new playerInfo(pUser, pID, iSplit);

                iInfo.ShowDialog();
            }
        );

        bw.RunWorkerAsync();
    }

请原谅我对这个问题的解释,我很不错。

1 个答案:

答案 0 :(得分:1)

您可能无意中发生内存泄漏情况。

看起来“bw”是一个类变量?如果是这样,您的click事件会不断向后台工作程序的事件添加新的处理程序,但永远不会删除处理程序,因此每次鼠标单击事件触发时它都会告诉后台工作人员执行此操作并且这些事件会触发所有以前的处理程序曾经登记过。

您可以尝试每次新建后台工作程序,这可以解决问题,或者在DoWork事件中,您可能希望确保取消注册委托。 I.E.

var inlineHandler = new DoWorkEventHandler ( delegate(object o, DoWorkEventArgs args) 
{
    BackgroundWorker b = o as BackgroundWorker;
    pInfo = pInfoClient.DownloadString("get info from web").Trim();
    bw.DoWork -= inlineHandler;

});
bw.DoWork += inlineHandler;

我强烈建议每次都要新工人。