Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Workaround:  ListBox SelectedIndexChanging Winforms event

Posted on 2006-10-22
6
Medium Priority
?
652 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 2000 total points
ID: 17785514
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
ID: 17785792
i usually just test for this error on the listbox_droppeddown  event
0
 

Author Comment

by:brotherbill999
ID: 17786144
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:brotherbill999
ID: 17786163
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
ID: 17786173
To highlight the entire width of the row, set FullRowSelect property to True.
0
 
LVL 13

Expert Comment

by:newyuppie
ID: 17786445
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

886 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