Link to home
Start Free TrialLog in
Avatar of brotherbill999
brotherbill999

asked on

Workaround: ListBox SelectedIndexChanging Winforms event

In .net it doesn't appear that Bill Gates provided the SelectedIndexChanging event for the ListBox, using Winforms.
This event would allow cancelling changing the SelectedIndex, depending on a cancel parameter.
It would be ideal to have this event.  Know anyone who has built this component as freeware
or payware?  I would pay $100 for a listbox with this feature, since this is requiring workarounds.

example usage:
Protected Sub  myListBox.SelectedIndexChanging(oldSelectIndex as Integer, proposedSelectIndex as Integer, e as SomeEventWithCancel)
   ' Some processing with oldSelectIndex
   If not processingWentOK()
         e.Cancel = True            ' which causes the proposed change to not occur.
   End
End Sub
ASKER CERTIFIED SOLUTION
Avatar of newyuppie
newyuppie
Flag of Ecuador image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gangwisch
gangwisch

i usually just test for this error on the listbox_droppeddown  event
Avatar of brotherbill999

ASKER

Great, here's a sample of using the treeView.BeforeSelect to prevent the user from selecting "World"
This event is also a great place to stick code which need to run with the existing state, but know whats coming.
For me, it will be used to save data.

    Private Sub TreeView1_BeforeSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView1.BeforeSelect
        Dim tn As TreeNode = TreeView1.SelectedNode   ' This was selected node prior to this selection
        If e.Node.Text = "World" Then                            ' e.Node.Text is the proposed node by the user
            e.Cancel = True                                            ' Reject the user's change, keeping the prior node
        End If
    End Sub

BTW, on mouse down, the proposed node shows selected, but on mouse up, it reverts to the prior selected.

Ah, if only the .NET team would have added this event to the humble ListBox

Brother Bill
A minor difference between the ListBox and TreeView is that the ListBox highlights the entire line, whereas the Treeview only highlights the node's Text.
I can live with that.
To highlight the entire width of the row, set FullRowSelect property to True.
great it worked out for you, treeview is quite powerful and probably the best move.

thanks for the grade and the tips
NY