Link to home
Start Free TrialLog in
Avatar of Bronco78th
Bronco78thFlag for United Kingdom of Great Britain and Northern Ireland

asked on

C# .SetSelection missing?

Hi guys,

I have a listbox and a delete button.

The first item in the listbox should never be deleted and ive stopped that by disabling the button when the top item is selected.

the problem I have is that if the listbox contains multiple items and the user was to delete all those extra items they still need to click the top item in the listbox to disable the button.

this is less than ideal because if the user just clicks the button with just the top item leaved C# throws an unhanded.

My solution is to simply make it so when the delete button is pressed it automatically selects the top item in the list box after deletion, thus making it impossible to delete that top item.

After a google I came across

http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.selectionmode%28v=VS.90%29.aspx

and the listBox1.SetSelected(4,true); which would seemingly solve my problem and enable me to set the selected item, but my listbox seems to be missing this function.

Any reason for this?

Im using WPF .Net 3.5 and VS2010

Regards,

John
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America 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 Bronco78th

ASKER

Hey There idle mind,

That function you provided works like a charm, got a couple of questions though. please excuse my dumbness here ....though ive upped the points abit to make it worth your while.

on   button1.IsEnabled = (listBox1.SelectedIndex > 0);.... how do you know if its true or false?,

I never even knew I could control objects in that way, good to know.

on the button....

if (index > 0) presumably is saying basically the same as lbPassnames.SelectedIndex = 0.....

 if (index < listBox1.Items.Count)
                {
                    listBox1.SelectedIndex = index;
                }
                else
                {
                    listBox1.SelectedIndex = (listBox1.Items.Count - 1);
                }

Open in new window


am I right in saying that basically if only item '0' is in the listbox it will select '0', if however more items are in the lisboxt it will select the next one down in the list (-1)?

also, although you have come up with a working solution to my problem, my orginal question (and topic title) remains unanswered, where is my .SetSelected function? :)

Many Thanks for your time though.

John






For the first line:

    button1.IsEnabled = (listBox1.SelectedIndex > 0);

The right side of the statement is evaluated first:  (everything to the right of the equals "=" sign)

    (listBox1.SelectedIndex > 0)

So the above part will resolve to true if listBox1.SelectedIndex is greater than zero, and will be false otherwise (less than or equal to zero).  After the right side has been resolved to true/false, it will then be assigned to the IsEnabled() property of button1.

For the second part, this:

    if (index > 0)

Is equivalent to:

    if (listBox1.SelectedIndex > 0)

Because in the line prior, I had assigned "index" to be the SelectedIndex() value:

    int index = listBox1.SelectedIndex;

I used "index" because I wanted to store the current SelectedIndex() value and use it later.  Also, the act of removing the selected item would change SelectedIndex() so I needed to preserve its state in a separate variable.

After removing the selected item (which must be at Index >= 1), I select either the item that is now in the same spot as the deleted one...or I select the new last item in the listbox if the deleted item was previously the last item.  If there is only the one item in the first spot left then it will be selected and the button will be automatically disabled.

To be honest, I barely know my way around the WPF world (I'm a WinForms developer) so I'm not sure where the SetSelected() method is.
Think I got it, just double checking!

about the .Setselected Just fired up a Win-forms app and '.Setselected' appears on a listbox in that so oviously its either been removed or renamed/done a different way in WPF.

John