Domanda

Sto scrivendo un codice per rilevare attivando le selezioni in un elenco WindForms con Multiselect acceso. Dato che SelectedIndExChanged mi consente solo di vedere cosa è selezionato dopo il clic, stavo cercando un modo per rilevare ciò che è stato selezionato prima che venisse cliccata. Ho implementato l'evento MouseDown e posso ottenere esattamente ciò che voglio, ma un sfortunato effetto collaterale è che ho ucciso l'evento selezionatoIntenexchanged. Non si spara.

è questo comportamento conosciuto? Ci sono dei pensieri sull'elenco di selezione prima del clic?

Grazie.

Modificato per includere frammenti di codice come richiesto.

Designer Generato eventi:

this.lbPhysicianClinic.SelectedIndexChanged += new System.EventHandler( this.lbPhysicianClinic_SelectedIndexChanged );
this.lbPhysicianClinic.MouseDown += new System.Windows.Forms.MouseEventHandler( this.lbPhysicianClinic_MouseDown );
.

Codice Snippet che mostra Evento MouseDown:

private void lbPhysicianClinic_MouseDown( object sender, MouseEventArgs e )
    {
        List<Clinic_List_ByPhysicianResult> Selected = this.PhysicianGetSelectedClinics( this.lbPhysicianClinic.SelectedIndices );
    }
.

Codice Snippet che mostra Evento selezionatoINDExchanged:

private void lbPhysicianClinic_SelectedIndexChanged( object sender, EventArgs e )
    {
        try
        {
            if ( this.FormInitComplete && this.RefreshUIComplete )
            {
                List<Clinic_List_ByPhysicianResult> Selected = this.PhysicianGetSelectedClinics( this.lbPhysicianClinic.SelectedIndices );

                Clinic_List_ByPhysicianResult DroppedClinic = new Clinic_List_ByPhysicianResult();
.

Ho impostato un punto di interruzione in ogni evento e se l'evento MouseDown è lì, l'evento selectedIndexChanged non si spara mai. Si incende solo quando l'evento Mousedown è sparito.

Speriamo che questo chiarisca le cose.

È stato utile?

Soluzione

The ListBox changes its selection before it raises the MouseDown or SelectedIndexChanged events.

What you need to do is capture the underlying Win32 message and raise an event yourself. You can subclass ListBox to do this.

class MyListBox : ListBox
{
    private const int WM_LBUTTONDOWN = 0x201;

    public event EventHandler PreSelect;

    protected override void WndProc(ref Message m)
    {
        switch (m.Msg)
        {
            case WM_LBUTTONDOWN:
                OnPreSelect();
                break;
        }

        base.WndProc(ref m);
    }

    protected void OnPreSelect()
    {
        if(null!=PreSelect)
            PreSelect(this, new EventArgs());
    }

}

You can use the MyListBox class, and add a handler for the PreSelect event like so:

this.lbPhysicianClinic.PreSelect += 
    new EventHandler(this.lbPhysicianClinic_PreSelect);

Inside the event handler you can access the selected indices before the listbox has changed them.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top