Solved

Workaround:  ListBox SelectedIndexChanging Winforms event

Posted on 2006-10-22
6
633 Views
Last Modified: 2008-01-09
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
0
Comment
Question by:brotherbill999
  • 3
  • 2
6 Comments
 
LVL 13

Accepted Solution

by:
newyuppie earned 500 total points
Comment Utility
maybe you could use a TreeView control, which has the Before_Select() event with a cancel event parameter, and it has nearly the same fucntionalities as the listbox.
0
 
LVL 10

Expert Comment

by:gangwisch
Comment Utility
i usually just test for this error on the listbox_droppeddown  event
0
 

Author Comment

by:brotherbill999
Comment Utility
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
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:brotherbill999
Comment Utility
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.
0
 

Author Comment

by:brotherbill999
Comment Utility
To highlight the entire width of the row, set FullRowSelect property to True.
0
 
LVL 13

Expert Comment

by:newyuppie
Comment Utility
great it worked out for you, treeview is quite powerful and probably the best move.

thanks for the grade and the tips
NY
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now