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
Bronco78thAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
How about something like this?
private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            button1.IsEnabled = (listBox1.SelectedIndex > 0);
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            int index = listBox1.SelectedIndex;
            if (index > 0)
            {
                listBox1.Items.RemoveAt(listBox1.SelectedIndex);
                if (index < listBox1.Items.Count)
                {
                    listBox1.SelectedIndex = index;
                }
                else
                {
                    listBox1.SelectedIndex = (listBox1.Items.Count - 1);
                }
            }
        }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bronco78thAuthor Commented:
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






0
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
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.
0
Bronco78thAuthor Commented:
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

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.