Setting selected item in a list box
I have a listbox that displays one item at a time and the user scrolls up/down the list using the arrows on the side.
By default the first item on the list is selected and displayed when the window is created, but I want item #6.
ListBox.SelectedIndex does set the right parameter in the control internals, but does not affect the display, which continues to show item #1 in the box and it is highlighted, as if it's currently selected.
Re: Setting selected item in a list box
Please show us your code concerning the ListBox. SelectedIndex *should* do the trick and should both display and highlight the item corresponding to the index you've given it. I don't know why it's not working as expected. Your index count starts at zero, correct?
/P
Re: Setting selected item in a list box
As petes1234 said SelectedIndex is what you need to use if you want to change the item in the list box. Make sure your not setting the index some place else as well. For example, if you are setting the index in form_load to 5 (which will be index 6), and then also setting it in another function called at the end of form_load to 0, it will appear that your code is not working. If selected index doesnt work for you I'd suggest dusting off the ol' debugger and walking through the code line by line until you find out where the problem is.
Re: Setting selected item in a list box
you need to set the selection mode to "One".
after that you can simply use the selecteditem or selectedindex as Petes said.
to scroll your selection to top, set the "TopIndex" property to the desired index
Re: Setting selected item in a list box
There's not much code to show. It's all in the form constructor:
Code:
public MyFormRun()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
this.listBoxSeed.SelectedIndex = 6;
this.listBoxSeed.TopIndex = 6;
}
I have no other explicit reference to the ListBox index, but I guess there's an implicit one that runs after my own code.
Is Load the last event to be generated before the Form is displayed?
How do I create an event handler for it?
Re: Setting selected item in a list box
Quote:
Originally Posted by Andy Tacker
you need to set the selection mode to "One".
after that you can simply use the selecteditem or selectedindex as Petes said.
SelectionMode is set to "One" by default. Reguardless of what mode you use the SelectedIndex property will still work correctly.
You can remove TopIndex unless you intend to move that index to the top. Its not required to set the SelectedIndex.
That code should work in the constructor, but you can try moving it to the form_load event by double clicking on the form to create the event in the code.
If all else fails you can simple create a new project drop a list box onto the form add some junk data and set the SelectedIndex in form_load and you will see that it works correctly.
Did you change any other options for the list box? Look in the property window to see what properties are in bold print which means they were changed from their default setting.
I'm assuming this is a windows-based application not a web application...?, it wasnt specified.
Edit: BTW, You said you wanted "Item" number six, if this is the case you need to use index number 5 not index number 6. Remember that the index always starts off at 0 not 1. ;)