• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 534
  • Last Modified:

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
0
Bronco78th
Asked:
Bronco78th
  • 2
  • 2
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
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 TomlinsonMiddle School Assistant TeacherCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now