Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Listview selectionchanged激发两次,如何控制?_C#_Listview_Selectedindexchanged - Fatal编程技术网

C# Listview selectionchanged激发两次,如何控制?

C# Listview selectionchanged激发两次,如何控制?,c#,listview,selectedindexchanged,C#,Listview,Selectedindexchanged,在显示表单后,此加载开始。我知道这是故意的。但我不喜欢再表演了 private void HandleLSVVacunacion_SelectedIndexChanged(object sender, EventArgs e) { ListView SourceControl = (ListView)sender; if (SourceControl.SelectedItems.Count > 0) {

在显示表单后,此加载开始。我知道这是故意的。但我不喜欢再表演了

    private void HandleLSVVacunacion_SelectedIndexChanged(object sender, EventArgs e)
    {
        ListView SourceControl = (ListView)sender;

        if (SourceControl.SelectedItems.Count > 0)
        {
            int IdVacunacion = Convert.ToInt32(LSVVacunacion.SelectedItems[0].SubItems[0].Text.Trim());
            Formularios.FrmVacunacion f = new Formularios.FrmVacunacion();
            f.ID = IdVacunacion;
            f.HC = hc;
            f.ShowDialog();
            CargarEsquemaVacunacion(); // reload The ListView with changes; and show again the form!

        }

    }

如果
CargarEsquemaVacunacion
中的代码导致事件处理程序重新输入,则可以删除事件处理程序,重新加载列表并重新应用事件处理程序

    if (SourceControl.SelectedItems.Count > 0)
    {
        .....
        f.ShowDialog();
        try
        {
            LSVVacunacion.SelectedIndexChanged -= HandleLSVVacunacion_SelectedIndexChanged;
            CargarEsquemaVacunacion(); 
        }
        finally
        {
            LSVVacunacion.SelectedIndexChanged += HandleLSVVacunacion_SelectedIndexChanged;
        }

    }
请注意,此代码位于try/finally块中,以确保在异常情况下,事件处理程序也会重新应用于事件SelectedIndexChanged

调用HandlelsVacunacion_SelectedIndexChanged(发送方,e);