SetSelected on listbox working oddly

  • Thread starter Thread starter Peter Mancini
  • Start date Start date
P

Peter Mancini

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
 
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^
============================================
 
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
 
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);
}
 
Back
Top