By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,022 Members | 1,198 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,022 IT Pros & Developers. It's quick & easy.

SetSelected on listbox working oddly

P: n/a
I have a simple winform with a listbox on it. I dynamically load the
box with integers (the years 1975 to present.) This works fine and I
can see the box has all of the items present. I then do a
mylistbox.SetSelected(0,true) so that the first item starts off
selected. It is a MultiSimple listbox. What happens is nothing. The
form draws and the listbox has the list of years but nothing is
selected. I do the selection in the MainForm() constructor.

Here's another odd bit. If I poke the listbox then it works and the
selection is fine. Thus if I follow up the SetSelected with:

int Kludge=mylistbox.SelectedItems.Count;

then it works. I've tried doing mylistbox.invalidate(),
mylistbox.refresh() and a couple of other things with no success. Am I
calling the SetSelected in the wrong place? If not, what would the
right place to add it be?

Thanks!
--Pete
Nov 16 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi Peter,

You have to call the SetSelected method in the Load event instead of the
constructor. Constructor is a good place for example to initialize objects,
however for any initialization to controls and forms that involve UI
elements should be done in the load event.

Hope it helps.

-- Ricky Lee
============================================
^o^ "When all doors are closed, God will open a Windows" ^o^
============================================
"Peter Mancini" <pm******@yahoo.com> wrote in message
news:O4**************@TK2MSFTNGP09.phx.gbl...
I have a simple winform with a listbox on it. I dynamically load the
box with integers (the years 1975 to present.) This works fine and I
can see the box has all of the items present. I then do a
mylistbox.SetSelected(0,true) so that the first item starts off
selected. It is a MultiSimple listbox. What happens is nothing. The
form draws and the listbox has the list of years but nothing is
selected. I do the selection in the MainForm() constructor.

Here's another odd bit. If I poke the listbox then it works and the
selection is fine. Thus if I follow up the SetSelected with:

int Kludge=mylistbox.SelectedItems.Count;

then it works. I've tried doing mylistbox.invalidate(),
mylistbox.refresh() and a couple of other things with no success. Am I
calling the SetSelected in the wrong place? If not, what would the
right place to add it be?

Thanks!
--Pete

Nov 16 '05 #2

P: n/a
I take it you mean to put the call in an OnLoad event handler for the
Winform? One doesn't exist for the winform automatically so I will
look into seeing how one builds one. Thanks for the lead.

--Pete

Nov 16 '05 #3

P: n/a
Wow that was easier than I thought. I overrode OnLoad as follows and
it worked like a charm:

protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
FYBox.SetSelected(0,true);
}

Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.